1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::Cursor;
use crate::controls::ControlHandle;
pub struct GlobalCursor;
impl GlobalCursor {
pub fn position() -> (i32, i32) {
use winapi::um::winuser::GetCursorPos;
use winapi::shared::windef::POINT;
let mut p = POINT{x: 0, y: 0};
unsafe { GetCursorPos(&mut p); }
(p.x as i32, p.y as i32)
}
pub fn local_position<C: Into<ControlHandle>>(control: C, point: Option<(i32, i32)>) -> (i32, i32) {
use winapi::shared::ntdef::LONG;
use winapi::shared::windef::POINT;
use winapi::um::winuser::ScreenToClient;
const MSG: &'static str = "local_position can only be used for window control";
let control = control.into();
if control.blank() { panic!(MSG); }
let handle = control.hwnd().expect(MSG);
let (x, y) = point.unwrap_or(GlobalCursor::position());
let mut p = POINT{x: x as LONG, y: y as LONG};
unsafe { ScreenToClient(handle, &mut p); }
(p.x as i32, p.y as i32)
}
pub fn set_position(x: i32, y: i32) {
use winapi::um::winuser::SetCursorPos;
use winapi::ctypes::c_int;
unsafe { SetCursorPos(x as c_int, y as c_int); }
}
pub fn set(cursor: &Cursor) {
use winapi::shared::windef::HCURSOR;
use winapi::um::winuser::SetCursor;
unsafe { SetCursor(cursor.handle as HCURSOR); }
}
pub fn get() -> Option<Cursor> {
use winapi::um::winuser::GetCursor;
use winapi::um::winnt::HANDLE;
let cursor = unsafe { GetCursor() };
match cursor.is_null() {
true => None,
false => Some( Cursor { handle: cursor as HANDLE, owned: false } )
}
}
pub fn set_capture(control: &ControlHandle) {
use winapi::um::winuser::SetCapture;
const MSG: &'static str = "Mouse capture can only be set for window control";
if control.blank() { panic!(MSG); }
let handle = control.hwnd().expect(MSG);
unsafe { SetCapture(handle); }
}
pub fn release() {
use winapi::um::winuser::ReleaseCapture;
unsafe{ ReleaseCapture(); }
}
pub fn capture() -> Option<ControlHandle> {
use winapi::um::winuser::GetCapture;
let cap = unsafe{ GetCapture() };
match cap.is_null() {
true => None,
false => Some(ControlHandle::Hwnd(cap))
}
}
pub fn dragging(control: &ControlHandle, point: Option<(i32, i32)>) -> bool {
use winapi::shared::ntdef::LONG;
use winapi::shared::windef::POINT;
use winapi::um::winuser::DragDetect;
const MSG: &'static str = "Dragging can only be set for window control";
if control.blank() { panic!(MSG); }
let handle = control.hwnd().expect(MSG);
let (x, y) = point.unwrap_or(GlobalCursor::position());
let c_point = POINT{x: x as LONG, y: y as LONG};
unsafe { DragDetect(handle, c_point) == 1 }
}
}