util.rs

 1use windows::Win32::{
 2    Foundation::{HWND, LPARAM, WPARAM},
 3    UI::WindowsAndMessaging::{
 4        GetWindowLongPtrW, GetWindowLongW, SetWindowLongPtrW, SetWindowLongW, WINDOW_LONG_PTR_INDEX,
 5    },
 6};
 7
 8pub(crate) trait HiLoWord {
 9    fn hiword(&self) -> u16;
10    fn loword(&self) -> u16;
11    fn signed_hiword(&self) -> i16;
12    fn signed_loword(&self) -> i16;
13}
14
15impl HiLoWord for WPARAM {
16    fn hiword(&self) -> u16 {
17        ((self.0 >> 16) & 0xFFFF) as u16
18    }
19
20    fn loword(&self) -> u16 {
21        (self.0 & 0xFFFF) as u16
22    }
23
24    fn signed_hiword(&self) -> i16 {
25        ((self.0 >> 16) & 0xFFFF) as i16
26    }
27
28    fn signed_loword(&self) -> i16 {
29        (self.0 & 0xFFFF) as i16
30    }
31}
32
33impl HiLoWord for LPARAM {
34    fn hiword(&self) -> u16 {
35        ((self.0 >> 16) & 0xFFFF) as u16
36    }
37
38    fn loword(&self) -> u16 {
39        (self.0 & 0xFFFF) as u16
40    }
41
42    fn signed_hiword(&self) -> i16 {
43        ((self.0 >> 16) & 0xFFFF) as i16
44    }
45
46    fn signed_loword(&self) -> i16 {
47        (self.0 & 0xFFFF) as i16
48    }
49}
50
51pub(crate) unsafe fn get_window_long(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX) -> isize {
52    #[cfg(target_pointer_width = "64")]
53    unsafe {
54        GetWindowLongPtrW(hwnd, nindex)
55    }
56    #[cfg(target_pointer_width = "32")]
57    unsafe {
58        GetWindowLongW(hwnd, nindex) as isize
59    }
60}
61
62pub(crate) unsafe fn set_window_long(
63    hwnd: HWND,
64    nindex: WINDOW_LONG_PTR_INDEX,
65    dwnewlong: isize,
66) -> isize {
67    #[cfg(target_pointer_width = "64")]
68    unsafe {
69        SetWindowLongPtrW(hwnd, nindex, dwnewlong)
70    }
71    #[cfg(target_pointer_width = "32")]
72    unsafe {
73        SetWindowLongW(hwnd, nindex, dwnewlong as i32) as isize
74    }
75}