Win32应用中创建多窗口

Win32应用中创建多窗口,创建同一个窗口类的多个窗口对象和不同窗口类的窗口对象。

 

  1 // win32_多窗口.cpp : Defines the entry point for the application.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "resource.h"
  6 
  7 #define MAX_LOADSTRING 100
  8 
  9 // Global Variables:
 10 HINSTANCE hInst;                                // current instance
 11 TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
 12 TCHAR szTitle2[MAX_LOADSTRING] = {"同一个窗口类的第二个窗口"};
 13 TCHAR szTitle3[MAX_LOADSTRING] = {"不同窗口类的窗口"};
 14 TCHAR szWindowClass[MAX_LOADSTRING];            // The title bar text
 15 TCHAR szWindowClass2[MAX_LOADSTRING] = {"Submarine"};    // 第二个窗口类的类名
 16 HWND hWnd, hWnd2, hWnd3;                        // 保存窗口句柄
 17 
 18 // Foward declarations of functions included in this code module:
 19 ATOM                MyRegisterClass(HINSTANCE hInstance);
 20 BOOL                InitInstance(HINSTANCE, int);
 21 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
 22 LRESULT CALLBACK    WndProc2(HWND, UINT, WPARAM, LPARAM);
 23 LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
 24 
 25 int APIENTRY WinMain(HINSTANCE hInstance,
 26                      HINSTANCE hPrevInstance,
 27                      LPSTR     lpCmdLine,
 28                      int       nCmdShow)
 29 {
 30      // TODO: Place code here.
 31     MSG msg;
 32     HACCEL hAccelTable;
 33 
 34     // Initialize global strings
 35     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 36     LoadString(hInstance, IDC_WIN32_, szWindowClass, MAX_LOADSTRING);
 37     MyRegisterClass(hInstance);
 38 
 39     // Perform application initialization:
 40     if (!InitInstance (hInstance, nCmdShow)) 
 41     {
 42         return FALSE;
 43     }
 44 
 45     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32_);
 46 
 47     // Main message loop:
 48     while (GetMessage(&msg, NULL, 0, 0)) 
 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 ATOM MyRegisterClass(HINSTANCE hInstance)
 76 {
 77     WNDCLASSEX wcex, wcex2;
 78 
 79     // 第一个窗口类的定义
 80     wcex.cbSize = sizeof(WNDCLASSEX); 
 81 
 82     wcex.style            = CS_HREDRAW | CS_VREDRAW;
 83     wcex.lpfnWndProc    = (WNDPROC)WndProc;
 84     wcex.cbClsExtra        = 0;
 85     wcex.cbWndExtra        = 0;
 86     wcex.hInstance        = hInstance;
 87     wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32_);
 88     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
 89     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
 90     wcex.lpszMenuName    = (LPCSTR)IDC_WIN32_;
 91     wcex.lpszClassName    = szWindowClass;
 92     wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
 93 
 94     // 第二个窗口类的定义
 95     wcex2.cbSize = sizeof(WNDCLASSEX); 
 96 
 97     wcex2.style            = CS_HREDRAW | CS_VREDRAW;
 98     wcex2.lpfnWndProc    = (WNDPROC)WndProc2;
 99     wcex2.cbClsExtra    = 0;
100     wcex2.cbWndExtra    = 0;
101     wcex2.hInstance        = hInstance;
102     wcex2.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32_);
103     wcex2.hCursor        = LoadCursor(NULL, IDC_ARROW);
104     wcex2.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
105     wcex2.lpszMenuName    = (LPCSTR)IDC_WIN32_;
106     wcex2.lpszClassName    = szWindowClass2;
107     wcex2.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
108 
109     return (RegisterClassEx(&wcex) && RegisterClassEx(&wcex2));
110 }
111 
112 //
113 //   FUNCTION: InitInstance(HANDLE, int)
114 //
115 //   PURPOSE: Saves instance handle and creates main window
116 //
117 //   COMMENTS:
118 //
119 //        In this function, we save the instance handle in a global variable and
120 //        create and display the main program window.
121 //
122 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
123 {
124 //   HWND hWnd;
125 
126    hInst = hInstance; // Store instance handle in our global variable
127 
128    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
129       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
130    hWnd2 = CreateWindow(szWindowClass, szTitle2, WS_OVERLAPPEDWINDOW,
131       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
132    hWnd3 = CreateWindow(szWindowClass2, szTitle3, WS_OVERLAPPEDWINDOW,
133       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
134 
135    if (!hWnd || !hWnd2 || !hWnd3)
136    {
137       return FALSE;
138    }
139 
140    ShowWindow(hWnd, nCmdShow);
141    UpdateWindow(hWnd);
142    ShowWindow(hWnd2, nCmdShow);
143    UpdateWindow(hWnd2);
144    ShowWindow(hWnd3, nCmdShow);
145    UpdateWindow(hWnd3);
146 
147    return TRUE;
148 }
149 
150 //
151 //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
152 //
153 //  PURPOSE:  Processes messages for the main window.
154 //
155 //  WM_COMMAND    - process the application menu
156 //  WM_PAINT    - Paint the main window
157 //  WM_DESTROY    - post a quit message and return
158 //
159 //
160 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
161 {
162     int wmId, wmEvent;
163     PAINTSTRUCT ps;
164     HDC hdc;
165 
166     switch (message) 
167     {
168         case WM_COMMAND:
169             wmId    = LOWORD(wParam); 
170             wmEvent = HIWORD(wParam); 
171             // Parse the menu selections:
172             switch (wmId)
173             {
174                 case IDM_ABOUT:
175                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
176                    break;
177                 case IDM_EXIT:
178                    DestroyWindow(hWnd);
179                    break;
180                 default:
181                    return DefWindowProc(hWnd, message, wParam, lParam);
182             }
183             break;
184         case WM_PAINT:
185             hdc = BeginPaint(hWnd, &ps);
186             // TODO: Add any drawing code here...
187 //            RECT rt;
188 //            GetClientRect(hWnd, &rt);
189             EndPaint(hWnd, &ps);
190             break;
191         case WM_DESTROY:
192             PostQuitMessage(0);
193             break;
194         default:
195             return DefWindowProc(hWnd, message, wParam, lParam);
196    }
197    return 0;
198 }
199 
200 LRESULT CALLBACK WndProc2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
201 {
202     int wmId, wmEvent;
203     PAINTSTRUCT ps;
204     HDC hdc;
205 
206     switch (message) 
207     {
208         case WM_COMMAND:
209             wmId    = LOWORD(wParam); 
210             wmEvent = HIWORD(wParam); 
211             // Parse the menu selections:
212             switch (wmId)
213             {
214                 case IDM_ABOUT:
215                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
216                    break;
217                 case IDM_EXIT:
218                    DestroyWindow(hWnd);
219                    break;
220                 default:
221                    return DefWindowProc(hWnd, message, wParam, lParam);
222             }
223             break;
224         case WM_PAINT:
225             hdc = BeginPaint(hWnd, &ps);
226             // TODO: Add any drawing code here...
227 //            RECT rt;
228 //            GetClientRect(hWnd, &rt);
229             EndPaint(hWnd, &ps);
230             break;
231         case WM_DESTROY:
232             MessageBox(hWnd, "不是同一个窗口类", "Note", MB_OK);
233 //            PostQuitMessage(0);        // 此处未调用该函数,应用的消息队列中也就未添加WM_QUIT消息,
234                                     // 在WinMain函数的消息循环中继续等待用户或系统消息,
235                                     // 消息循环没结束,整个主函数WinMan也就没结束。
236             break;
237         default:
238             return DefWindowProc(hWnd, message, wParam, lParam);
239    }
240    return 0;
241 }
242 
243 // Mesage handler for about box.
244 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
245 {
246     switch (message)
247     {
248         case WM_INITDIALOG:
249                 return TRUE;
250 
251         case WM_COMMAND:
252             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
253             {
254                 EndDialog(hDlg, LOWORD(wParam));
255                 return TRUE;
256             }
257             break;
258     }
259     return FALSE;
260 }

 

  。

转载于:https://www.cnblogs.com/submarinex/archive/2013/01/03/2843325.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值