-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhello-world.swift
60 lines (51 loc) · 1.53 KB
/
hello-world.swift
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
import WinSDK
let hInstance = GetModuleHandleW(nil)
extension LPCWSTR: ExpressibleByStringLiteral, ExpressibleByExtendedGraphemeClusterLiteral, ExpressibleByUnicodeScalarLiteral {
public init(stringLiteral: String) {
var arr = Array<UInt16>(stringLiteral.utf16)
arr.append(0)
// TODO: たぶんこれメモリリークする
let ptr = UnsafeMutablePointer<UInt16>.allocate(capacity: arr.count)
ptr.initialize(from: &arr, count: arr.count)
self.init(ptr)
}
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
}
func windowProc(hWnd: HWND?, msg: UINT, wParam: WPARAM, lParam: LPARAM) -> LRESULT {
switch msg {
case UINT(WM_DESTROY):
PostQuitMessage(0)
return 0
default:
return DefWindowProcW(hWnd, msg, wParam, lParam)
}
}
let WINDOW_CLASS_NAME: LPCWSTR = "HelloWorldWindow"
var windowClass = WNDCLASSW()
windowClass.lpfnWndProc = windowProc
windowClass.hInstance = hInstance
windowClass.lpszClassName = WINDOW_CLASS_NAME
windowClass.hbrBackground = .init(OpaquePointer(GetStockObject(WHITE_BRUSH)))
RegisterClassW(&windowClass)
let hWnd = CreateWindowExW(
0,
WINDOW_CLASS_NAME,
"Hello windows world from Swift!! 日本語もオッケー👌!!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
nil,
nil,
hInstance,
nil
)
ShowWindow(hWnd, SW_SHOW);
var msg = MSG()
while GetMessageW(&msg, nil, 0, 0) {
TranslateMessage(&msg)
DispatchMessageW(&msg)
}