-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.dart
91 lines (75 loc) · 2.48 KB
/
app.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
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Basic Petzoldian "hello world" Win32 app
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_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
final ps = calloc<PAINTSTRUCT>();
final hdc = BeginPaint(hWnd, ps);
final rect = calloc<RECT>();
final msg = TEXT('Hello, Dart!');
GetClientRect(hWnd, rect);
DrawText(hdc, msg, -1, rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, ps);
free(ps);
free(rect);
free(msg);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
// An optional approach to launching a GUI app that lets you use a more
// traditional WinMain entry point, rather than having to manually retrieve the
// hInstance and nShowCmd parameters.
void main() => initApp(winMain);
void winMain(int hInstance, List<String> args, int nShowCmd) {
// Register the window class.
final className = TEXT('Sample Window Class');
final wc = calloc<WNDCLASS>()
..ref.style = CS_HREDRAW | CS_VREDRAW
..ref.lpfnWndProc = Pointer.fromFunction<WindowProc>(mainWindowProc, 0)
..ref.hInstance = hInstance
..ref.lpszClassName = className
..ref.hCursor = LoadCursor(NULL, IDC_ARROW)
..ref.hbrBackground = GetStockObject(WHITE_BRUSH);
RegisterClass(wc);
// Create the window.
final windowCaption = TEXT('Dart Native Win32 window');
final hWnd = CreateWindowEx(
0, // Optional window styles.
className, // Window class
windowCaption, // Window caption
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
nullptr // Additional application data
);
free(windowCaption);
free(className);
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);
}
free(msg);
}