-
Notifications
You must be signed in to change notification settings - Fork 1
/
minivg.inl
2184 lines (1863 loc) · 60.6 KB
/
minivg.inl
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2005-2020 sdragonx (mail:[email protected])
minivg.inl
2020-07-08 23:31:53
*/
#ifndef MINIVG_INL_20200708233153
#define MINIVG_INL_20200708233153
//#include "minivg.hpp"
#include <stdio.h>
#include <tchar.h>
#include <time.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <ole2.h> // CreateStreamOnHGlobal
#if __cplusplus < 201103L
#define nullptr NULL
#endif
#ifdef __GNUC
#define __MT__
#endif
#ifdef _MSC_VER
#pragma warning(disable: 4717)
#endif
#include <process.h>
#if defined(__BORLANDC__) || defined(_MSC_VER)
#pragma comment (lib, "gdiplus.lib")
#pragma comment (lib, "winmm.lib")
#pragma comment (lib, "ole32.lib")
#endif
#define MINIVG_VERSION PCWSTR(L"minivg 2021-07-30")
#define MINIVG_CLASS_NAME PCWSTR(L"minivg_window")
#define MINIVG_DEFAULT_FONT PCWSTR(L"msyh") // 微软雅黑
#define MINIVG_TIMER_ID 1
#define MINIVG_INLINE inline
namespace minivg {
namespace detail {
// 获取浮点时间戳
inline float tick_time()
{
return static_cast<float>(clock()) * 0.001f;
}
//---------------------------------------------------------------------------
//
// 资源管理类
//
//---------------------------------------------------------------------------
// 安全删除指针
template<typename T>
void safe_delete(T* &p)
{
if (p) {
delete p;
p = NULL;
}
}
template<typename T>
void delete_object(T& obj)
{
DeleteObject(obj);
obj = NULL;
}
// 删除stl容器里面的内容
template<typename T>
void delete_all(T& obj)
{
typename T::iterator itr = obj.begin();
for (; itr != obj.end(); ++itr) {
delete itr->second;
}
obj.clear();
}
// 创建一个 HBITMAP
MINIVG_INLINE HBITMAP bm_create(int width, int height, int pixelbits = 32)
{
HBITMAP hBitmap;
BITMAPV5HEADER bi;
void *lpBits = 0;
ZeroMemory(&bi, sizeof(BITMAPV5HEADER));
bi.bV5Size = sizeof(BITMAPV5HEADER);
bi.bV5Width = width;
bi.bV5Height = height;
bi.bV5Planes = 1;
bi.bV5BitCount = pixelbits;
bi.bV5Compression = BI_BITFIELDS;
bi.bV5RedMask = 0x00FF0000;
bi.bV5GreenMask = 0x0000FF00;
bi.bV5BlueMask = 0x000000FF;
bi.bV5AlphaMask = 0xFF000000;
hBitmap = CreateDIBSection(GetDC(nullptr), (BITMAPINFO *) &bi, DIB_RGB_COLORS, (void **) &lpBits, nullptr, (DWORD) 0);
return hBitmap;
}
// 资源管理类
class ezResource
{
private:
std::map<unistring, ezImage*> images; // 加载的图片
std::map<int, ezImage*> resource_images; // 加载的资源图片
std::vector<ezImage*> image_pool; // 创建的图片
public:
// 加载一个图片
ezImage* loadimage(const unistring& name)
{
ezImage* bmp = nullptr;
std::map<unistring, ezImage*>::iterator itr;
itr = images.find(name);
if (itr == images.end()) {
bmp = new ezImage;
if (bmp->open(name) == EZ_OK) {
images[name] = bmp;
}
else {
delete bmp;
}
}
else {
bmp = itr->second;
}
return bmp;
}
// 加载资源图片
ezImage* loadimage(int id, PCTSTR resource_type)
{
ezImage* bmp = nullptr;
std::map<int, ezImage*>::iterator itr;
itr = resource_images.find(id);
if (itr == resource_images.end()) {
bmp = new ezImage;
if (bmp->open(id, resource_type) == EZ_OK) {
resource_images[id] = bmp;
}
else {
delete bmp;
}
}
else {
bmp = itr->second;
}
return bmp;
}
// 创建图片
ezImage* allocate(int width, int height)
{
ezImage *image = new ezImage();
image->create(width, height);
image_pool.push_back(image);
return image;
}
// 删除图片
void free(ezImage* image)
{
std::vector<ezImage*>::iterator itr = std::find(
image_pool.begin(), image_pool.end(), image);
if (itr != image_pool.end()) {
delete *itr;
image_pool.erase(itr);
}
}
// 释放所有资源,这个函数在程序退出的时候执行
void dispose()
{
delete_all(images);
delete_all(resource_images);
for (size_t i = 0; i < image_pool.size(); ++i) {
delete image_pool[i];
}
image_pool.clear();
}
};
//---------------------------------------------------------------------------
//
// 界面
//
//---------------------------------------------------------------------------
class ezWindow
{
protected:
HWND m_handle;
public:
ezWindow() : m_handle() {}
HWND handle()const { return m_handle; }
int create(
PCWSTR className,
PCWSTR title,
int x, int y, int width, int height,
DWORD style = WS_OVERLAPPEDWINDOW,
DWORD styleEx = 0)
{
// 2023-03-12 23:08:02 bug
/*static bool is_init = false;
if (!is_init) {
InitClass(className, 0, basic_wndproc);
is_init = true;
}*/
init_window_class(className, basic_wndproc);
#ifndef UNICODE
std::string buf = to_ansi(title, -1);
title = (PCWSTR) buf.c_str();
#endif
// 获取当前进程句柄
HINSTANCE hModule = GetModuleHandle(nullptr);
// 创建窗口
m_handle = CreateWindowExW(
styleEx, // 窗口的扩展风格
className, // 类名
title, // 标题
style, // 风格
x, // 左边位置
y, // 顶部位置
width, // 宽度
height, // 高度
nullptr, // 父窗口的句柄
nullptr, // 菜单的句柄或是子窗口的标识符
hModule, // 应用程序实例的句柄
this); // 指向窗口的创建数据
if (m_handle == nullptr) {
MessageBox(nullptr, TEXT("Window Creation Failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
return -1;
}
return 0;
}
// 关闭窗口
void close()
{
SendMessage(m_handle, WM_CLOSE, 0, 0);
}
// 设置窗口范围
void setBounds(int x, int y, int width, int height)
{
MoveWindow(m_handle, x, y, width, height, TRUE);
}
// 移动窗口
void move(int x, int y)
{
SetWindowPos(m_handle, nullptr, x, y, 0, 0, SWP_NOSIZE);
}
// 设置窗口大小
void resize(int width, int height)
{
SetWindowPos(m_handle, nullptr, 0, 0, width, height, SWP_NOMOVE);
}
// 显示窗口
void show()
{
ShowWindow(m_handle, SW_SHOW);
}
// 隐藏窗口
void hide()
{
ShowWindow(m_handle, SW_HIDE);
}
int showModel(HWND owner)
{
if (!m_handle) {
return 0;
}
ShowWindow(m_handle, SW_SHOW);
UpdateWindow(m_handle);
if (owner) {
SetWindowLongPtr(m_handle, GWLP_HWNDPARENT, (LONG_PTR) owner);
EnableWindow(owner, FALSE);
}
MSG msg;
while (GetMessage(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (owner) {
EnableWindow(owner, TRUE);
SetForegroundWindow(owner);
}
return int(msg.wParam);
}
void setID(int id) { SetWindowLong(m_handle, GWL_ID, id); }
void setTitle(const unistring& text)
{
#ifdef UNICODE
SetWindowTextW(m_handle, text.c_str());
#else
std::string buf = to_ansi(text.c_str(), text.length());
SetWindowTextW(m_handle, (PCWSTR) buf.c_str());
#endif
}
unistring title()const
{
#ifdef UNICODE
int size = GetWindowTextLengthW(m_handle);
std::vector<wchar_t> buf;
buf.resize(size + 1);
GetWindowTextW(m_handle, &buf[0], size + 1);
return unistring(&buf[0], size);
#else
int size = GetWindowTextLengthW(m_handle);
std::vector<char> buf;
buf.resize(size + 1);
GetWindowTextW(m_handle, (wchar_t*) &buf[0], size + 1);
return unistring(&buf[0], size);
#endif
}
void setText(const unistring& text)
{
SetWindowTextW(m_handle, text.c_str());
}
unistring text()const
{
int size = GetWindowTextLengthW(m_handle);
std::vector<wchar_t> buf;
buf.resize(size + 1);
GetWindowTextW(m_handle, &buf[0], size + 1);
return unistring(&buf[0], size);
}
LRESULT send(int id, UINT msg, WPARAM wParam, LPARAM lParam)
{
return SendDlgItemMessage(m_handle, id, msg, wParam, lParam);
}
void setFont(HFONT hFont)
{
SendMessage(m_handle, WM_SETFONT, (WPARAM) hFont, TRUE);
}
//这个函数不工作
HFONT getFont()
{
return (HFONT) (UINT_PTR) SendMessage(m_handle, WM_GETFONT, 0, 0);
}
void repaint()
{
RECT rc;
GetClientRect(m_handle, &rc);
RedrawWindow(m_handle, &rc, 0, RDW_UPDATENOW | RDW_INVALIDATE | RDW_NOERASE);
}
protected:
static LRESULT CALLBACK basic_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
ezWindow *win = reinterpret_cast<ezWindow*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (win) {
return win->wndproc(msg, wparam, lparam);
}
else if (msg == WM_CREATE) {
LPCREATESTRUCTW pcs = LPCREATESTRUCTW(lparam);
win = reinterpret_cast<ezWindow*>(pcs->lpCreateParams);
if (win) {
win->m_handle = hwnd;
::SetLastError(ERROR_SUCCESS);
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(win));
if (GetLastError() != ERROR_SUCCESS)
return -1;
else
return win->wndproc(msg, wparam, lparam);
}
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
protected:
virtual LRESULT wndproc(UINT msg, WPARAM wparam, LPARAM lparam)
{
if (m_handle) {
return DefWindowProc(m_handle, msg, wparam, lparam);
}
else {
return 0;
}
}
void MotifyStyle(DWORD style, bool enable)
{
if (enable) {
SetWindowLong(m_handle, GWL_STYLE, GetWindowLong(m_handle, GWL_STYLE) | style);
}
else {
SetWindowLong(m_handle, GWL_STYLE, GetWindowLong(m_handle, GWL_STYLE) & (~style));
}
}
// 创建控件
HWND CreateComponent(ezWindow* parent, PCWSTR classname, int x, int y, int width, int height, int style, int styleEx = 0)
{
HWND hwnd = CreateWindowExW(styleEx, classname, nullptr, style,
x, y, width, height, parent->handle(), (HMENU) nullptr, GetModuleHandle(nullptr), nullptr);
HFONT font = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hwnd, WM_SETFONT, (WPARAM) font, 0);
return hwnd;
}
// 初始化窗口类
BOOL init_window_class(LPCWSTR classname, WNDPROC WndProc)
{
HINSTANCE hInstance = GetModuleHandle(nullptr);
WNDCLASSEXW wc = { sizeof(wc) };
if (GetClassInfoExW(hInstance, classname, &wc)) {
//printf("class is exists.\n");
}
else {
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;// CS_DBLCLKS 支持鼠标双击事件
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW);
wc.lpszClassName = classname;
wc.hIcon = LoadIcon(hInstance, TEXT("IDI_APPLICATION"));
wc.hIconSm = LoadIcon(hInstance, TEXT("IDI_APPLICATION"));
ATOM atom = RegisterClassExW(&wc);
if (!atom) {
MessageBox(nullptr, TEXT("Register Window Class Failed!"), TEXT("Error!"), MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
//printf("register class success.\n");
}
return TRUE;
}
};
class ezButton : public ezWindow
{
public:
int create(ezWindow* parent, int x, int y, int width, int height)
{
DWORD style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;
m_handle = CreateComponent(parent, L"button", x, y, width, height, style);
return 0;
}
void setDefault(bool value)
{
MotifyStyle(BS_DEFPUSHBUTTON, value);
}
};
class ezLabel : public ezWindow
{
public:
int create(ezWindow* parent, int x, int y, int width, int height)
{
DWORD style = WS_CHILD | WS_VISIBLE | SS_EDITCONTROL;
m_handle = CreateComponent(parent, L"static", x, y, width, height, style);
return 0;
}
};
class ezEdit : public ezWindow
{
public:
int create(ezWindow* parent, int x, int y, int width, int height)
{
DWORD style = WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_AUTOVSCROLL;
m_handle = CreateComponent(parent, L"edit", x, y, width, height, style, WS_EX_CLIENTEDGE);
return 0;
}
void multiline(bool value)
{
MotifyStyle(ES_MULTILINE, value);
}
void setMaxLength(UINT n)
{
SendMessage(m_handle, EM_LIMITTEXT, n, 0);
}
};
//
// InputBox
//
class ezInputBox : private ezWindow
{
private:
unistring m_message;
unistring m_text;
bool m_result;
ezLabel lblMessage;
ezEdit editbox;
ezButton btnOK;
ezButton btnCancel;
public:
bool execute(HWND parent, const unistring& title, const unistring& message, const unistring& text = unistring())
{
int cx = GetSystemMetrics(SM_CXFULLSCREEN);
int cy = GetSystemMetrics(SM_CYFULLSCREEN);
int w = 400;
int h = 150;
int x = (cx - w) / 2;
int y = (cy - h) / 2;
m_message = message;
m_text = text;
ezWindow::create(L"EZGDI_InputBox", title.c_str(), x, y, w, h, WS_CAPTION | WS_SYSMENU | WS_CLIPSIBLINGS);
showModel(parent);
return m_result;
}
unistring text()const
{
return m_text;
}
protected:
void on_create()
{
RECT rect;
GetClientRect(m_handle, &rect);
lblMessage.create(this, 4, 5, rect.right - 80, 70);
lblMessage.setID(1000);
lblMessage.setText(m_message);
btnOK.create(this, rect.right - 70, 8, 64, 24);
btnOK.setID(IDOK);
btnOK.setDefault(true);
btnOK.setText(L"确定(&O)");
btnCancel.create(this, rect.right - 70, 36, 64, 24);
btnCancel.setID(IDCANCEL);
btnCancel.setText(L"取消(&C)");
editbox.create(this, 4, rect.bottom - 24, rect.right - 8, 20);
editbox.setID(2000);
editbox.setText(m_text);
editbox.setMaxLength(128);
}
void ButtonOKClick()
{
TCHAR buf[256];
GetDlgItemText(m_handle, 2000, buf, 256);
m_text = buf;
}
LRESULT wndproc(UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg) {
case WM_CREATE:
on_create();
break;
case WM_DESTROY:
m_handle = nullptr;
PostQuitMessage(0);
break;
case WM_KEYDOWN:
if (wparam == VK_RETURN) {
SendMessage(m_handle, WM_COMMAND, IDOK, 0);
}
else if (wparam == VK_ESCAPE) {
SendMessage(m_handle, WM_COMMAND, IDCANCEL, 0);
}
break;
case WM_CHAR:
SendDlgItemMessage(m_handle, 2000, msg, wparam, lparam);
break;
case WM_SETFOCUS:
SendDlgItemMessage(m_handle, 2000, WM_SETFOCUS, 0, 0);
break;
case WM_COMMAND:
switch (LOWORD(wparam)) {
case IDOK:
ButtonOKClick();
DestroyWindow(m_handle);
m_result = true;
break;
case IDCANCEL:
DestroyWindow(m_handle);
m_result = false;
break;
};
break;
default:
break;
}
return DefWindowProc(m_handle, msg, wparam, lparam);
}
};
//---------------------------------------------------------------------------
//
// minivg 实例类
//
//---------------------------------------------------------------------------
template<typename T = int>
class vgContext : public ezWindow
{
public:
HDC hdc; // GDI 绘图设备
Gdiplus::Graphics* g; // GDIPlus 设备
HBITMAP pixelbuf; // 像素缓冲区
int effectLevel; // 效果等级
Gdiplus::Pen* pen; // 画笔
Gdiplus::SolidBrush* brush; // 画刷
Gdiplus::Font* font; // 字体
Gdiplus::SolidBrush* font_color;
unistring fontName;
float fontSize;
int fontStyle;
bool fontIsChange;
WNDPROC prevWndProc; // 如果是设置已有的窗口,保存已有窗口的消息处理函数
int initParam; // 创建参数
RECT winRect; // 窗口模式下窗口大小
bool fullscreen; // 是否全屏
vec4i vp; // 视口
bool running; // 程序是否运行
EZ_KEY_EVENT OnKeyDown; // 键盘事件
EZ_KEY_EVENT OnKeyUp;
EZ_KEY_EVENT OnKeyPress;
EZ_MOUSE_EVENT OnMouseDown; // 鼠标事件
EZ_MOUSE_EVENT OnMouseUp;
EZ_MOUSE_EVENT OnMouseMove;
EZ_TIMER_EVENT OnTimer; // 计时器事件
float tick;
EZ_PAINT_EVENT OnPaint; // 窗口绘制事件
ezResource resource; // 资源管理器
static vgContext instance;
private:
ULONG_PTR token;
Gdiplus::GdiplusStartupInput input;
public:
vgContext() :
ezWindow(),
hdc(),
g(),
pixelbuf(),
effectLevel(EZ_MEDIUM),
pen(),
brush(),
font(),
fontName(MINIVG_DEFAULT_FONT),
fontSize(12),
fontStyle(EZ_NORMAL),
running(true),
OnKeyDown(), OnKeyUp(), OnKeyPress(),
OnMouseDown(), OnMouseUp(), OnMouseMove(),
OnTimer(),
OnPaint()
{
prevWndProc = nullptr;
gdiplusInit();
tick = tick_time();
}
~vgContext()
{
resource.dispose();
gdiplusShutdown();
}
// 设置到已有的窗口
void setWindow(HWND hwnd)
{
if (hwnd) {
m_handle = hwnd;
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(this));
prevWndProc = (WNDPROC) SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) basic_wndproc);
}
else {
if (prevWndProc) {
SetWindowLongPtr(m_handle, GWLP_WNDPROC, (LONG_PTR) prevWndProc);
m_handle = nullptr;
prevWndProc = nullptr;
resource.dispose();
gdiplusShutdown();
}
}
}
// 设置边框样式
void setStyle(int style)
{
DWORD dwStyle;
DWORD dwExStyle;
switch (style) {
case EZ_FIXED:
dwStyle = WS_POPUPWINDOW | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX;
dwExStyle = WS_EX_CLIENTEDGE;
break;
case EZ_SIZEABLE:
dwStyle = WS_OVERLAPPEDWINDOW;
dwExStyle = WS_EX_CLIENTEDGE;
break;
case EZ_FULLSCREEN:
dwStyle = WS_POPUP;
dwExStyle = 0;
break;
default:
dwStyle = WS_OVERLAPPEDWINDOW;
dwExStyle = WS_EX_CLIENTEDGE;
break;
}
dwStyle |= WS_VISIBLE;
dwExStyle |= WS_EX_APPWINDOW;
SetWindowLong(m_handle, GWL_STYLE, dwStyle);
SetWindowLong(m_handle, GWL_EXSTYLE, dwExStyle);
}
// 重建背景缓冲区
void viewport(int x, int y, int width, int height)
{
closeGraphics();
if (!width || !height) {
return;
}
if (width != vp.z || height != vp.w) {
pixelbuf = bm_create(width, height);
SelectObject(hdc, pixelbuf);
g = new Gdiplus::Graphics(hdc);
effect_level(effectLevel);
}
vp = vec4i(x, y, width, height);
}
// 将缓冲区的图像绘制到目标 HDC
void bitblt(HDC dc)
{
BitBlt(dc, vp.x, vp.y, vp.z, vp.w, hdc, 0, 0, SRCCOPY);
}
// 设置窗口置顶
bool topmose(bool top)
{
if (top) {
return SetWindowPos(m_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
else {
return SetWindowPos(m_handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
}
// 主窗口消息
LRESULT wndproc(UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message) {
case WM_CREATE:
break;
case WM_DESTROY:
running = false;
PostQuitMessage(0);
break;
case WM_WINDOWPOSCHANGING:
break;
case WM_ERASEBKGND:
return TRUE;
case WM_QUIT:
running = false;
break;
case WM_SHOWWINDOW:
this->repaint();
break;
case WM_SIZE:
this->viewport(0, 0, LOWORD(lParam), HIWORD(lParam));
break;
case WM_PAINT:
this->OnWindowPaint();
break;
case WM_TIMER:
{
float t = tick_time();
if (OnTimer)OnTimer(tick - t);
tick = t;
}
break;
case WM_KEYDOWN:
if (OnKeyDown)OnKeyDown(int(wParam));
break;
case WM_KEYUP:
if (OnKeyUp)OnKeyUp(int(wParam));
break;
case WM_CHAR:
if (OnKeyPress)OnKeyPress(int(wParam));
break;
case WM_MOUSEMOVE:
if (OnMouseMove)OnMouseMove(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), int(wParam) & 0x13);
break;
case WM_LBUTTONDOWN:
if (OnMouseDown)OnMouseDown(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), EZ_LEFT);
break;
case WM_LBUTTONUP:
if (OnMouseUp)OnMouseUp(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), EZ_LEFT);
break;
case WM_RBUTTONDOWN:
if (OnMouseDown)OnMouseDown(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), EZ_RIGHT);
break;
case WM_RBUTTONUP:
if (OnMouseUp)OnMouseUp(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), EZ_RIGHT);
break;
case WM_MBUTTONDOWN:
if (OnMouseDown)OnMouseDown(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), EZ_MIDDLE);
break;
case WM_MBUTTONUP:
if (OnMouseUp)OnMouseUp(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), EZ_MIDDLE);
break;
default:
break;
}
if (prevWndProc) {
return CallWindowProc(prevWndProc, m_handle, Message, wParam, lParam);
}
else {
return ezWindow::wndproc(Message, wParam, lParam);
}
}
protected:
void gdiplusInit()
{
Gdiplus::GdiplusStartup(&token, &input, nullptr);
pen = new Gdiplus::Pen(Gdiplus::Color::Black);
brush = new Gdiplus::SolidBrush(Gdiplus::Color::White);
font = new Gdiplus::Font(fontName.c_str(), 12, Gdiplus::FontStyleRegular, Gdiplus::UnitPoint, nullptr);
font_color = new Gdiplus::SolidBrush(Gdiplus::Color::Black);
hdc = CreateCompatibleDC(nullptr);
}
void gdiplusShutdown()
{
closeGraphics();
delete_object(hdc);
detail::safe_delete(pen);
detail::safe_delete(brush);
detail::safe_delete(font);
detail::safe_delete(font_color);
Gdiplus::GdiplusShutdown(token);
}
void closeGraphics()
{
detail::safe_delete(g);
if (pixelbuf) {
delete_object(pixelbuf);
}
}
// 窗口重绘事件
void OnWindowPaint()
{
PAINTSTRUCT ps;
BeginPaint(m_handle, &ps);
if (OnPaint)OnPaint();
this->bitblt(ps.hdc);
EndPaint(m_handle, &ps);
}
};
template<typename T>
vgContext<T> vgContext<T>::instance = vgContext<T>();
// 屏幕更新线程
MINIVG_INLINE void updateThread(void* arg)
{
while (vgContext<>::instance.running) {
// 刷新窗口
vgContext<>::instance.repaint();
}
}
}// end namespace detail
//---------------------------------------------------------------------------
//
// 主函数
//
//---------------------------------------------------------------------------
MINIVG_INLINE int initgraph(const unistring& title, int width, int height, int param)
{
setlocale(LC_ALL, "chs"); // c 中文环境
std::locale::global(std::locale("chs")); // c++ 中文环境
// 保存创建参数
detail::vgContext<>::instance.initParam = param;
if (param & EZ_BACKBUFFER) {
detail::vgContext<>::instance.viewport(0, 0, width, height);
detail::vgContext<>::instance.winRect.left = 0;
detail::vgContext<>::instance.winRect.top = 0;
detail::vgContext<>::instance.winRect.right = width;
detail::vgContext<>::instance.winRect.bottom = height;
}
else {
// 获得屏幕大小
int cx = GetSystemMetrics(SM_CXFULLSCREEN);
int cy = GetSystemMetrics(SM_CYFULLSCREEN);
// 居中显示
if (width < cx) {
cx = (cx - width) / 2;
}
else {
width = cx;
cx = 0;
}
if (height < cy) {
cy = (cy - height) / 2;
}
else {
height = cy;
cy = 0;
}
// 设置窗口样式
DWORD dwStyle;