Skip to content

Instantly share code, notes, and snippets.

@clearlysid
Last active October 11, 2024 15:05
Show Gist options
  • Save clearlysid/6e4ff02b90ef9f7042ab06d5ef94963e to your computer and use it in GitHub Desktop.
Save clearlysid/6e4ff02b90ef9f7042ab06d5ef94963e to your computer and use it in GitHub Desktop.
get mouse cursor type on windows
use windows::Win32::UI::WindowsAndMessaging::{GetCursorInfo, LoadCursorW, CURSORINFO, HCURSOR};
// Different cursor types
use windows::Win32::UI::WindowsAndMessaging::{
IDC_ARROW, IDC_CROSS, IDC_HAND, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_SIZENESW,
IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT,
};
unsafe fn get_cursor_type_from_handle(cursor_handle: HCURSOR) -> &'static str {
let arrow_cursor = LoadCursorW(None, IDC_ARROW);
let ibeam_cursor = LoadCursorW(None, IDC_IBEAM);
let wait_cursor = LoadCursorW(None, IDC_WAIT);
let cross_cursor = LoadCursorW(None, IDC_CROSS);
let uparrow_cursor = LoadCursorW(None, IDC_UPARROW);
let sizeall_cursor = LoadCursorW(None, IDC_SIZEALL);
let sizenwse_cursor = LoadCursorW(None, IDC_SIZENWSE);
let sizenesw_cursor = LoadCursorW(None, IDC_SIZENESW);
let sizewe_cursor = LoadCursorW(None, IDC_SIZEWE);
let sizens_cursor = LoadCursorW(None, IDC_SIZENS);
let no_cursor = LoadCursorW(None, IDC_NO);
let hand_cursor = LoadCursorW(None, IDC_HAND);
if cursor_handle == arrow_cursor.expect("Failed to load arrow cursor") {
"Arrow"
} else if cursor_handle == ibeam_cursor.expect("Failed to load ibeam cursor") {
"IBeam"
} else if cursor_handle == wait_cursor.expect("Failed to load wait cursor") {
"Wait"
} else if cursor_handle == cross_cursor.expect("Failed to load cross cursor") {
"Cross"
} else if cursor_handle == uparrow_cursor.expect("Failed to load uparrow cursor") {
"UpArrow"
} else if cursor_handle == sizeall_cursor.expect("Failed to load sizeall cursor") {
"SizeAll"
} else if cursor_handle == sizenwse_cursor.expect("Failed to load sizenwse cursor") {
"SizeNWSE"
} else if cursor_handle == sizenesw_cursor.expect("Failed to load sizenesw cursor") {
"SizeNESW"
} else if cursor_handle == sizewe_cursor.expect("Failed to load sizewe cursor") {
"SizeWE"
} else if cursor_handle == sizens_cursor.expect("Failed to load sizens cursor") {
"SizeNS"
} else if cursor_handle == no_cursor.expect("Failed to load no cursor") {
"No"
} else if cursor_handle == hand_cursor.expect("Failed to load hand cursor") {
"Hand"
} else {
"Unknown"
}
}
pub unsafe fn get_current_cursor_type() -> String {
let mut cursor_info = CURSORINFO {
cbSize: std::mem::size_of::<CURSORINFO>() as u32,
..Default::default()
};
let cursor_type = if GetCursorInfo(&mut cursor_info as *mut CURSORINFO).is_ok() {
get_cursor_type_from_handle(cursor_info.hCursor)
} else {
"Unknown"
};
cursor_type.to_string()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment