金鳞

目标->软件测试架构师

 

  1// win32.cpp : Defines the entry point for the application.    
  2//    
  3   
  4#include "stdafx.h"//包含有windows.h头文件    
  5#include "resource.h"//资源头文件,包含有各个资源的ID号。    
  6   
  7#define MAX_LOADSTRING 100    
  8   
  9// Global Variables:    
 10HINSTANCE hInst;                        // current instance    
 11TCHAR szTitle[MAX_LOADSTRING];          // The title bar text    
 12TCHAR szWindowClass[MAX_LOADSTRING];    // The title bar text    
 13   
 14// Foward declarations of functions included in this code module:    
 15   
 16BOOL                MyRegisterClass(HINSTANCE hInstance);//注册窗口类    
 17BOOL                InitInstance(HINSTANCE, int);        //初始化程序    
 18LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM); //主窗口函数    
 19LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);   //两个都是CALLBACK(回调)函数    
 20   
 21//----------------------------------------------------------------------    
 22//程序的入口点WinMain函数    
 23//----------------------------------------------------------------------    
 24int CALLBACK WinMain(HINSTANCE hInstance,   
 25                     HINSTANCE hPrevInstance,   
 26                     LPSTR     lpCmdLine,   
 27                     int       nCmdShow)   
 28{   
 29    // TODO: Place code here.    
 30    MSG msg;                //Windows的消息结构    
 31    HACCEL hAccelTable;     //加速键表句柄(handle);    
 32   
 33    // Initialize global strings    
 34    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);   
 35    LoadString(hInstance, IDC_WIN32, szWindowClass, MAX_LOADSTRING);   
 36    MyRegisterClass(hInstance);//注册窗口类    
 37   
 38    // Perform application initialization:    
 39    if (!InitInstance (hInstance, nCmdShow)) //实例的初始化    
 40    {   
 41        return FALSE;   
 42    }
   
 43   
 44    //装入加速键表。    
 45    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32);   
 46   
 47    // Main message loop:    
 48    while (GetMessage(&msg, NULL, 00)) //从消息队列中获取消息    
 49    {   
 50//      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) //解释快捷键    
 51        {   
 52            TranslateMessage(&msg);//解释消息    
 53            DispatchMessage(&msg);//分派消息(到窗口函数中);    
 54        }
   
 55    }
   
 56   
 57    return msg.wParam;   
 58}
   
 59   
 60   
 61   
 62//    
 63//  FUNCTION: MyRegisterClass()    
 64//    
 65//  PURPOSE: Registers the window class.    
 66//    
 67//  COMMENTS:    
 68//    
 69//    This function and its usage is only necessary if you want this code    
 70//    to be compatible with Win32 systems prior to the 'RegisterClassEx'    
 71//    function that was added to Windows 95. It is important to call this function    
 72//    so that the application will get 'well formed' small icons associated    
 73//    with it.    
 74//    
 75   
 76//----------------------------------------------------------------------    
 77//注册窗口类,在产生窗口之前必须注册窗口类。    
 78//如同开一个公司之前,先要登记注册一样。    
 79//----------------------------------------------------------------------    
 80BOOL MyRegisterClass(HINSTANCE hInstance)   
 81{   
 82    WNDCLASSEX wcex;   
 83   
 84    wcex.cbSize = sizeof(WNDCLASSEX);    
 85   
 86    wcex.style          = CS_HREDRAW | CS_VREDRAW;  //注册窗口的风格。    
 87    wcex.lpfnWndProc    = (WNDPROC)WndProc;         //注册窗口函数    
 88    wcex.cbClsExtra     = 0;                        //为窗口类额外分配的字节(一般为0)    
 89    wcex.cbWndExtra     = 0;                        //为窗口实例额外分配的字节(一般为0)    
 90    wcex.hInstance      = hInstance;                //实例句柄    
 91    wcex.hIcon          = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32);//大图标    
 92    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);//光标    
 93    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);//窗口背景颜色    
 94    wcex.lpszMenuName   = (LPCSTR)IDC_WIN32;//窗口的菜单名称(与类名一致)    
 95    wcex.lpszClassName  = szWindowClass;//窗口类名称。    
 96    wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);//小图标    
 97   
 98    return RegisterClassEx(&wcex);   
 99}
   
100   
101//    
102//   FUNCTION: InitInstance(HANDLE, int)    
103//    
104//   PURPOSE: Saves instance handle and creates main window    
105//    
106//   COMMENTS:    
107//    
108//        In this function, we save the instance handle in a global variable and    
109//        create and display the main program window.    
110//    
111//----------------------------------------------------------------------------    
112//此函数用来产生窗口。    
113//----------------------------------------------------------------------    
114BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)   
115   
142   
143//    
144//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)    
145//    
146//  PURPOSE:  Processes messages for the main window.    
147//    
148//  WM_COMMAND  - process the application menu    
149//  WM_PAINT    - Paint the main window    
150//  WM_DESTROY  - post a quit message and return    
151//    
152//    
153//----------------------------------------------------------------------    
154//主窗口函数(用来处理消息)    
155//----------------------------------------------------------------------    
156LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)   
157{   
158    int wmId, wmEvent;   
159    PAINTSTRUCT ps;   
160    HDC hdc;   
161    TCHAR szHello[MAX_LOADSTRING];   
162    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);   
163   
164    switch (message)    
165    {   
166        case WM_COMMAND:   
167            wmId    = LOWORD(wParam);    
168            wmEvent = HIWORD(wParam);    
169            // Parse the menu selections:    
170            switch (wmId)   
171            {   
172                case IDM_ABOUT:   
173                   DialogBox(hInst,//所属实例    
174                            (LPCTSTR)IDD_ABOUTBOX, //对话框资源名称。    
175                            hWnd,//父窗口    
176                            (DLGPROC)About);//对话框的窗口函数名称。    
177                   break;   
178                case IDM_EXIT:   
179                   DestroyWindow(hWnd);//销毁窗口。    
180                   break;   
181                default:   
182                    //注意:不论什么消息,都必须被处理。    
183                    //未被处理的消息由Windows系统缺省的窗口函数调用。    
184                   return DefWindowProc(hWnd, message, wParam, lParam);   
185            }
   
186            break;   
187        case WM_PAINT://重画    
188            hdc = BeginPaint(hWnd, &ps);   
189            // TODO: Add any drawing code here    
190            RECT rt;   
191            GetClientRect(hWnd, &rt);   
192            DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);   
193            EndPaint(hWnd, &ps);   
194            break;   
195        case WM_DESTROY://窗口已经被销毁,程序即将结束。    
196            PostQuitMessage(0);   
197            break;   
198        default:   
199            //未被处理的消息由Windows系统缺省的窗口函数调用。    
200            return DefWindowProc(hWnd, message, wParam, lParam);   
201   }
   
202   return 0;   
203}
   
204   
205// Mesage handler for about box.    
206//----------------------------------------------------------------------    
207//处理对话框消息的窗口函数。    
208//----------------------------------------------------------------------    
209LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)   
210{   
211    switch (message)   
212    {   
213        case WM_INITDIALOG:   
214                return TRUE;   
215   
216        case WM_COMMAND:   
217            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)    
218            {   
219                EndDialog(hDlg, LOWORD(wParam));   
220                return TRUE;   
221            }
   
222            break;   
223    }
   
224    return FALSE;//FALSE表示没有处理这个消息。    
225}
  

原文:http://read.pudn.com/downloads95/sourcecode/windows/388297/02%E7%AB%A0/win32/win32.cpp__.htm
posted on 2009-03-12 10:16 金鳞 阅读(333) 评论(0)  编辑 收藏 引用 所属分类: C
只有注册用户登录后才能发表评论。