-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathBaseWindow.cpp
178 lines (156 loc) · 6.66 KB
/
BaseWindow.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// sktoolslib - common files for SK tools
// Copyright (C) 2012-2013, 2015-2018, 2020-2021 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include "BaseWindow.h"
#include <memory>
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
bool CWindow::RegisterWindow(UINT style, HICON hIcon, HCURSOR hCursor, HBRUSH hbrBackground,
LPCWSTR lpszMenuName, LPCWSTR lpszClassName, HICON hIconSm)
{
WNDCLASSEX wcx;
// Fill in the window class structure with default parameters
wcx.cbSize = sizeof(WNDCLASSEX); // size of structure
wcx.style = style; // redraw if size changes
wcx.lpfnWndProc = CWindow::stWinMsgHandler; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hResource; // handle to instance
wcx.hIcon = hIcon; // predefined app. icon
wcx.hCursor = hCursor; // predefined arrow
wcx.hbrBackground = hbrBackground; // white background brush
wcx.lpszMenuName = lpszMenuName; // name of menu resource
wcx.lpszClassName = lpszClassName; // name of window class
wcx.hIconSm = hIconSm; // small class icon
// Register the window class.
return RegisterWindow(&wcx);
}
bool CWindow::RegisterWindow(CONST WNDCLASSEX* wcx)
{
// Register the window class.
sClassName = std::wstring(wcx->lpszClassName);
bRegisterWindowCalled = true;
if (RegisterClassEx(wcx) == 0)
{
if (GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
return TRUE;
return FALSE;
}
else
return TRUE;
}
LRESULT CALLBACK CWindow::stWinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_NCCREATE)
{
// get the pointer to the window from lpCreateParams which was set in CreateWindow
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(reinterpret_cast<LPCREATESTRUCT>(lParam)->lpCreateParams));
}
// get the pointer to the window
CWindow* pWnd = GetObjectFromWindow(hwnd);
// if we have the pointer, go to the message handler of the window
// else, use DefWindowProc
if (pWnd)
{
switch (uMsg)
{
case WM_CREATE:
if ((!pWnd->bWindowRestored) && (!pWnd->sRegistryPath.empty()))
{
WINDOWPLACEMENT wpl = {0};
DWORD size = sizeof(wpl);
if (SHGetValue(HKEY_CURRENT_USER, pWnd->sRegistryPath.c_str(), pWnd->sRegistryValue.c_str(), REG_NONE, &wpl, &size) == ERROR_SUCCESS)
SetWindowPlacement(hwnd, &wpl);
else
ShowWindow(hwnd, SW_SHOW);
pWnd->bWindowRestored = true;
}
break;
case WM_CLOSE:
if (!pWnd->sRegistryPath.empty())
{
WINDOWPLACEMENT wpl = {0};
wpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(hwnd, &wpl);
SHSetValue(HKEY_CURRENT_USER, pWnd->sRegistryPath.c_str(), pWnd->sRegistryValue.c_str(), REG_NONE, &wpl, sizeof(wpl));
}
break;
}
return pWnd->WinMsgHandler(hwnd, uMsg, wParam, lParam);
}
else
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
bool CWindow::Create()
{
// Create the window
RECT rect;
rect.top = 0;
rect.left = 0;
rect.right = 600;
rect.bottom = 400;
return Create(WS_OVERLAPPEDWINDOW | WS_VISIBLE, nullptr, &rect);
}
bool CWindow::Create(DWORD dwStyles, HWND hParent /* = nullptr */, RECT* rect /* = nullptr */)
{
return CreateEx(0, dwStyles, hParent, rect);
}
bool CWindow::CreateEx(DWORD dwExStyles, DWORD dwStyles, HWND hParent /* = nullptr */, RECT* rect /* = nullptr */, LPCWSTR classname /* = nullptr */, HMENU hMenu /* = nullptr */)
{
// send the this pointer as the window creation parameter
if (rect == nullptr)
m_hwnd = CreateWindowEx(dwExStyles, classname ? classname : sClassName.c_str(), sWindowTitle.c_str(), dwStyles, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hParent, hMenu, hResource, static_cast<void*>(this));
else
{
m_hwnd = CreateWindowEx(dwExStyles, classname ? classname : sClassName.c_str(), sWindowTitle.c_str(), dwStyles, rect->left, rect->top,
rect->right - rect->left, rect->bottom - rect->top, hParent, hMenu, hResource,
static_cast<void*>(this));
}
m_hParent = hParent;
if (!bRegisterWindowCalled)
{
::SetWindowLongPtr(m_hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
prevWndProc = reinterpret_cast<WNDPROC>(GetWindowLongPtr(m_hwnd, GWLP_WNDPROC));
::SetWindowLongPtr(m_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(stWinMsgHandler));
}
return (m_hwnd != nullptr);
}
void CWindow::SetTransparency(BYTE alpha, COLORREF color /* = 0xFF000000 */)
{
if (alpha == 255)
{
LONG_PTR exStyle = GetWindowLongPtr(*this, GWL_EXSTYLE);
exStyle &= ~WS_EX_LAYERED;
SetWindowLongPtr(*this, GWL_EXSTYLE, exStyle);
}
else
{
LONG_PTR exStyle = GetWindowLongPtr(*this, GWL_EXSTYLE);
exStyle |= WS_EX_LAYERED;
SetWindowLongPtr(*this, GWL_EXSTYLE, exStyle);
}
COLORREF col = color;
DWORD flags = LWA_ALPHA;
if (col & 0xFF000000)
{
col = RGB(255, 255, 255);
flags = LWA_ALPHA;
}
else
{
flags = LWA_COLORKEY;
}
SetLayeredWindowAttributes(*this, col, alpha, flags);
}