最经典的SDK程序结构 HelloWin

程序运行效果:在创建窗口的时候,播放一个声音。且在窗口的客户区中央画一句文字:Hello, Windows 98!,无论程序怎么移动、最大化,文字始终在程序的中央部位。

程序总共分为六个步骤:定义,注册,创建,显示,刷新,消息循环。其中定义部分还包括一个窗口回调函数WndProc。

/*------------------------------------------------------------
   HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
                 (c) Charles Petzold, 1998
  ------------------------------------------------------------*/

#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 ;
     // 10项内容(注册窗口类使用,但生成窗口实例时仍需要class name,windows style和hInstance三项重复内容)
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;   // 重要属性:窗口函数(回调函数)
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ; // 重要属性:从main函数的参数里得到值
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ; // API,设置窗口默认小图标,比如IDI_QUESTION
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     // API,设置窗口鼠标,比如IDC_WAIT
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; // API,设置窗口背景色,比如BLACK_BRUSH
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ; // 重要属性:窗口类名称

     // 第二步,注册窗口类
     if (!RegisterClass (&wndclass)) // API
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), // API
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

     // 第三步,创建窗口类(11项),得到一个窗口句柄
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello Program"), // window caption
                          WS_OVERLAPPEDWINDOW,        // 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

     // 第四步,显示窗口
     ShowWindow (hwnd, iCmdShow) ; // API,使用句柄显示窗口(参数来自main函数的参数)
     // 第五步,刷新窗口
     UpdateWindow (hwnd) ; // API,使用句柄立刻刷新窗口

     // 第六步,消息循环
     while (GetMessage (&msg, NULL, 0, 0)) // API,就一个消息变量,不断取到消息放到里面准备处理
     {
          TranslateMessage (&msg) ; // API,翻译消息
          DispatchMessage (&msg) ;  // API,发送消息(消息里就包含了句柄)
     }
     // 最后一步,将最后一个消息的wParam参数返回给main函数当作函数结果
     return msg.wParam ;
}

// 注意,这是全局普通函数,不是类成员函数,否则会发生参数错误!!此外,参数列表里有Handle句柄,所以可处理不同窗口类的消息!!
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
 
     // 所有窗口共用一个窗口函数(但是一般情况下每个窗口会拥有一个不同的窗口函数)
     // 此处处理三条消息
     switch (message)
     {
     case WM_CREATE:
          PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ; // API
          return 0 ;
          
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ; // API,使用窗口句柄得到DC句柄,开始绘图过程
          
          GetClientRect (hwnd, &rect) ; // API,使用窗口句柄得到客户区
          
          DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; // API,使用DC句柄绘图
          
          EndPaint (hwnd, &ps) ; // API,使用窗口句柄,结束绘图过程
          return 0 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ; // API
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ; // API,需要四个参数:句柄,消息代号,参数w,参数l(注意不是消息结构),与回调函数的参数完全一致
}

参考:
http://blog.csdn.net/mousebaby808/article/details/5264961
http://download.csdn.net/download/bhsr12/8081133

posted @ 2014-09-14 05:58  findumars  Views(435)  Comments(0Edit  收藏  举报