Skip to content

Commit

Permalink
set_mouse_cursor via SetCursor API
Browse files Browse the repository at this point in the history
  • Loading branch information
discordance committed Sep 3, 2023
1 parent 2134538 commit 5744a2b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
38 changes: 38 additions & 0 deletions src/win/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use winapi::{
shared::ntdef::PCWSTR,
um::winuser::{
IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_HELP, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_WAIT,
},
};

use crate::MouseCursor;

impl MouseCursor {
pub(crate) fn to_windows_cursor(self) -> PCWSTR {
match self {
MouseCursor::Default => IDC_ARROW,
MouseCursor::Hand | MouseCursor::Pointer => IDC_HAND,
MouseCursor::HandGrabbing
| MouseCursor::Move
| MouseCursor::ZoomIn
| MouseCursor::ZoomOut
| MouseCursor::AllScroll => IDC_SIZEALL,
MouseCursor::Help => IDC_HELP,
MouseCursor::Text | MouseCursor::VerticalText => IDC_IBEAM,
MouseCursor::Working | MouseCursor::PtrWorking => IDC_WAIT,
MouseCursor::NotAllowed | MouseCursor::PtrNotAllowed => IDC_NO,
MouseCursor::Crosshair => IDC_CROSS,
MouseCursor::EResize
| MouseCursor::WResize
| MouseCursor::EwResize
| MouseCursor::ColResize => IDC_SIZEALL,
MouseCursor::NResize
| MouseCursor::SResize
| MouseCursor::NsResize
| MouseCursor::RowResize => IDC_SIZEALL,
MouseCursor::NeResize | MouseCursor::SwResize | MouseCursor::NeswResize => IDC_SIZEALL,
MouseCursor::NwResize | MouseCursor::SeResize | MouseCursor::NwseResize => IDC_SIZEALL,
_ => IDC_ARROW,
}
}
}
1 change: 1 addition & 0 deletions src/win/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod drop_target;
mod keyboard;
mod window;
mod cursor;

pub use window::*;
11 changes: 7 additions & 4 deletions src/win/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winapi::um::ole2::{RegisterDragDrop, OleInitialize, RevokeDragDrop};
use winapi::um::oleidl::LPDROPTARGET;
use winapi::um::winuser::{
AdjustWindowRectEx, CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW,
GetDpiForWindow, GetMessageW, GetWindowLongPtrW, LoadCursorW, PostMessageW, RegisterClassW,
GetDpiForWindow, GetMessageW, GetWindowLongPtrW, LoadCursorW, SetCursor, 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,
Expand All @@ -31,7 +31,7 @@ use raw_window_handle::{HasRawWindowHandle, RawWindowHandle, Win32Handle};
const BV_WINDOW_MUST_CLOSE: UINT = WM_USER + 1;

use crate::{
Event, MouseButton, MouseEvent, PhyPoint, PhySize, ScrollDelta, Size, WindowEvent,
Event, MouseButton, MouseEvent, PhyPoint, PhySize, ScrollDelta, Size, MouseCursor, WindowEvent,
WindowHandler, WindowInfo, WindowOpenOptions, WindowScalePolicy,
};

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

pub fn set_mouse_cursor(&mut self, cursor: MouseCursor) {
//@TODO: Implement
unsafe {
let cursor = LoadCursorW(null_mut(), cursor.to_windows_cursor());
SetCursor(cursor);
}
}

pub fn close(&mut self) {
Expand Down

0 comments on commit 5744a2b

Please sign in to comment.