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
184
185
use std::ptr;
use std::mem;
use std::hash::Hash;
use winapi::{HWND, WNDPROC, DWORD, GWL_USERDATA};
use ui::UiInner;
use low::other_helper::to_utf16;
use error::SystemError;
pub struct SysclassParams<S: Into<String>> {
pub class_name: S,
pub sysproc: WNDPROC
}
pub struct WindowParams<S1: Into<String>, S2: Into<String>> {
pub title: S1,
pub class_name: S2,
pub position: (i32, i32),
pub size: (u32, u32),
pub flags: DWORD
}
pub unsafe fn build_sysclass<S: Into<String>>(p: SysclassParams<S>) -> Result<(), SystemError> {
use kernel32::{GetModuleHandleW, GetLastError};
use user32::{LoadCursorW, RegisterClassExW};
use winapi::{WNDCLASSEXW, CS_HREDRAW, CS_VREDRAW, IDC_ARROW, COLOR_WINDOW, HBRUSH, UINT, ERROR_CLASS_ALREADY_EXISTS};
let hmod = GetModuleHandleW(ptr::null_mut());
if hmod.is_null() { return Err(SystemError::SystemClassCreation); }
let class_name = to_utf16(p.class_name.into().as_ref());
let class =
WNDCLASSEXW {
cbSize: mem::size_of::<WNDCLASSEXW>() as UINT,
style: CS_HREDRAW | CS_VREDRAW,
lpfnWndProc: p.sysproc,
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: hmod,
hIcon: ptr::null_mut(),
hCursor: LoadCursorW(ptr::null_mut(), IDC_ARROW),
hbrBackground: mem::transmute(COLOR_WINDOW as HBRUSH),
lpszMenuName: ptr::null(),
lpszClassName: class_name.as_ptr(),
hIconSm: ptr::null_mut()
};
let class_token = RegisterClassExW(&class);
if class_token == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS {
Err(SystemError::SystemClassCreation)
} else {
Ok(())
}
}
pub unsafe fn build_window<S1: Into<String>, S2: Into<String>>(p: WindowParams<S1, S2>) -> Result<HWND, SystemError>{
use kernel32::GetModuleHandleW;
use user32::CreateWindowExW;
use winapi::{WS_EX_COMPOSITED};
let hmod = GetModuleHandleW(ptr::null_mut());
if hmod.is_null() { return Err(SystemError::WindowCreationFail); }
let class_name = to_utf16(p.class_name.into().as_ref());
let window_name = to_utf16(p.title.into().as_ref());
let handle = CreateWindowExW (
WS_EX_COMPOSITED,
class_name.as_ptr(), window_name.as_ptr(),
p.flags,
p.position.0, p.position.1,
p.size.0 as i32, p.size.1 as i32,
ptr::null_mut(),
ptr::null_mut(),
hmod,
ptr::null_mut()
);
if handle.is_null() {
Err(SystemError::WindowCreationFail)
} else {
Ok(handle)
}
}
#[allow(unused_variables)]
pub unsafe fn list_window_children<ID: Clone+Hash>(handle: HWND, ui: *mut UiInner<ID>) -> Vec<u64> {
use user32::GetMenu;
use low::menu_helper::list_menu_children;
let mut children = Vec::new();
let menu = GetMenu(handle);
if !menu.is_null() {
children.append(&mut list_menu_children(menu) );
}
children
}
#[cfg(target_arch = "x86")] use winapi::LONG;
#[cfg(target_arch = "x86_64")] use winapi::LONG_PTR;
#[inline(always)]
#[cfg(target_arch = "x86_64")]
pub fn get_window_long(handle: HWND) -> LONG_PTR {
use user32::GetWindowLongPtrW;
unsafe{ GetWindowLongPtrW(handle, GWL_USERDATA) }
}
#[inline(always)]
#[cfg(target_arch = "x86")]
pub fn get_window_long(handle: HWND) -> LONG {
use user32::GetWindowLongW;
unsafe { GetWindowLongW(handle, GWL_USERDATA) }
}
#[inline(always)]
#[cfg(target_arch = "x86_64")]
pub fn set_window_long(handle: HWND, v: usize) {
use user32::SetWindowLongPtrW;
unsafe{ SetWindowLongPtrW(handle, GWL_USERDATA, v as LONG_PTR); }
}
#[inline(always)]
#[cfg(target_arch = "x86")]
pub fn set_window_long(handle: HWND, v: usize) {
use user32::SetWindowLongW;
unsafe { SetWindowLongW(handle, GWL_USERDATA, v as LONG); }
}