Skip to content

Commit 5744a2b

Browse files
committed
set_mouse_cursor via SetCursor API
1 parent 2134538 commit 5744a2b

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
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,5 +1,6 @@
11
mod drop_target;
22
mod keyboard;
33
mod window;
4+
mod cursor;
45

56
pub use window::*;

src/win/window.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use winapi::um::ole2::{RegisterDragDrop, OleInitialize, RevokeDragDrop};
66
use winapi::um::oleidl::LPDROPTARGET;
77
use winapi::um::winuser::{
88
AdjustWindowRectEx, CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW,
9-
GetDpiForWindow, GetMessageW, GetWindowLongPtrW, LoadCursorW, PostMessageW, RegisterClassW,
9+
GetDpiForWindow, GetMessageW, GetWindowLongPtrW, LoadCursorW, SetCursor, PostMessageW, RegisterClassW,
1010
ReleaseCapture, SetCapture, SetProcessDpiAwarenessContext, SetTimer, SetWindowLongPtrW,
1111
SetWindowPos, TranslateMessage, UnregisterClassW, CS_OWNDC, GET_XBUTTON_WPARAM, GWLP_USERDATA,
1212
IDC_ARROW, MSG, SWP_NOMOVE, SWP_NOZORDER, WHEEL_DELTA, WM_CHAR, WM_CLOSE, WM_CREATE,
@@ -31,7 +31,7 @@ use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, Win32Handle};
3131
const BV_WINDOW_MUST_CLOSE: UINT = WM_USER + 1;
3232

3333
use crate::{
34-
Event, MouseButton, MouseEvent, PhyPoint, PhySize, ScrollDelta, Size, WindowEvent,
34+
Event, MouseButton, MouseEvent, PhyPoint, PhySize, ScrollDelta, Size, MouseCursor, WindowEvent,
3535
WindowHandler, WindowInfo, WindowOpenOptions, WindowScalePolicy,
3636
};
3737

@@ -433,7 +433,7 @@ unsafe fn register_wnd_class() -> ATOM {
433433
cbClsExtra: 0,
434434
cbWndExtra: 0,
435435
hIcon: null_mut(),
436-
hCursor: LoadCursorW(null_mut(), IDC_ARROW),
436+
hCursor: null_mut(), // If the class cursor is not NULL, the system restores the class cursor each time the mouse is moved.
437437
hbrBackground: null_mut(),
438438
lpszMenuName: null_mut(),
439439
};
@@ -761,7 +761,10 @@ impl Window<'_> {
761761
}
762762

763763
pub fn set_mouse_cursor(&mut self, cursor: MouseCursor) {
764-
//@TODO: Implement
764+
unsafe {
765+
let cursor = LoadCursorW(null_mut(), cursor.to_windows_cursor());
766+
SetCursor(cursor);
767+
}
765768
}
766769

767770
pub fn close(&mut self) {

0 commit comments

Comments
 (0)