[code]HELLOWIN

#include <windows.h>



//窗口过程函数

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



//程序入口函数

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

                   PSTR szCmdLine, int iCmdShow)

{

    static TCHAR szAppName[] = TEXT("HelloWin");

    HWND hWnd;    //窗口句柄

    MSG msg;    //消息结构

    WNDCLASS wndclass;    //窗口类



    wndclass.style = CS_HREDRAW | CS_VREDRAW;    //水平和垂直重画

    wndclass.lpfnWndProc = WndProc;    //窗口过程

    wndclass.cbClsExtra = 0;

    wndclass.cbWndExtra = 0;

    wndclass.hInstance = hInstance;

    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);    //初始化为白色画刷

    wndclass.lpszMenuName = NULL;

    wndclass.lpszClassName = szAppName;    //窗口类名



    //注册窗口类

    if(!RegisterClass(&wndclass))

    {    //注册窗口类失败

        MessageBox(NULL, TEXT("This program requires Windows NT!"),

            szAppName, MB_ICONERROR);

        return 0;

    }



    //创建窗口,此时会发生WM_CREATE消息

    hWnd = CreateWindow(szAppName,    //窗口类名

        TEXT("The Hello Program"),    //窗口标题

        WS_OVERLAPPEDWINDOW,    //标准窗口风格

        CW_USEDEFAULT,    //默认x坐标

        CW_USEDEFAULT,    //默认y坐标

        CW_USEDEFAULT,    //默认宽度

        CW_USEDEFAULT,    //默认高度

        NULL,    //父窗口句柄

        NULL,    //菜单句柄

        hInstance,    //程序实例句柄

        NULL);    //传递给窗口过程函数的数据指针



    //显示窗口,此时会发生WM_PAINT消息

    ShowWindow(hWnd, iCmdShow);

    //更新窗口

    UpdateWindow(hWnd);

    //消息循环

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

    {

        //转换消息,如键盘消息转换为WM_CAHR消息

        TranslateMessage(&msg);

        //把消息发送给Windows

        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_CREATE:

        return 0;

    case WM_PAINT:

        hdc = BeginPaint(hWnd, &ps);

        //获取窗口客户区区域

        GetClientRect(hWnd, &rect);

        //绘制文本

        DrawText(hdc, TEXT("Hello, Windows 98!"), -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);

}

 

你可能感兴趣的:(code)