1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: MIT |
3 | |
4 | #include <windows.h> |
5 | #include "appview.h" |
6 | #include <memory> |
7 | |
8 | #define THE_BUTTON_ID 101 |
9 | |
10 | static std::unique_ptr<AppView> app; |
11 | |
12 | extern "C" static LRESULT WindowProc(HWND h, UINT msg, WPARAM wp, LPARAM lp) |
13 | { |
14 | switch (msg) { |
15 | /* Add a win32 push button and do something when it's clicked. */ |
16 | case WM_CREATE: { |
17 | HWND hbutton = CreateWindow( |
18 | "BUTTON" , "Hey There" , /* class and title */ |
19 | WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, /* style */ |
20 | 0, 0, 100, 30, /* position */ |
21 | h, /* parent */ |
22 | (HMENU)THE_BUTTON_ID, /* unique (within the application) integer identifier */ |
23 | GetModuleHandle(0), 0 /* GetModuleHandle(0) gets the hinst */ |
24 | ); |
25 | app = std::make_unique<AppView>(); |
26 | app->attachToWindow(h); |
27 | } break; |
28 | |
29 | case WM_SIZE: { |
30 | UINT width = LOWORD(lp); |
31 | UINT height = HIWORD(lp); |
32 | if (app) |
33 | app->setGeometry(x: 0, y: 40, width: width, height: height - 40); |
34 | } break; |
35 | |
36 | case WM_COMMAND: { |
37 | switch (LOWORD(wp)) { |
38 | case THE_BUTTON_ID: |
39 | app = nullptr; |
40 | PostQuitMessage(0); |
41 | break; |
42 | default:; |
43 | } |
44 | } break; |
45 | |
46 | case WM_CLOSE: |
47 | app = nullptr; |
48 | PostQuitMessage(0); |
49 | break; |
50 | default: |
51 | return DefWindowProc(h, msg, wp, lp); |
52 | } |
53 | return 0; |
54 | } |
55 | |
56 | extern "C" int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPSTR cmdline, int show) |
57 | { |
58 | if (!hprev) { |
59 | WNDCLASS c = { 0 }; |
60 | c.lpfnWndProc = (WNDPROC)WindowProc; |
61 | c.hInstance = hinst; |
62 | c.hIcon = LoadIcon(0, IDI_APPLICATION); |
63 | c.hCursor = LoadCursor(0, IDC_ARROW); |
64 | c.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
65 | c.lpszClassName = "MainWindow" ; |
66 | RegisterClass(&c); |
67 | } |
68 | |
69 | HWND h = CreateWindow("MainWindow" , /* window class name*/ |
70 | "WindowTitle" , /* title */ |
71 | WS_OVERLAPPEDWINDOW, /* style */ |
72 | CW_USEDEFAULT, CW_USEDEFAULT, /* position */ |
73 | CW_USEDEFAULT, CW_USEDEFAULT, /* size */ |
74 | 0, /* parent */ |
75 | 0, /* menu */ |
76 | hinst, 0 /* lparam */ |
77 | ); |
78 | |
79 | ShowWindow(h, show); |
80 | |
81 | while (1) { /* or while(running) */ |
82 | MSG msg; |
83 | while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { |
84 | if (msg.message == WM_QUIT) |
85 | return (int)msg.wParam; |
86 | TranslateMessage(&msg); |
87 | DispatchMessage(&msg); |
88 | } |
89 | } |
90 | |
91 | return 0; |
92 | } |
93 | |