-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin32_window.cpp
More file actions
113 lines (94 loc) · 3.21 KB
/
win32_window.cpp
File metadata and controls
113 lines (94 loc) · 3.21 KB
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
#include "win32_window.h"
#include "../runtime/runtime_impl.h"
#include <windowsx.h> // For GET_X_LPARAM, GET_Y_LPARAM
#include <string> // For std::to_string
#include <iostream> // For std::cout
static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
// 从窗口用户数据获取 Win32Window 指针
Win32Window* window = reinterpret_cast<Win32Window*>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
if (window) {
// 处理窗口大小改变
if (msg == WM_SIZE) {
window->width = LOWORD(lp);
window->height = HIWORD(lp);
window->resized = true;
}
// 处理输入事件
if (window->inputImpl) {
switch (msg) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
window->inputImpl->setKeyState((int)wp, true);
break;
case WM_KEYUP:
case WM_SYSKEYUP:
window->inputImpl->setKeyState((int)wp, false);
break;
case WM_LBUTTONDOWN:
window->inputImpl->setMouseButton(0, true);
break;
case WM_LBUTTONUP:
window->inputImpl->setMouseButton(0, false);
break;
case WM_RBUTTONDOWN:
window->inputImpl->setMouseButton(1, true);
break;
case WM_RBUTTONUP:
window->inputImpl->setMouseButton(1, false);
break;
case WM_MBUTTONDOWN:
window->inputImpl->setMouseButton(2, true);
break;
case WM_MBUTTONUP:
window->inputImpl->setMouseButton(2, false);
break;
case WM_MOUSEMOVE: {
int x = GET_X_LPARAM(lp);
int y = GET_Y_LPARAM(lp);
window->inputImpl->setMousePosition((float)x, (float)y);
break;
}
case WM_MOUSEWHEEL: {
// 获取滚轮增量 (WHEEL_DELTA = 120)
int delta = GET_WHEEL_DELTA_WPARAM(wp);
float normalizedDelta = (float)delta / WHEEL_DELTA;
window->inputImpl->setMouseScrollDelta(normalizedDelta);
break;
}
}
}
}
// 阻止右键上下文菜单,让右键可以用于拖动
if (msg == WM_CONTEXTMENU) {
return 0; // 不显示上下文菜单
}
if (msg == WM_DESTROY) {
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wp, lp);
}
bool Win32Window::create(const wchar_t* title) {
hInstance = GetModuleHandleW(nullptr);
WNDCLASSW wc{};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"BitUIWin";
if (!RegisterClassW(&wc)) return false;
hWnd = CreateWindowExW(0, wc.lpszClassName, title, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
nullptr, nullptr, hInstance, nullptr);
if (!hWnd) return false;
// 将 Win32Window 指针存储到窗口用户数据中
SetWindowLongPtrW(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
ShowWindow(hWnd, SW_SHOW);
return true;
}
void Win32Window::loop(bool& running) {
MSG msg{};
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) { running = false; }
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}