This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
widget.cpp
194 lines (167 loc) · 4.55 KB
/
widget.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "../include/pch.hpp"
#include "../include/widget.hpp"
#include "../include/log_helper.hpp"
#include <imgui_impl_win32.h>
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
namespace Win11SysCheck
{
namespace gui
{
std::unordered_map <HWND, CWidget*> CWidget::m_upWidgets;
LRESULT CALLBACK CWidget::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, message, wParam, lParam))
return true;
const auto current_widget = CWidget::m_upWidgets[hWnd];
if (current_widget)
{
if (current_widget->m_umCustomMessages[message])
{
return current_widget->m_umCustomMessages[message](hWnd, wParam, lParam);
}
else
{
if (current_widget->m_bIsWindow)
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
else
{
return CallWindowProc(current_widget->m_pOriginalWndProc, hWnd, message, wParam, lParam);
}
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
CWidget::CWidget(HWND hParentWnd, HINSTANCE hInstance, bool bIsWindow)
: m_hFont(nullptr), m_hWnd(nullptr), m_hParentWnd(hParentWnd), m_pOriginalWndProc(nullptr), m_bIsWindow(bIsWindow), m_bVisible(true)
{
m_hInstance = (hInstance ? hInstance : reinterpret_cast<HINSTANCE>(GetWindowLongPtrA(hParentWnd, GWLP_HINSTANCE)));
}
CWidget::~CWidget()
{
if (m_hWnd)
{
m_upWidgets.erase(m_hWnd);
if (IsWindow(m_hWnd))
DestroyWindow(m_hWnd);
m_hWnd = nullptr;
}
if (m_hFont)
{
DeleteObject(m_hFont);
m_hFont = nullptr;
}
m_bVisible = false;
}
bool CWidget::Create(const std::string& stClassName, const std::string& stWidgetName, rectangle& rcRectangle, DWORD dwStyle, DWORD dwExStyle, HMENU hMenu)
{
m_hWnd = CreateWindowExA(
dwExStyle, stClassName.c_str(), stWidgetName.c_str(), dwStyle,
rcRectangle.x, rcRectangle.y, rcRectangle.width, rcRectangle.height,
m_hParentWnd, hMenu, m_hInstance, nullptr
);
if (!m_hWnd)
{
CLogHelper::Instance().LogError("CreateWindow fail!", true);
return false;
}
m_hFont = CreateFontA(13, 0, 0, 0, FW_DONTCARE, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, 5, DEFAULT_PITCH | FF_DONTCARE, "Tahoma");
if (!m_hFont)
{
CLogHelper::Instance().LogError("CreateFontA fail!", true);
return false;
}
SendMessage(m_hWnd, WM_SETFONT, reinterpret_cast<WPARAM>(m_hFont), TRUE);
if (!m_bIsWindow)
{
m_pOriginalWndProc = reinterpret_cast<WNDPROC>(SetWindowLongPtrA(m_hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(CWidget::WndProc)));
}
m_rcCurrRect = rcRectangle;
m_upWidgets[m_hWnd] = this;
return true;
}
void CWidget::AddMessageHandlers(const std::vector <TMessagePair>& vMessages)
{
for (const auto& msg : vMessages)
{
m_umCustomMessages[msg.first] = msg.second;
}
}
void CWidget::RemoveMessageHandler(uint32_t message)
{
m_umCustomMessages.erase(message);
}
bool CWidget::SetText(const std::string& stText) const
{
return (SetWindowTextA(m_hWnd, stText.c_str()) != FALSE);
}
HWND CWidget::GetHandle() const
{
return m_hWnd;
}
void CWidget::SetInstance(HINSTANCE hInstance)
{
m_hInstance = hInstance;
}
HINSTANCE CWidget::GetInstance() const
{
return m_hInstance;
}
void CWidget::SetVisible(bool bIsVisible)
{
m_bVisible = bIsVisible;
if (m_bVisible)
ShowWindow(m_hWnd, SW_SHOW);
else
ShowWindow(m_hWnd, SW_HIDE);
}
bool CWidget::IsVisible() const
{
return m_bVisible;
}
void CWidget::Show()
{
m_bVisible = true;
ShowWindow(m_hWnd, SW_SHOW);
}
void CWidget::Hide()
{
m_bVisible = false;
ShowWindow(m_hWnd, SW_HIDE);
}
int CWidget::GetScreenWidth() const
{
return GetSystemMetrics(SM_CXSCREEN);
}
int CWidget::GetScreenHeight() const
{
return GetSystemMetrics(SM_CYSCREEN);
}
void CWidget::GetWindowRect(RECT* pRC)
{
::GetWindowRect(m_hWnd, pRC);
}
void CWidget::GetClientRect(RECT* pRC)
{
::GetClientRect(m_hWnd, pRC);
}
void CWidget::GetMousePosition(POINT* pPT)
{
GetCursorPos(pPT);
ScreenToClient(m_hWnd, pPT);
}
void CWidget::SetPosition(int x, int y)
{
SetWindowPos(m_hWnd, nullptr, x, y, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
void CWidget::SetCenterPosition()
{
RECT rc{ 0 };
GetClientRect(&rc);
const auto nWindowWidth = rc.right - rc.left;
const auto nWindowHeight = rc.bottom - rc.top;
SetPosition((GetScreenWidth() - nWindowWidth) / 2, (GetScreenHeight() - nWindowHeight) / 2);
}
}
};