用SDK创建一个简单带滚动条的窗口

//=====================================================================================================
//TITLE:
//  用SDK创建一个简单带滚动条的窗口
//AUTHOR:
//  norains
//DATE:
//  Friday 25-July-2006
//=====================================================================================================
  代码比较简单,思路是先创建一个主窗口framewindow,然后创建一个子窗口clientwindow,子窗口带有滚动条,可以随意滚动.
  在创建的时候,必须要遵循如下步骤:
  1.注册主窗口;注册子窗口
  2.用CreateWindow()函数创建主窗口
  3.在主窗口的WM_CREAT消息时创建子窗口,否则子窗口将无法显示
  还有一点要注意的是,采用CreateWindow创建子窗口时hMenu参数不能为空,并且创建完毕后需要设置子窗口位置,否则滚动条在如下代码中不会显示.
  
  
  
  
  
  
/************************************************************************/
FullWnd.h
/************************************************************************/
// Generic defines used by application

#define  IDC_CLIENT 2                    // Client window ID
#define FRAME_TITLE TEXT("ImageFrame")      //Full displayed frame window title
#define FRAME_CLASS_NAME TEXT("ImageFrame")   //Full displayed frame class name
#define CLIENT_TITLE TEXT("")          //Full displayed client window title
#define CLIENT_CLASS_NAME TEXT("ImageClient")  //Full displayed client window class name
//----------------------------------------------------------------------
//Client Window

int RegisterClient (HINSTANCE);
HWND InitClientInstance (HWND hWnd,HINSTANCE hInstance);
LRESULT CALLBACK ClientWndProc (HWND, UINT, WPARAM, LPARAM);

//----------------------------------------------------------------------
// Frame Window
//
BOOL RegisterFrame (HINSTANCE);
HWND InitFrameInstance (HINSTANCE, LPWSTR, int);
int TermFrameInstance (HINSTANCE, int);
// Window procedures
LRESULT CALLBACK FrameWndProc (HWND, UINT, WPARAM, LPARAM);
// Message handlers
LRESULT DoCreateFrame (HWND, UINT, WPARAM, LPARAM);
LRESULT DoSizeFrame (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyFrame (HWND, UINT, WPARAM, LPARAM);

 


/************************************************************************/
FullWndFrame.cpp
/************************************************************************/

#include "stdafx.h"
#include <windows.h>                 // For all that Windows stuff
#include "FullWnd.h"               // Program-specific stuff

//----------------------------------------------------------------------
// Global data
//
HINSTANCE hInst;                     // Program instance handle
//=====================================================================
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPWSTR lpCmdLine, int nCmdShow) {
    MSG msg;
    int rc = 0;
    HWND hwndFrame;

    // Initialize application.
    rc = RegisterFrame (hInstance);
    if (rc) return rc;

    // Initialize this instance.
    hwndFrame = InitFrameInstance (hInstance, lpCmdLine, nCmdShow);
    if (hwndFrame == 0)
        return 0x10;

    // Application message loop
    while (GetMessage (&msg, NULL, 0, 0)) {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    // Instance cleanup
    return TermInstance (hInstance, msg.wParam);
}
//=====================================================================
// Register Frame window
BOOL RegisterFrame (HINSTANCE hInstance) {
    WNDCLASS wc;

    // Register application frame window class.
    wc.style = 0;                             // Window style
    wc.lpfnWndProc = FrameWndProc;            // Callback function
    wc.cbClsExtra = 0;                        // Extra class data
    wc.cbWndExtra = 0;                        // Extra window data
    wc.hInstance = hInstance;                 // Owner handle
    wc.hIcon = NULL,                          // Application icon
    wc.hCursor = NULL;                        // Default cursor
    wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wc.lpszMenuName =  NULL;                  // Menu name
    wc.lpszClassName = FRAME_CLASS_NAME;             // Window class name

    if (RegisterClass (&wc) == 0)
    {
      return FALSE;
    }

    // Initialize client window class.
    if (RegisterClient (hInstance) != 0)
    {
      return FALSE;
    }
    return TRUE;
}
//=====================================================================
// Instance initialization for frame
//
HWND InitFrameInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
    HWND hWnd;
  
    // Save program instance handle in global variable.
   hInst = hInstance;

    // Create frame window.
    hWnd = CreateWindow (FRAME_CLASS_NAME,            // Window class
                         FRAME_TITLE, // Window title
                         WS_VISIBLE,           // Style flags
                         CW_USEDEFAULT,        // x position
                         CW_USEDEFAULT,        // y position
                         CW_USEDEFAULT,        // Initial width
                         CW_USEDEFAULT,        // Initial height
                         NULL,                 // Parent
                         NULL,                 // Menu, must be null
                         hInstance,            // Application instance
                         NULL);                // Pointer to create
                                               // parameters
    // Return fail code if window not created.
    if (!IsWindow (hWnd)) return 0;

    // Standard show and update calls
    ShowWindow (hWnd, nCmdShow);
    UpdateWindow (hWnd);
    return hWnd;
}
//=====================================================================
// TermInstance - Program cleanup
//
int TermFrameInstance (HINSTANCE hInstance, int nDefRC) {

    return nDefRC;
}
//======================================================================
// Callback function for application window
//
LRESULT CALLBACK FrameWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
                               LPARAM lParam) {

 switch(wMsg)
 {
  case WM_CREATE:
   return DoCreateFrame(hWnd, wMsg, wParam, lParam);
  case WM_SIZE:
   return DoSizeFrame(hWnd, wMsg, wParam, lParam);
  case WM_DESTROY:
   return DoDestroyFrame(hWnd, wMsg, wParam, lParam);
 }

    return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//=====================================================================
// Process WM_CREATE message for window.
//
LRESULT DoCreateFrame (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    HWND hWndClient;
   
    hWndClient=InitClientInstance(hWnd,hInst);
    if(hWndClient==NULL)
    {
     DestroyWindow(hWnd);
    }
    return 0;

}
//=====================================================================
// Process WM_SIZE message for window.
//
LRESULT DoSizeFrame (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam) {
    RECT rect;
    GetClientRect (hWnd, &rect);
    SetWindowPos (GetDlgItem (hWnd, IDC_CLIENT), NULL, rect.left, rect.top,
                  rect.right - rect.left, rect.bottom - rect.top,
                  SWP_NOZORDER);
 
   return 0;
}
//=====================================================================
// Process WM_DESTROY message for window.
//
LRESULT DoDestroyFrame (HWND hWnd, UINT wMsg, WPARAM wParam,
                        LPARAM lParam) {
    PostQuitMessage (0);
    return 0;
}

 


/************************************************************************/
FullWndClient.cpp
/************************************************************************/

#include "stdafx.h"
#include <windows.h>                 // For all that Windows stuff
#include "FullWnd.h"               // Program-specific stuff

//----------------------------------------------------------------------
// Resister client window
//
int RegisterClient (HINSTANCE hInstance) {
    WNDCLASS wc;

    // Register application client window class.
    wc.style = 0;                             // Window style
    wc.lpfnWndProc = ClientWndProc;           // Callback function
    wc.cbClsExtra = 0;                        // Extra class data
    wc.cbWndExtra = 0;                        // Extra window data
    wc.hInstance = hInstance;                 // Owner handle
    wc.hIcon = NULL,                          // Application icon
    wc.hCursor = NULL;                        // Default cursor
    wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wc.lpszMenuName =  NULL;                  // Menu name
    wc.lpszClassName = CLIENT_CLASS_NAME;          // Window class name

    if (RegisterClass (&wc) == 0) return 1;

    return 0;
}

//======================================================================
// Callback function for application window
LRESULT CALLBACK ClientWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
                                LPARAM lParam) {

    return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

//=======================================================================
//Instance initialization for client
HWND InitClientInstance (HWND hWnd,HINSTANCE hInstance)
{
 HWND hWndClient;
 hWndClient=CreateWindow (CLIENT_CLASS_NAME,
          CLIENT_TITLE,
                    WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_HSCROLL,
                    CW_USEDEFAULT,
            CW_USEDEFAULT,
                    CW_USEDEFAULT,
            CW_USEDEFAULT,
                    hWnd,
            (HMENU)IDC_CLIENT,
                    hInstance,
            NULL);

    // Destroy frame if client window not created.
    if (!IsWindow (hWndClient))
  {
   return 0;
  }
  return hWndClient;

posted @ 2006-07-25 20:17  我的一天  阅读(223)  评论(0编辑  收藏  举报