forked from vsite-nwp/v05
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nwpwin.cpp
92 lines (79 loc) · 2.23 KB
/
nwpwin.cpp
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
#include "nwpwin.h"
#include <windowsx.h>
int Application::Run()
{
MSG msg;
while(::GetMessage(&msg, NULL, 0, 0)){
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
std::string Window::ClassName()
{
return std::string();
}
bool Window::Register(const std::string& name)
{
WNDCLASS wc; ZeroMemory(&wc, sizeof wc);
wc.lpfnWndProc = Proc;
wc.lpszClassName = name.c_str();
wc.cbWndExtra = sizeof(Window*);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
return RegisterClass(&wc) != 0;
}
std::string Window::GenerateClassName()
{
static int cnt=1;
char s[16]; sprintf(s, "NWP%d", cnt++);
return std::string(s);
}
Window::Window() : hw(0) {}
bool Window::Create(HWND parent, DWORD style, PCSTR caption, int IdOrMenu,
int x, int y, int width, int height)
{
std::string cn = ClassName();
if(cn.empty())
Register(cn = GenerateClassName());
hw = ::CreateWindow(cn.c_str(), caption, style, x, y, width, height, parent, (HMENU)IdOrMenu, 0, this);
return hw != 0;
}
Window::operator HWND()
{
return hw;
}
POINT GetPoint(LPARAM lp)
{
POINT p;
p.x = GET_X_LPARAM(lp);
p.y = GET_Y_LPARAM(lp);
return p;
}
LRESULT CALLBACK Window::Proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_CREATE){
CREATESTRUCT* pcs = reinterpret_cast<CREATESTRUCT*>(lParam);
Window* pw = reinterpret_cast<Window*>(pcs->lpCreateParams);
::SetWindowLong(hwnd, 0, reinterpret_cast<long>(pw));
pw->hw = hwnd;
return pw->OnCreate(pcs);
}
Window* pThis = reinterpret_cast<Window*>(::GetWindowLong(hwnd, 0));
switch (message)
{
case WM_COMMAND: pThis->OnCommand(LOWORD(wParam)); return 0;
case WM_DESTROY: pThis->OnDestroy(); return 0;
case WM_KEYDOWN: pThis->OnKeyDown(wParam); return 0;
case WM_KEYUP: pThis->OnKeyUp(wParam); return 0;
case WM_LBUTTONDOWN: pThis->OnLButtonDown(GetPoint(lParam)); return 0;
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
pThis->OnPaint(hdc);
EndPaint(hwnd, &ps);
return 0;
}
return ::DefWindowProc(hwnd, message, wParam, lParam);
}