一个程序二个窗口
1 #include "stdafx.h"
2 #include <map>
3 #include "MultiWindow.h"
4 #define MAX_LOADSTRING 100
5 using namespace std;
6 // 全局变量:
7 HINSTANCE hInst; // 当前实例
8 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
9 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
10
11 // 此代码模块中包含的函数的前向声明:
12 ATOM MyRegisterClass(HINSTANCE hInstance);
13 BOOL InitInstance(HINSTANCE, int);
14 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
15 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
16 using namespace std;
17
18 class CMyWnd;
19 typedef map<HWND,CMyWnd*> MAPHWND2WND;
20 MAPHWND2WND mapHWND2Wnd;
21
22 CMyWnd* tmpMyWnd = NULL;
23
24 class CMyWnd
25 {
26 public:
27 static LRESULT CALLBACK WndProc(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam);
28
29 LRESULT WindowProc(UINT nMessage,WPARAM wParam,LPARAM lParam);
30
31
32 CMyWnd()
33 {
34 m_hWnd = NULL;
35 }
36 bool Create()
37 {
38 tmpMyWnd = this;
39
40 m_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
41 0, 0, 100,100, NULL, NULL, hInst, NULL);
42
43 if (!m_hWnd)
44 return false;
45
46
47 return true;
48 }
49
50 BOOL ShowWindow(int nCmdShow)
51 {
52 return ::ShowWindow(m_hWnd,nCmdShow);
53 }
54
55 BOOL UpdataWindow()
56 {
57 return ::UpdateWindow(m_hWnd);
58 }
59
60 virtual void OnPaint()
61 {
62 PAINTSTRUCT ps;
63 HDC hdc = BeginPaint(m_hWnd, &ps);
64 ::TextOut(hdc,0,0,"123456",6);
65 EndPaint(m_hWnd, &ps);
66
67 }
68
69 HWND m_hWnd;
70 };
71
72 class CMyWnd2 : public CMyWnd
73 {
74 public:
75 virtual void OnPaint()
76 {
77 PAINTSTRUCT ps;
78 HDC hdc = BeginPaint(m_hWnd, &ps);
79 ::TextOut(hdc,0,0,"6789",4);
80 EndPaint(m_hWnd, &ps);
81 }
82
83 string m_str;
84 };
85
86 class CMyWnd3 : public CMyWnd
87 {
88 public:
89 virtual void OnPaint()
90 {
91 PAINTSTRUCT ps;
92 HDC hdc = BeginPaint(m_hWnd, &ps);
93 ::TextOut(hdc,0,0,"000000",6);
94 EndPaint(m_hWnd, &ps);
95 }
96
97 string m_str;
98 };
99
100 CMyWnd* findCMyWnd(HWND hWnd)
101 {
102 MAPHWND2WND::iterator it = mapHWND2Wnd.find(hWnd);
103 if (it != mapHWND2Wnd.end())
104 {
105 return it->second;
106 }
107 else
108 {
109 mapHWND2Wnd.insert(make_pair(hWnd,tmpMyWnd));
110
111 tmpMyWnd->m_hWnd = hWnd;
112 return tmpMyWnd;
113 //DebugBreak();
114 }
115 return NULL;
116 }
117
118 LRESULT CALLBACK CMyWnd::WndProc(HWND hWnd,UINT nMessage,WPARAM wParam,LPARAM lParam)
119 {
120 CMyWnd* pWnd = findCMyWnd(hWnd);
121 if (pWnd != NULL)
122 {
123 return pWnd->WindowProc( nMessage, wParam, lParam);
124 }
125 else
126 {
127 DebugBreak();
128 }
129 return 0;
130 }
131
132 LRESULT CMyWnd::WindowProc(UINT nMessage,WPARAM wParam,LPARAM lParam)
133 {
134 if (nMessage == WM_PAINT)
135 {
136 OnPaint();
137 return 0;
138 }
139 return ::DefWindowProc(m_hWnd, nMessage, wParam, lParam);;
140 }
141
142
143 int APIENTRY _tWinMain(HINSTANCE hInstance,
144 HINSTANCE hPrevInstance,
145 LPTSTR lpCmdLine,
146 int nCmdShow)
147 {
148 // TODO: 在此放置代码。
149 MSG msg;
150 HACCEL hAccelTable;
151
152 // 初始化全局字符串
153 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
154 LoadString(hInstance, IDC_MULTIWINDOW, szWindowClass, MAX_LOADSTRING);
155 MyRegisterClass(hInstance);
156
157 // 执行应用程序初始化:
158 if (!InitInstance (hInstance, nCmdShow))
159 {
160 return FALSE;
161 }
162
163 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MULTIWINDOW);
164
165 // 主消息循环:
166 while (GetMessage(&msg, NULL, 0, 0))
167 {
168 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
169 {
170 TranslateMessage(&msg);
171 DispatchMessage(&msg);
172 }
173 }
174
175 return (int) msg.wParam;
176 }
177
178
179
180 //
181 // 函数: MyRegisterClass()
182 //
183 // 目的: 注册窗口类。
184 //
185 // 注释:
186 //
187 // 仅当希望在已添加到 Windows 95 的
188 // “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
189 // 才需要此函数及其用法。调用此函数
190 // 十分重要,这样应用程序就可以获得关联的
191 // “格式正确的”小图标。
192 //
193 ATOM MyRegisterClass(HINSTANCE hInstance)
194 {
195 WNDCLASSEX wcex;
196
197 wcex.cbSize = sizeof(WNDCLASSEX);
198
199 wcex.style = CS_HREDRAW | CS_VREDRAW;
200 wcex.lpfnWndProc = CMyWnd::WndProc;
201 wcex.cbClsExtra = 0;
202 wcex.cbWndExtra = 0;
203 wcex.hInstance = hInstance;
204 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MULTIWINDOW);
205 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
206 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
207 wcex.lpszMenuName = (LPCTSTR)IDC_MULTIWINDOW;
208 wcex.lpszClassName = szWindowClass;
209 wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
210
211 return RegisterClassEx(&wcex);
212 }
213
214 //
215 // 函数: InitInstance(HANDLE, int)
216 //
217 // 目的: 保存实例句柄并创建主窗口
218 //
219 // 注释:
220 //
221 // 在此函数中,我们在全局变量中保存实例句柄并
222 // 创建和显示主程序窗口。
223 //
224 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
225 {
226
227
228 hInst = hInstance; // 将实例句柄存储在全局变量中
229
230
231 CMyWnd* pWnd = new CMyWnd3();
232 if(!pWnd->Create())
233 return false;
234 pWnd->ShowWindow(nCmdShow);
235 pWnd->UpdataWindow();
236
237 pWnd = new CMyWnd2();
238
239 if (!pWnd->Create())
240 return FALSE;
241 pWnd->ShowWindow(nCmdShow);
242 pWnd->UpdataWindow();
243
244 return TRUE;
245 }