Skip to main content

hwnd/um/winuser/structures/
wndclass.rs

1use crate::*;
2use bytemuck::*;
3use std::fmt::{self, Debug, Formatter};
4use std::ptr::*;
5
6
7
8pub type WndProcNonNull = unsafe extern "system" fn (hwnd: HWnd, msg: WM32, wparam: WPARAM, lparam: LPARAM) -> LRESULT;
9pub type WndProc        = Option<WndProcNonNull>;
10
11/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassa)\]
12/// WNDCLASSA
13///
14/// ### See Also
15/// *   [register_class_a]
16/// *   [About Window Classes](https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes)
17#[derive(Clone, Copy)]
18#[repr(C)] pub struct WndClassA<'a> {
19    pub style:      WindowStyle,
20    pub wnd_proc:   WndProc,
21    pub cls_extra:  i32,
22    pub wnd_extra:  i32,
23    pub hinstance:  HInstance<'static>,
24    pub hicon:      HIcon<'static>,
25    pub hcursor:    HCursor<'static>,
26    pub background: HBRUSH,     // TODO: lifetime bound handle?
27    pub menu_name:  Option<abistr::CStrNonNull<'a>>, // TODO: OrAtom types?
28    pub class_name: Option<abistr::CStrNonNull<'a>>, // TODO: OrAtom types?
29}
30
31/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassw)\]
32/// WNDCLASSW
33///
34/// ### See Also
35/// *   [register_class_w]
36/// *   [About Window Classes](https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes)
37#[derive(Clone, Copy)]
38#[repr(C)] pub struct WndClassW<'a> {
39    pub style:      WindowStyle,
40    pub wnd_proc:   WndProc,
41    pub cls_extra:  i32,
42    pub wnd_extra:  i32,
43    pub hinstance:  HInstance<'static>,
44    pub hicon:      HIcon<'static>,
45    pub hcursor:    HCursor<'static>,
46    pub background: HBRUSH,     // TODO: lifetime bound handle?
47    pub menu_name:  Option<abistr::CStrNonNull<'a, u16>>, // TODO: OrAtom types?
48    pub class_name: Option<abistr::CStrNonNull<'a, u16>>, // TODO: OrAtom types?
49}
50
51/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassexa)\]
52/// WNDCLASSEXA
53///
54/// ### See Also
55/// *   [register_class_ex_a]
56/// *   [About Window Classes](https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes)
57#[derive(Clone, Copy)]
58#[repr(C)] pub struct WndClassExA<'a> {
59    pub size:       u32,
60    pub style:      WindowStyle,
61    pub wnd_proc:   WndProc,
62    pub cls_extra:  i32,
63    pub wnd_extra:  i32,
64    pub hinstance:  HInstance<'static>,
65    pub hicon:      HIcon<'static>,
66    pub hcursor:    HCursor<'static>,
67    pub background: HBRUSH,     // TODO: lifetime bound handle?
68    pub menu_name:  Option<abistr::CStrNonNull<'a>>, // TODO: OrAtom types?
69    pub class_name: Option<abistr::CStrNonNull<'a>>, // TODO: OrAtom types?
70    pub hicon_sm:   HIcon<'static>,
71}
72
73/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassexw)\]
74/// WNDCLASSEXW
75///
76/// ### See Also
77/// *   [register_class_ex_w]
78/// *   [About Window Classes](https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes)
79#[derive(Clone, Copy)]
80#[repr(C)] pub struct WndClassExW<'a> {
81    pub size:       u32,
82    pub style:      WindowStyle,
83    pub wnd_proc:   WndProc,
84    pub cls_extra:  i32,
85    pub wnd_extra:  i32,
86    pub hinstance:  HInstance<'static>,
87    pub hicon:      HIcon<'static>,
88    pub hcursor:    HCursor<'static>,
89    pub background: HBRUSH,     // TODO: lifetime bound handle?
90    pub menu_name:  Option<abistr::CStrNonNull<'a, u16>>, // TODO: OrAtom types?
91    pub class_name: Option<abistr::CStrNonNull<'a, u16>>, // TODO: OrAtom types?
92    pub hicon_sm:   HIcon<'static>,
93}
94
95unsafe impl Zeroable for WndClassA<'_> {} // wnd_proc
96unsafe impl Zeroable for WndClassW<'_> {} // wnd_proc
97unsafe impl Zeroable for WndClassExA<'_> {} // wnd_proc
98unsafe impl Zeroable for WndClassExW<'_> {} // wnd_proc
99
100impl Default for WndClassA<'_>   { fn default() -> Self { Self::zeroed() } }
101impl Default for WndClassW<'_>   { fn default() -> Self { Self::zeroed() } }
102impl Default for WndClassExA<'_> { fn default() -> Self { Self { size: size_of_32::<Self>(), ..Self::zeroed() } } }
103impl Default for WndClassExW<'_> { fn default() -> Self { Self { size: size_of_32::<Self>(), ..Self::zeroed() } } }
104
105impl Debug for WndClassA<'_> {
106    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
107        fmt.debug_struct("WndClassA")
108            .field("style",         &self.style         )
109            .field("wnd_proc",      &self.wnd_proc.map_or(null::<()>(), |f| unsafe { std::mem::transmute(f) }))
110            .field("cls_extra",     &self.cls_extra     )
111            .field("wnd_extra",     &self.wnd_extra     )
112            .field("hinstance",     &self.hinstance     )
113            .field("hicon",         &self.hicon         )
114            .field("hcursor",       &self.hcursor       )
115            .field("background",    &self.background    )
116            .field("menu_name",     &self.menu_name     )
117            .field("class_name",    &self.class_name    )
118            .finish()
119    }
120}
121
122impl Debug for WndClassW<'_> {
123    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
124        fmt.debug_struct("WndClassW")
125            .field("style",         &self.style         )
126            .field("wnd_proc",      &self.wnd_proc.map_or(null::<()>(), |f| unsafe { std::mem::transmute(f) }))
127            .field("cls_extra",     &self.cls_extra     )
128            .field("wnd_extra",     &self.wnd_extra     )
129            .field("hinstance",     &self.hinstance     )
130            .field("hicon",         &self.hicon         )
131            .field("hcursor",       &self.hcursor       )
132            .field("background",    &self.background    )
133            .field("menu_name",     &self.menu_name     )
134            .field("class_name",    &self.class_name    )
135            .finish()
136    }
137}
138
139impl Debug for WndClassExA<'_> {
140    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
141        fmt.debug_struct("WndClassExA")
142            .field("size",          &self.size          )
143            .field("style",         &self.style         )
144            .field("wnd_proc",      &self.wnd_proc.map_or(null::<()>(), |f| unsafe { std::mem::transmute(f) }))
145            .field("cls_extra",     &self.cls_extra     )
146            .field("wnd_extra",     &self.wnd_extra     )
147            .field("hinstance",     &self.hinstance     )
148            .field("hicon",         &self.hicon         )
149            .field("hcursor",       &self.hcursor       )
150            .field("background",    &self.background    )
151            .field("menu_name",     &self.menu_name     )
152            .field("class_name",    &self.class_name    )
153            .field("hicon_sm",      &self.hicon_sm      )
154            .finish()
155    }
156}
157
158impl Debug for WndClassExW<'_> {
159    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
160        fmt.debug_struct("WndClassExW")
161            .field("size",          &self.size          )
162            .field("style",         &self.style         )
163            .field("wnd_proc",      &self.wnd_proc.map_or(null::<()>(), |f| unsafe { std::mem::transmute(f) }))
164            .field("cls_extra",     &self.cls_extra     )
165            .field("wnd_extra",     &self.wnd_extra     )
166            .field("hinstance",     &self.hinstance     )
167            .field("hicon",         &self.hicon         )
168            .field("hcursor",       &self.hcursor       )
169            .field("background",    &self.background    )
170            .field("menu_name",     &self.menu_name     )
171            .field("class_name",    &self.class_name    )
172            .field("hicon_sm",      &self.hicon_sm      )
173            .finish()
174    }
175}
176
177convert! {
178    WndClassA<'_>   => unsafe { winapi::um::winuser::WNDCLASSA },
179    WndClassW<'_>   => unsafe { winapi::um::winuser::WNDCLASSW },
180    WndClassExA<'_> => unsafe { winapi::um::winuser::WNDCLASSEXA },
181    WndClassExW<'_> => unsafe { winapi::um::winuser::WNDCLASSEXW },
182}