1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164#include "Window.h"
#include "ge.h"
#include "Console.h"
#include "Event.h"
#include "Device.h"
GE_NAMESPACE;
map<HWND, Window*> Window::mapWnd;
int Window::wndCount = 0;
Window::Window() {
tmpWaitNotify = new Wait_Notify;
thMsg = new thread(&Window::thMsgFn, this);
tmpWaitNotify->wait();
SafeDelete(tmpWaitNotify);
}
Window::~Window() {
close();
thMsg->join();
SafeDelete(thMsg);
}
void Window::thMsgFn() {
threadId = GetCurrentThreadId();
className = _T("SGE") + std::to_wstring(wndCount);
//窗口设定
WNDCLASS wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = SGE::g_hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = className.c_str();
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
wndCount++;
//创建窗口
g_hWnd = CreateWindow(wc.lpszClassName,
_T("SGE"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, wc.hInstance, NULL);
//得到窗口画面的区域
RECT rect;
GetClientRect(g_hWnd, &rect);
clientSize = Size(rect.right - rect.left, rect.bottom - rect.top);
mapWnd[g_hWnd] = this;
tmpWaitNotify->notify();
//消息处理
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT) {
GetMessage(&msg, 0, 0, 0);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
SGE::mtxML.lock();
vector<Window*>& vSendQuitMsgHWnd = SGE::vSendQuitMsgHWnd;
vector<Window*>::iterator iter = std::find(vSendQuitMsgHWnd.begin(), vSendQuitMsgHWnd.end(), this);
if(iter != vSendQuitMsgHWnd.end())
vSendQuitMsgHWnd.erase(iter);
SGE::mtxML.unlock();
UnregisterClass(className.c_str(), SGE::g_hInstance);
wndCount--;
}
void Window::setWindowTitle(const wstring& title) {
wndTitle = title;
SetWindowText(g_hWnd, wndTitle.c_str());
}
std::wstring Window::getWindowTitle() const {
return wndTitle;
}
Rect Window::rect() const {
RECT rect;
GetWindowRect(g_hWnd, &rect);
return Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}
void Window::move(int x, int y, int w, int h) {
MoveWindow(g_hWnd, x, y, w, h, true);
}
void Window::move(const Rect& rect) {
MoveWindow(g_hWnd, rect.x, rect.y, rect.width, rect.height, true);
}
void Window::move(const Point& pos) {
RECT rect;
GetWindowRect(g_hWnd, &rect);
MoveWindow(g_hWnd, pos.x, pos.y, rect.right - rect.left, rect.bottom - rect.top, true);
}
void Window::move(const Size& size) {
RECT rect;
GetWindowRect(g_hWnd, &rect);
MoveWindow(g_hWnd, rect.left, rect.top, size.width, size.height, true);
}
void Window::show(int nCmdShow) {
ShowWindow(g_hWnd, nCmdShow);
}
bool Window::isVisible() const {
return IsWindowVisible(g_hWnd);
}
void Window::close() {
SendMessage(g_hWnd, WM_CLOSE, 0, 0);
}
void Window::update() {
SGE::postEvent(this, (EventFunc)&Window::paintEvent, new PaintEvent, true);
}
LRESULT CALLBACK Window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
auto iter = mapWnd.find(hWnd);
if (iter == mapWnd.end())
return DefWindowProc(hWnd, uMsg, wParam, lParam);
return (*iter).second->procWndMessage(hWnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK Window::procWndMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_SIZE:
//得到窗口画面的区域
RECT rect;
GetClientRect(g_hWnd, &rect);
clientSize = Size(rect.right - rect.left, rect.bottom - rect.top);
SGE::mtxML.lock();
SGE::postEvent(this, (EventFunc)&Window::resizeEvent, new ResizeEvent(clientSize), true);
SGE::mtxML.unlock();
break;
case WM_CLOSE:
//销毁窗口
setClosed(true);
DestroyWindow(hWnd);
SGE::mtxML.lock();
mapWnd.erase(g_hWnd);
SGE::mtxML.unlock();
break;
case WM_DESTROY:
//退出消息循环
SGE::mtxML.lock();
SGE::vSendQuitMsgHWnd.push_back(this);
SGE::mtxML.unlock();
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}