-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
customwin.dart
118 lines (95 loc) · 2.89 KB
/
customwin.dart
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
// Draw a circular window
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
int mainWindowProc(int hWnd, int uMsg, int wParam, int lParam) {
switch (uMsg) {
case WM_CREATE:
final hRgn = CreateEllipticRgn(0, 0, 500, 500);
SetWindowRgn(hWnd, hRgn, TRUE);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_NCHITTEST:
// A click on the client area is treated as a click on the title bar
if (DefWindowProc(hWnd, uMsg, wParam, lParam) == HTCLIENT) {
return HTCAPTION;
}
return 0;
case WM_PAINT:
final ps = calloc<PAINTSTRUCT>();
final hdc = BeginPaint(hWnd, ps);
final hPen = CreatePen(PEN_STYLE.PS_SOLID, 4, RGB(64, 64, 64));
final hPrevPen = SelectObject(hdc, hPen);
Ellipse(hdc, 0, 0, 499, 499);
SelectObject(hdc, hPrevPen);
DeleteObject(hPen);
final rect = calloc<RECT>();
final msg = TEXT('What a strange window!');
GetClientRect(hWnd, rect);
DrawText(
hdc,
msg,
-1,
rect,
DRAW_TEXT_FORMAT.DT_CENTER |
DRAW_TEXT_FORMAT.DT_VCENTER |
DRAW_TEXT_FORMAT.DT_SINGLELINE);
EndPaint(hWnd, ps);
free(rect);
free(msg);
free(ps);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void main() => initApp(winMain);
void winMain(int hInstance, List<String> args, int nShowCmd) {
// Register the window class.
final className = TEXT('Sample Window Class');
final lpfnWndProc = NativeCallable<WNDPROC>.isolateLocal(
mainWindowProc,
exceptionalReturn: 0,
);
final wc = calloc<WNDCLASS>()
..ref.style = WNDCLASS_STYLES.CS_HREDRAW | WNDCLASS_STYLES.CS_VREDRAW
..ref.lpfnWndProc = lpfnWndProc.nativeFunction
..ref.hInstance = hInstance
..ref.lpszClassName = className
..ref.hCursor = LoadCursor(NULL, IDC_ARROW)
..ref.hbrBackground = GetStockObject(GET_STOCK_OBJECT_FLAGS.WHITE_BRUSH);
RegisterClass(wc);
// Create the window.
final hWnd = CreateWindowEx(
0, // Optional window styles.
className, // Window class
nullptr, // Window caption
WINDOW_STYLE.WS_BORDER, // Window style
// Size and position
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
500,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
nullptr // Additional application data
);
// Remove the title bar
SetWindowLongPtr(hWnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE, 0);
if (hWnd == 0) {
final error = GetLastError();
throw WindowsException(HRESULT_FROM_WIN32(error));
}
ShowWindow(hWnd, nShowCmd);
UpdateWindow(hWnd);
// Run the message loop.
final msg = calloc<MSG>();
while (GetMessage(msg, NULL, 0, 0) != 0) {
TranslateMessage(msg);
DispatchMessage(msg);
}
lpfnWndProc.close();
free(className);
}