Skip to content

Commit 1f881ec

Browse files
committed
set_mouse_cursor via SetCursor API
1 parent 3467cb1 commit 1f881ec

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

src/win/cursor.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use winapi::{
2+
shared::ntdef::PCWSTR,
3+
um::winuser::{
4+
IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_WAIT,
5+
},
6+
};
7+
8+
use crate::MouseCursor;
9+
10+
impl MouseCursor {
11+
pub(crate) fn to_windows_cursor(self) -> PCWSTR {
12+
match self {
13+
MouseCursor::Default => IDC_ARROW,
14+
MouseCursor::Hand | MouseCursor::Pointer => IDC_HAND,
15+
MouseCursor::HandGrabbing
16+
| MouseCursor::Move
17+
| MouseCursor::ZoomIn
18+
| MouseCursor::ZoomOut
19+
| MouseCursor::AllScroll => IDC_SIZEALL,
20+
MouseCursor::Help => IDC_HELP,
21+
MouseCursor::Text | MouseCursor::VerticalText => IDC_IBEAM,
22+
MouseCursor::Working | MouseCursor::PtrWorking => IDC_WAIT,
23+
MouseCursor::NotAllowed | MouseCursor::PtrNotAllowed => IDC_NO,
24+
MouseCursor::Crosshair => IDC_CROSS,
25+
MouseCursor::EResize
26+
| MouseCursor::WResize
27+
| MouseCursor::EwResize
28+
| MouseCursor::ColResize => IDC_SIZEALL,
29+
MouseCursor::NResize
30+
| MouseCursor::SResize
31+
| MouseCursor::NsResize
32+
| MouseCursor::RowResize => IDC_SIZEALL,
33+
MouseCursor::NeResize | MouseCursor::SwResize | MouseCursor::NeswResize => IDC_SIZEALL,
34+
MouseCursor::NwResize | MouseCursor::SeResize | MouseCursor::NwseResize => IDC_SIZEALL,
35+
_ => IDC_ARROW,
36+
}
37+
}
38+
}

src/win/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod keyboard;
22
mod window;
3+
mod cursor;
34

45
pub use window::*;

src/win/window.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@ use winapi::shared::guiddef::GUID;
22
use winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM};
33
use winapi::shared::windef::{HWND, RECT};
44
use winapi::um::combaseapi::CoCreateGuid;
5-
use winapi::um::winuser::{
6-
AdjustWindowRectEx, CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW,
7-
GetDpiForWindow, GetMessageW, GetWindowLongPtrW, LoadCursorW, PostMessageW, RegisterClassW,
8-
ReleaseCapture, SetCapture, SetProcessDpiAwarenessContext, SetTimer, SetWindowLongPtrW,
9-
SetWindowPos, TranslateMessage, UnregisterClassW, CS_OWNDC, GET_XBUTTON_WPARAM, GWLP_USERDATA,
10-
IDC_ARROW, MSG, SWP_NOMOVE, SWP_NOZORDER, WHEEL_DELTA, WM_CHAR, WM_CLOSE, WM_CREATE,
11-
WM_DPICHANGED, WM_INPUTLANGCHANGE, WM_KEYDOWN, WM_KEYUP, WM_LBUTTONDOWN, WM_LBUTTONUP,
12-
WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_NCDESTROY, WM_RBUTTONDOWN,
13-
WM_RBUTTONUP, WM_SHOWWINDOW, WM_SIZE, WM_SYSCHAR, WM_SYSKEYDOWN, WM_SYSKEYUP, WM_TIMER,
14-
WM_USER, WM_XBUTTONDOWN, WM_XBUTTONUP, WNDCLASSW, WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS,
15-
WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, WS_VISIBLE, XBUTTON1, XBUTTON2,
16-
};
5+
use winapi::um::winuser::{AdjustWindowRectEx, CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, GetDpiForWindow, GetMessageW, GetWindowLongPtrW, LoadCursorW, PostMessageW, RegisterClassW, ReleaseCapture, SetCapture, SetProcessDpiAwarenessContext, SetTimer, SetWindowLongPtrW, SetWindowPos, TranslateMessage, UnregisterClassW, CS_OWNDC, GET_XBUTTON_WPARAM, GWLP_USERDATA, IDC_ARROW, MSG, SWP_NOMOVE, SWP_NOZORDER, WHEEL_DELTA, WM_CHAR, WM_CLOSE, WM_CREATE, WM_DPICHANGED, WM_INPUTLANGCHANGE, WM_KEYDOWN, WM_KEYUP, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_NCDESTROY, WM_RBUTTONDOWN, WM_RBUTTONUP, WM_SHOWWINDOW, WM_SIZE, WM_SYSCHAR, WM_SYSKEYDOWN, WM_SYSKEYUP, WM_TIMER, WM_USER, WM_XBUTTONDOWN, WM_XBUTTONUP, WNDCLASSW, WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, WS_VISIBLE, XBUTTON1, XBUTTON2, SetCursor};
6+
7+
178

189
use std::cell::RefCell;
1910
use std::ffi::{c_void, OsStr};
@@ -29,7 +20,7 @@ const BV_WINDOW_MUST_CLOSE: UINT = WM_USER + 1;
2920

3021
use crate::{
3122
Event, MouseButton, MouseEvent, PhyPoint, PhySize, ScrollDelta, WindowEvent, WindowHandler,
32-
WindowInfo, WindowOpenOptions, WindowScalePolicy,
23+
WindowInfo, WindowOpenOptions, WindowScalePolicy, MouseCursor,
3324
};
3425

3526
use super::keyboard::KeyboardState;
@@ -350,7 +341,7 @@ unsafe fn register_wnd_class() -> ATOM {
350341
cbClsExtra: 0,
351342
cbWndExtra: 0,
352343
hIcon: null_mut(),
353-
hCursor: LoadCursorW(null_mut(), IDC_ARROW),
344+
hCursor: null_mut(), // If the class cursor is not NULL, the system restores the class cursor each time the mouse is moved.
354345
hbrBackground: null_mut(),
355346
lpszMenuName: null_mut(),
356347
};
@@ -602,7 +593,10 @@ impl Window {
602593
}
603594

604595
pub fn set_mouse_cursor(&mut self, cursor: MouseCursor) {
605-
//@TODO: Implement
596+
unsafe {
597+
let cursor = LoadCursorW(null_mut(), cursor.to_windows_cursor());
598+
SetCursor(cursor);
599+
}
606600
}
607601

608602
pub fn close(&mut self) {

0 commit comments

Comments
 (0)