This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path源.cpp
349 lines (285 loc) · 8.52 KB
/
源.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#ifndef UNICODE
#define UNICODE
#endif // !UNICODE
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "BaseWindow.h"
#include <d2d1.h>
#include <windowsx.h>
#include <dwrite.h>
#include "powerscheme.h"
template<class T> void SafeRelease(T** ppT) {
if (*ppT) {
(*ppT)->Release();
*ppT = NULL;
}
}
BOOL inRect(LPARAM pos, D2D1_RECT_F rect) {
int x = GET_X_LPARAM(pos);
int y = GET_Y_LPARAM(pos);
if (x<rect.right && x>rect.left && y>rect.top && y<rect.bottom)
return TRUE;
else
return FALSE;
}
const D2D1_COLOR_F Red = D2D1::ColorF(0xF65314);
const D2D1_COLOR_F Green = D2D1::ColorF(0x7CBB00);
const D2D1_COLOR_F Blue= D2D1::ColorF(0x00A1F1);
const D2D1_COLOR_F Yellow = D2D1::ColorF(0xFFBB00);
const D2D1_COLOR_F DarkCover = D2D1::ColorF(D2D1::ColorF::Black, 0.6f);
const D2D1_COLOR_F LightCover = D2D1::ColorF(D2D1::ColorF::White, 0.4f);
const float sideBarWidth = 50.f;
class MainWindow : public BaseWindow<MainWindow> {
PowerScheme powerScheme;
ID2D1Factory* pFactory = NULL;
ID2D1HwndRenderTarget* pRenderTarget = NULL;
ID2D1SolidColorBrush* pBrushRed = NULL;
ID2D1SolidColorBrush* pBrushGreen = NULL;
ID2D1SolidColorBrush* pBrushBlue = NULL;
ID2D1SolidColorBrush* pBrushYellow = NULL;
ID2D1SolidColorBrush* pBrushWhite = NULL;
ID2D1SolidColorBrush* pBrushBlack= NULL;
ID2D1SolidColorBrush* pBrushDrakCover = NULL;
ID2D1SolidColorBrush* pBrushLightCover = NULL;
IDWriteFactory* pDWriteFactory = NULL;
IDWriteTextFormat* pTextFormatYaHei = NULL;
IDWriteTextFormat* pTextFormatSegoe= NULL;
const wchar_t* wszTextTl = NULL;
const wchar_t* wszTextTr = NULL;
const wchar_t* wszTextBl = NULL;
const wchar_t* wszTextBr = NULL;
const wchar_t* wszTextHome = NULL;
D2D1_RECT_F rectTl;
D2D1_RECT_F rectTr;
D2D1_RECT_F rectBl;
D2D1_RECT_F rectBr;
D2D1_RECT_F rectAll;
D2D1_RECT_F rectSideBar;
D2D1_RECT_F rectHome;
float x = 0.f, y = 0.f; //整个Client
HCURSOR arrowCursor = LoadCursor(NULL, IDC_ARROW);
HCURSOR waitCursor = LoadCursor(NULL, IDC_WAIT);
LRESULT CreateGraphicsResources();
void DiscardGraphcisResources();
void CalculateLayout();
void OnPaint();
void ReSize();
void SchemeSelect(LPARAM lParam);
void FillSchemeRect(int rect = 0);
public:
PCWSTR ClassName() const { return L"Mode Changer Window Class"; }
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
};
LRESULT MainWindow::CreateGraphicsResources() {
HRESULT hr = S_OK;
if (pRenderTarget == NULL) {
RECT rc;
GetClientRect(m_hwnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);
hr = pFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(m_hwnd, size),
&pRenderTarget
);
pRenderTarget->CreateSolidColorBrush(Red, &pBrushRed);
pRenderTarget->CreateSolidColorBrush(Green, &pBrushGreen);
pRenderTarget->CreateSolidColorBrush(Blue, &pBrushBlue);
pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &pBrushWhite);
pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &pBrushBlack);
pRenderTarget->CreateSolidColorBrush(DarkCover, &pBrushDrakCover);
pRenderTarget->CreateSolidColorBrush(LightCover, &pBrushLightCover);
hr = pRenderTarget->CreateSolidColorBrush(Yellow, &pBrushYellow);
if (SUCCEEDED(hr)) {
wszTextTl = L"平衡";
wszTextTr = L"DELL";
wszTextBl = L"下载";
wszTextBr = L"Gaming";
wszTextHome = L"";
pDWriteFactory->CreateTextFormat(
L"Segoe MDL2 Assets",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
36.0f,
L"zh-cn",
&pTextFormatSegoe
);
pDWriteFactory->CreateTextFormat(
L"Microsoft YaHei UI",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
72.0f,
L"zh-cn",
&pTextFormatYaHei
);
pTextFormatSegoe->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
pTextFormatSegoe->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
pTextFormatYaHei->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
hr = pTextFormatYaHei->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
}
if (SUCCEEDED(hr)) {
CalculateLayout();
}
}
return hr;
}
void MainWindow::DiscardGraphcisResources() {
SafeRelease(&pRenderTarget);
SafeRelease(&pBrushRed);
SafeRelease(&pBrushGreen);
SafeRelease(&pBrushBlue);
SafeRelease(&pBrushYellow);
SafeRelease(&pBrushWhite);
SafeRelease(&pBrushBlack);
SafeRelease(&pBrushDrakCover);
SafeRelease(&pBrushLightCover);
SafeRelease(&pTextFormatYaHei);
SafeRelease(&pTextFormatSegoe);
SafeRelease(&pFactory);
SafeRelease(&pDWriteFactory);
}
void MainWindow::CalculateLayout() {
if (pRenderTarget != NULL) {
D2D1_SIZE_F size = pRenderTarget->GetSize();
x = size.width;
y = size.height;
rectTl = D2D1::RectF(sideBarWidth, 0.f, (x + sideBarWidth) / 2, y / 2);
rectTr = D2D1::RectF((x + sideBarWidth) / 2, 0.f, x, y / 2);
rectBl = D2D1::RectF(sideBarWidth, y / 2, (x + sideBarWidth) / 2, y);
rectBr = D2D1::RectF((x + sideBarWidth) / 2, y / 2, x, y);
rectAll = D2D1::RectF(sideBarWidth, 0.f, x, y);
rectSideBar = D2D1::RectF(0, 0, 50, y * 2);
rectHome = D2D1::RectF(0, 0, 50, 50);
}
}
void MainWindow::OnPaint() {
HRESULT hr = CreateGraphicsResources();
if (SUCCEEDED(hr)) {
PAINTSTRUCT ps;
BeginPaint(m_hwnd, &ps);
pRenderTarget->BeginDraw();
pRenderTarget->Clear();
pRenderTarget->FillRectangle(&rectSideBar, pBrushBlack);
pRenderTarget->DrawTextW(wszTextHome, (UINT32)wcslen(wszTextHome), pTextFormatSegoe, rectHome, pBrushWhite);
pRenderTarget->FillRectangle(&rectHome, pBrushLightCover);
FillSchemeRect();
pRenderTarget->FillRectangle(&rectAll, pBrushDrakCover);
FillSchemeRect(powerScheme.getActiveCfg());
hr = pRenderTarget->EndDraw();
if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET) {
DiscardGraphcisResources();
}
EndPaint(m_hwnd, &ps);
}
}
LRESULT MainWindow::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
if (FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory))) {
return -1;
}
if (FAILED(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown * *>(&pDWriteFactory)))) {
return -2;
}
return 0;
}
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
}
case WM_PAINT: {
OnPaint();
return 0;
}
/*
case WM_CLOSE: {
if (MessageBox(m_hwnd, L"确定要关闭吗?", L"Mode Changer", MB_OKCANCEL) == IDOK)
DestroyWindow(m_hwnd);
return 0;
}
*/
case WM_SIZE: {
ReSize();
return 0;
}
case WM_LBUTTONUP: {
SetCursor(waitCursor);
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
if (x > 50)
SchemeSelect(lParam);
OnPaint();
SetCursor(arrowCursor);
return 0;
}
case WM_SETCURSOR: {
SetCursor(arrowCursor);
}
}
return DefWindowProc(m_hwnd, uMsg, wParam, lParam);
}
void MainWindow::ReSize() {
if (pRenderTarget != NULL) {
RECT rc;
GetClientRect(m_hwnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);
pRenderTarget->Resize(size);
CalculateLayout();
InvalidateRect(m_hwnd, NULL, FALSE);
}
}
void MainWindow::SchemeSelect(LPARAM lParam) {
if (inRect(lParam, rectTl))
powerScheme.changecfg(1);
else if (inRect(lParam, rectTr))
powerScheme.changecfg(2);
else if (inRect(lParam, rectBl))
powerScheme.changecfg(3);
else if (inRect(lParam, rectBr))
powerScheme.changecfg(4);
}
void MainWindow::FillSchemeRect(int rect) {
switch (rect) {
case 0:
FillSchemeRect(1);
FillSchemeRect(2);
FillSchemeRect(3);
FillSchemeRect(4);
break;
case 1:
pRenderTarget->FillRectangle(&rectTl, pBrushRed);
pRenderTarget->DrawTextW(wszTextTl, (UINT32)wcslen(wszTextTl), pTextFormatYaHei, rectTl, pBrushWhite);
break;
case 2:
pRenderTarget->FillRectangle(&rectTr, pBrushGreen);
pRenderTarget->DrawTextW(wszTextTr, (UINT32)wcslen(wszTextTr), pTextFormatYaHei, rectTr, pBrushWhite);
break;
case 3:
pRenderTarget->FillRectangle(&rectBl, pBrushBlue);
pRenderTarget->DrawTextW(wszTextBl, (UINT32)wcslen(wszTextBl), pTextFormatYaHei, rectBl, pBrushWhite);
break;
case 4:
pRenderTarget->FillRectangle(&rectBr, pBrushYellow);
pRenderTarget->DrawTextW(wszTextBr, (UINT32)wcslen(wszTextBr), pTextFormatYaHei, rectBr, pBrushWhite);
break;
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) {
MainWindow win;
if (!win.Create(L"Mode Changer", WS_OVERLAPPEDWINDOW)) {
return 0;
}
ShowWindow(win.Window(), nCmdShow);
/*---消息循环---*/
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}