• Windows程序设计(Charles Petzold)HELLOWIN程序实现


     1 /*--------------------------------------------------------------
     2     HELLOWIN.C--DisPlays "Hello, Windows!" in client area
     3                 (c) Charles Petzold, 1998
     4 --------------------------------------------------------------*/
     5 #include <windows.h>
     6 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
     7 
     8 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreVInstance,
     9                    PSTR szCmdLine, int iCmdShow)
    10 {
    11     static TCHAR szAppName[] = TEXT("HelloWin");
    12     HWND         hwnd;
    13     MSG            msg;
    14     WNDCLASS     wndclass;
    15     
    16     wndclass.style            = CS_HREDRAW | CS_VREDRAW;
    17     wndclass.lpfnWndProc    = WndProc;
    18     wndclass.cbClsExtra        = 0;
    19     wndclass.cbWndExtra        = 0;
    20     wndclass.hInstance        = hInstance;
    21     wndclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    22     wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    23     wndclass.hbrBackground    = (HBRUSH) GetStockObject(WHITE_BRUSH);
    24     wndclass.lpszMenuName    = NULL;
    25     wndclass.lpszClassName    = szAppName;
    26     
    27     if (!RegisterClass(&wndclass))
    28     {
    29         MessageBox(NULL, TEXT("This progam requires Windows NT!"),
    30                     szAppName, MB_ICONERROR);
    31         return 0;
    32     }
    33     
    34     hwnd = CreateWindow(szAppName,                    //window class name
    35                         TEXT("The Hello Program"),    //window caption
    36                         WS_OVERLAPPEDWINDOW,        //window style
    37                         CW_USEDEFAULT,                //initial x position
    38                         CW_USEDEFAULT,                //initial y position
    39                         CW_USEDEFAULT,                //initial x size
    40                         CW_USEDEFAULT,                //initial y size
    41                         NULL,                        //parent window handle
    42                         NULL,                        //window menu handle
    43                         hInstance,                    //program instance
    44                         NULL                        //creation parameters
    45                         );
    46     ShowWindow(hwnd, iCmdShow);
    47     UpdateWindow(hwnd);
    48     
    49     while (GetMessage(&msg, NULL, 0, 0))
    50     {
    51         TranslateMessage(&msg);
    52         DispatchMessage(&msg);
    53     }
    54     return msg.wParam;
    55 }
    56 
    57 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    58 {
    59     HDC            hdc;
    60     PAINTSTRUCT    ps;
    61     RECT        rect;
    62     
    63     switch (message)
    64     {
    65         case WM_CREATE:
    66             //PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
    67             return 0;
    68             
    69         case WM_PAINT:
    70             hdc = BeginPaint(hwnd, &ps);
    71             GetClientRect(hwnd, &rect);
    72             DrawText(hdc, TEXT("Hello, windows!"), -1, &rect,
    73                     DT_SINGLELINE | DT_CENTER| DT_VCENTER);
    74             EndPaint(hwnd, &ps);
    75             return 0;
    76             
    77         case WM_DESTROY:
    78             PostQuitMessage(0);
    79             return 0;
    80     }
    81     return DefWindowProc(hwnd, message, wParam, lParam);
    HELLOWIN.c

    vs新建一空的win32应用程序,将已经编辑好的代码文件添加至源文件中。在工程属性->附加依赖项中添加"winmm.lib"(或将PlaySound()函数注释掉),编译运行,查看结果。

  • 相关阅读:
    应对需求变更的软件的设计——我的想法
    重温数据库访问——故事篇
    用接口实现事件的一种方法,只是玩玩。
    面向对象最重要的是“抽象”,三层最重要的也是“抽象”,没有抽象就不是真正的面向对象、三层。
    【视频】自然框架之分页控件的使用方法(一) PostBack方式的一般分页方式
    【自然框架 免费视频】资源角色的思路介绍(整理了一下以前帖子的目录,请刷新)
    自信。
    钢铁是怎样炼成的?千锤百炼
    Android 中文API (94) —— MediaController
    android中文api(80)——Gallery.LayoutParams
  • 原文地址:https://www.cnblogs.com/web1013/p/5359967.html
Copyright © 2020-2023  润新知