Windows Mobile 编程 (Win32) - Hello, Window.

《Windows 程序设计》第三章主要介绍窗口和消息。详细的说明了Windows程序的工作流程和原理,并给出了一个基本的程序结构(HelloWin)。实际上,每个Windowss程序都包括HelloWin程序的大部分。没人能真正记住编写此代码的全部语法,通常,在开始新的Windows程序编写时总是复制一个现有的程序,然后再做相应的修改。下面是HelloWin的全部,对应Windows Mobile平台,作了相应修改。

#include <windows.h>

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND hwnd;
    MSG msg;
    WNDCLASS wc;

    // Set up the window class description
    ZeroMemory(&wc, sizeof(wc));
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszClassName = szAppName;

    // We want to redraw the window contents anytime we get resized. That way
    // we'll respond appropriately when the user switches between portrait and
    // landscape. If we had any child windows or controls, we'd need to
    // reposition or resize them when we get a WM_SIZE message.
    wc.style = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClass(&wc))
    {
        MessageBox(NULL, TEXT("RegisterClass() failed!"), szAppName, MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(szAppName,  // window class name
        TEXT("The Hello Program"),  // window caption
        WS_OVERLAPPED | WS_SYSMENU, // window style
        CW_USEDEFAULT,              // initial x position
        CW_USEDEFAULT,              // initial y position
        CW_USEDEFAULT,              // initial x size
        CW_USEDEFAULT,              // initial y size
        NULL,                       // parent window handle
        NULL,                       // window menu handle
        hInstance,                  // program instance handle
        NULL);                      // creation parameters

    if (hwnd == NULL)
    {
        MessageBox(NULL, TEXT("CreateWindow() failed!"), szAppName, MB_ICONERROR);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd) ;

    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc ;
    PAINTSTRUCT ps ;
    RECT rect ;

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);

        GetClientRect(hwnd, &rect);

        DrawText(hdc, TEXT("Hello, Windows Mobile!"), -1, &rect,
            DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

        EndPaint(hwnd, &ps);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
}
posted @ 2009-03-11 10:35  可乐罐  阅读(745)  评论(0编辑  收藏  举报