Skip to content

Commit 04ed1dc

Browse files
author
Dmitry_Milk
committed
Hold cursor at center of menu
1 parent 49c3879 commit 04ed1dc

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

ContextMenuTest.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,14 @@ BOOL InitInstance(HINSTANCE, int);
387387
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
388388
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
389389

390+
void SetCursorWindowPos(HWND hwnd, int x, int y) {
391+
POINT pos;
392+
pos.x = x;
393+
pos.y = y;
394+
ClientToScreen(hwnd, &pos);
395+
SetCursorPos(pos.x, pos.y);
396+
}
397+
390398
[[noreturn]] void winapi_failure() {
391399
DWORD errCode = GetLastError();
392400
LPWSTR buffer = nullptr;
@@ -806,7 +814,6 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
806814
if (mode == Mode::Clicked) {
807815
mode = Mode::PressedAgain;
808816
} else {
809-
mode = Mode::Pressed;
810817
center_point.x = GET_X_LPARAM(lparam);
811818
center_point.y = GET_Y_LPARAM(lparam);
812819
RECT client_rect;
@@ -826,6 +833,11 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
826833
center_point.y = cy - params.min_window_margin;
827834
}
828835
menu_state.reset();
836+
837+
SetCursorWindowPos(hwnd, center_point.x, center_point.y);
838+
839+
mode = Mode::Pressed;
840+
829841
InvalidateRect(hwnd, nullptr, FALSE);
830842
}
831843
return 0;
@@ -878,21 +890,22 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
878890
}
879891
return 0;
880892
}
881-
case WM_INPUT:
893+
case WM_MOUSEMOVE:
882894
{
883-
UINT dwSize;
884-
RAWINPUT raw;
885-
GetRawInputData((HRAWINPUT)lparam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
886-
if (dwSize > sizeof(raw)) {
887-
printf("Raw input data is too large\n");
888-
return 0;
889-
}
890-
GetRawInputData((HRAWINPUT)lparam, RID_INPUT, &raw, &dwSize, sizeof(RAWINPUTHEADER));
891-
if (raw.header.dwType == RIM_TYPEMOUSE) {
892-
RAWMOUSE& mouse = raw.data.mouse;
893-
menu_state.apply_delta(vec2{(float)mouse.lLastX, (float)mouse.lLastY});
894-
InvalidateRect(hwnd, nullptr, FALSE);
895+
if (mode != Mode::Disabled) {
896+
int dx = GET_X_LPARAM(lparam) - center_point.x;
897+
int dy = GET_Y_LPARAM(lparam) - center_point.y;
898+
899+
if (dx != 0 || dy != 0) {
900+
menu_state.apply_delta(vec2{dx / display_scale, dy / display_scale});
901+
// menu_state.apply_delta(vec2{(float)dx, (float)dy});
902+
903+
SetCursorWindowPos(hwnd, center_point.x, center_point.y);
904+
905+
InvalidateRect(hwnd, nullptr, FALSE);
906+
}
895907
}
908+
896909
return 0;
897910
}
898911
default:

0 commit comments

Comments
 (0)