天天看點

用SDK建立一個簡單的視窗

//=====================================================================================================

//TITLE:

//  用SDK建立一個簡單的視窗

//AUTHOR:

//  norains

//DATE:

//  Friday 7-April-2006

//=====================================================================================================

  在EVC編譯環境下,不使用MFC架構建立一個極其簡單的視窗----甚至連關閉按鈕都沒有,隻有最簡單的消息循環.

  此代碼分為兩個檔案,分别是:HelloWindow.h和HelloWindow.cpp;代碼根據《WindowCE程式設計》一書的第一個代碼例子進行精簡。

//----------------------------------------------------------------------

// Function prototypes

// Returns number of elements

int InitApp (HINSTANCE);

HWND InitInstance (HINSTANCE, LPWSTR, int);

int TermInstance (HINSTANCE, int);

// Window procedures

LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);

// Message handlers

LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);

#include "stdafx.h"

#include "HelloWindow.h"

//----------------------------------------------------------------------

// Global data

//

const TCHAR szAppName[] = TEXT ("HelloCE");

HINSTANCE hInst;                     // Program instance handle

int WINAPI WinMain( HINSTANCE hInstance,

     HINSTANCE hPrevInstance,

     LPTSTR    lpCmdLine,

     int       nCmdShow)

{

  // TODO: Place code here.

  MSG msg;

    int rc = 0;

    HWND hwndMain;

    // init application

    rc = InitApp (hInstance);

    if (rc) return rc;

    // Initialize this instance.

    hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);

    if (hwndMain == 0)

        return 0x10;

    // Application message loop

    while (GetMessage (&msg, NULL, 0, 0)) {

        TranslateMessage (&msg);

        DispatchMessage (&msg);

    }

    // Instance cleanup

    return TermInstance (hInstance, msg.wParam);

 return 0;

}

//----------------------------------------------------------------------

// InitApp - Application initialization

//

int InitApp (HINSTANCE hInstance) {

    WNDCLASS wc;

    // Register application main window class.

    wc.style = 0;                             // Window style

    wc.lpfnWndProc = MainWndProc;             // 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 = szAppName;             // Window class name

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

    return 0;

}

//----------------------------------------------------------------------

// InitInstance - Instance initialization

//

HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine,

                   int nCmdShow) {

    HWND hWnd;

    // Save program instance handle in global variable.

    hInst = hInstance;

    // Create main window.

    hWnd = CreateWindow (szAppName,           // Window class

                         TEXT("Hello"),       // 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 TermInstance (HINSTANCE hInstance, int nDefRC) {

    return nDefRC;

}

//----------------------------------------------------------------------

// MainWndProc - Callback function for application window

//

LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,

                              LPARAM lParam)

{

    // Search message list to see if we need to handle this

    // message.  If in list, call procedure.

  switch(wMsg)

  {

 case WM_DESTROY:

  return DoDestroyMain(hWnd,wMsg,wParam,lParam);

 default:

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

  }

}

//----------------------------------------------------------------------

// DoDestroyMain - Process WM_DESTROY message for window.

//

LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,

                       LPARAM lParam) {

    PostQuitMessage (0);

    return 0;

}

繼續閱讀