用SDK建立一個簡單帶捲軸的視窗

來源:互聯網
上載者:User

//=====================================================================================================
//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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.