Riordon

  博客园 :: 首页 :: 新随笔 :: :: :: 管理 ::

Windows窗口

建立Windows窗口的步奏:

(1)注册窗口类

(2)创建窗口

(3)显示并更新

(4)消息消息循环

(5)窗口过程的处理

 1 #include <tchar.h>
 2 #include <windows.h>
 3 
 4 LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 5 
 6 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nShowCmd )
 7 {
 8     WNDCLASSEX wcex;
 9     wcex.cbClsExtra = 0;
10     wcex.cbSize = sizeof(WNDCLASSEX);
11     wcex.cbWndExtra = 0;
12     wcex.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
13     wcex.hCursor = ::LoadCursor(NULL,IDC_ARROW);
14     wcex.hIcon = ::LoadIcon(NULL,IDI_APPLICATION);
15     wcex.hIconSm = ::LoadIcon(NULL,IDI_APPLICATION);
16     wcex.hInstance = hInstance;
17     wcex.lpfnWndProc = WinProc;
18     wcex.lpszClassName = _T("WinApp");
19     wcex.lpszMenuName = NULL;
20     wcex.lpszMenuName = NULL;
21     wcex.style = CS_HREDRAW | CS_VREDRAW;
22 
23     if (!::RegisterClassEx(&wcex))
24     {
25         MessageBox(NULL, _T("注册窗口类失败!"), _T("注册窗口"),MB_OK);
26         return FALSE;
27     }
28 
29     HWND hWnd = ::CreateWindowEx(0,_T("WinApp"),_T("WinTest"),WS_VISIBLE | WS_OVERLAPPEDWINDOW,
30                                CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
31                                NULL,NULL,hInstance,NULL);
32 
33     ::ShowWindow(hWnd,SW_SHOW);
34     ::UpdateWindow(hWnd);
35 
36     MSG msg;
37     while (GetMessage(&msg,NULL,0,0))
38     {
39         ::TranslateMessage(&msg);
40         ::DispatchMessage(&msg);
41     }
42 
43     return 0;
44 }
45  
46 
47 LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
48 {
49     switch (uMsg)
50     {
51     case WM_CLOSE:
52         ::DestroyWindow(hWnd);
53         break;
54     case WM_DESTROY:
55         ::PostQuitMessage(WM_QUIT);
56         break;
57     case WM_CHAR:
58         MessageBox(NULL,_T("已经触发了"),_T("Message"),MB_OK);
59         break;
60     default:
61         return DefWindowProc(hWnd,uMsg,wParam,lParam);
62     }
63 
64     return 0;
65 }

 

posted on 2013-07-29 23:41  Riordon  阅读(272)  评论(0)    收藏  举报