hwnd/um/winuser/functions/
create_window_.rs

1use crate::*;
2use abistr::{TryIntoAsOptCStr, AsOptCStr};
3use winapi::um::winuser::*;
4use std::ffi::c_void;
5
6
7
8/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexa)\]
9/// CreateWindowA
10///
11/// Creates an overlapped, pop-up, or child window.
12///
13/// ### Safety
14/// The underlying window classes involved may impose soundness requirements on:
15/// *   `param`     Might need to be a valid reference to a specific type, might need to be non-null
16/// *   `parent`    Might not tolerate children, or the class might not tolerate having a parent
17/// *   `hmenu`     Might need to be valid
18/// *   undocumented global or thread local state
19///
20/// ### Examples
21/// ```rust
22/// # use hwnd::*;
23/// # use std::ptr::*;
24/// unsafe {
25///
26///     let mw = create_window_a(
27///         abistr::cstr!("Message"), (), 0,
28///         0, 0, 0, 0,
29///         HWnd::MESSAGE, null_mut(), None, null_mut()
30///     ).unwrap();
31///
32///     destroy_window(mw).unwrap();
33///
34/// }
35/// ```
36pub unsafe fn create_window_a<'a>(
37    class_name:     impl Into<NameOrAtom<'a, u8>>,
38    window_name:    impl TryIntoAsOptCStr,
39    style:          impl Into<WindowStyle>,
40    x:              i32,
41    y:              i32,
42    width:          i32,
43    height:         i32,
44    parent:         impl TryInto<HWnd>,
45    hmenu:          HMENU,
46    hinstance:      impl Into<HInstance<'static>>,
47    param:          *mut c_void,
48) -> Result<HWnd, Error> {
49    fn_context!(create_window_a => CreateWindowA);
50    let parent      = parent        .try_into().map_err(|_| fn_param_error!(parent,         ERROR::INVALID_WINDOW_HANDLE))?.into();
51    let window_name = window_name   .try_into().map_err(|_| fn_param_error!(window_name,    ERROR::INVALID_WINDOW_HANDLE))?;
52    let hwnd = unsafe { CreateWindowExA(
53        0, class_name.into().as_atom_or_cstr_ptr(), window_name.as_opt_cstr(), style.into().into(),
54        x, y, width, height,
55        parent, hmenu, hinstance.into().into(), param,
56    )};
57    fn_succeeded!(!hwnd.is_null())?;
58    Ok(hwnd.into())
59}
60
61/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw)\]
62/// CreateWindowW
63///
64/// Creates an overlapped, pop-up, or child window.
65///
66/// ### Safety
67/// The underlying window classes involved may impose soundness requirements on:
68/// *   `param`     Might need to be a valid reference to a specific type, might need to be non-null
69/// *   `parent`    Might not tolerate children, or the class might not tolerate having a parent
70/// *   `hmenu`     Might need to be valid
71/// *   undocumented global or thread local state
72///
73/// ### Examples
74/// ```rust
75/// # use hwnd::*;
76/// # use std::ptr::*;
77/// unsafe {
78///
79///     let mw = create_window_w(
80///         abistr::cstr16!("Message"), (), 0,
81///         0, 0, 0, 0,
82///         HWnd::MESSAGE, null_mut(), None, null_mut()
83///     ).unwrap();
84///
85///     destroy_window(mw).unwrap();
86///
87/// }
88/// ```
89pub unsafe fn create_window_w<'a>(
90    class_name:     impl Into<NameOrAtom<'a, u16>>,
91    window_name:    impl TryIntoAsOptCStr<u16>,
92    style:          impl Into<WindowStyle>,
93    x:              i32,
94    y:              i32,
95    width:          i32,
96    height:         i32,
97    parent:         impl TryInto<HWnd>,
98    hmenu:          HMENU,
99    hinstance:      impl Into<HInstance<'static>>,
100    param:          *mut c_void,
101) -> Result<HWnd, Error> {
102    fn_context!(create_window_w => CreateWindowW);
103    let parent      = parent        .try_into().map_err(|_| fn_param_error!(parent,         ERROR::INVALID_WINDOW_HANDLE))?.into();
104    let window_name = window_name   .try_into().map_err(|_| fn_param_error!(window_name,    ERROR::INVALID_WINDOW_HANDLE))?;
105    let hwnd = unsafe { CreateWindowExW(
106        0, class_name.into().as_atom_or_cstr_ptr(), window_name.as_opt_cstr(), style.into().into(),
107        x, y, width, height,
108        parent, hmenu, hinstance.into().into(), param,
109    )};
110    fn_succeeded!(!hwnd.is_null())?;
111    Ok(hwnd.into())
112}
113
114/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexa)\]
115/// CreateWindowExA
116///
117/// Creates an overlapped, pop-up, or child window with an extended window style.
118///
119/// ### Safety
120/// The underlying window classes involved may impose soundness requirements on:
121/// *   `param`     Might need to be a valid reference to a specific type, might need to be non-null
122/// *   `parent`    Might not tolerate children, or the class might not tolerate having a parent
123/// *   `hmenu`     Might need to be valid
124/// *   undocumented global or thread local state
125///
126/// ### Examples
127/// ```rust
128/// # use hwnd::*;
129/// # use std::ptr::*;
130/// unsafe {
131///
132///     let mw = create_window_ex_a(
133///         0, abistr::cstr!("Message"), (), 0,
134///         0, 0, 0, 0,
135///         HWnd::MESSAGE, null_mut(), None, null_mut()
136///     ).unwrap();
137///
138///     destroy_window(mw).unwrap();
139///
140/// }
141/// ```
142pub unsafe fn create_window_ex_a<'a>(
143    ex_style:       impl Into<WindowStyleExtended>,
144    class_name:     impl Into<NameOrAtom<'a, u8>>,
145    window_name:    impl TryIntoAsOptCStr,
146    style:          impl Into<WindowStyle>,
147    x:              i32,
148    y:              i32,
149    width:          i32,
150    height:         i32,
151    parent:         impl TryInto<HWnd>,
152    hmenu:          HMENU,
153    hinstance:      impl Into<HInstance<'static>>,
154    param:          *mut c_void,
155) -> Result<HWnd, Error> {
156    fn_context!(create_window_ex_a => CreateWindowExA);
157    let parent      = parent        .try_into().map_err(|_| fn_param_error!(parent,         ERROR::INVALID_WINDOW_HANDLE))?.into();
158    let window_name = window_name   .try_into().map_err(|_| fn_param_error!(window_name,    ERROR::INVALID_WINDOW_HANDLE))?;
159    let hwnd = unsafe { CreateWindowExA(
160        ex_style.into().into(), class_name.into().as_atom_or_cstr_ptr(), window_name.as_opt_cstr(), style.into().into(),
161        x, y, width, height,
162        parent, hmenu, hinstance.into().into(), param,
163    )};
164    fn_succeeded!(!hwnd.is_null())?;
165    Ok(hwnd.into())
166}
167
168/// \[[learn.microsoft.com](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createwindowexw)\]
169/// CreateWindowExW
170///
171/// Creates an overlapped, pop-up, or child window with an extended window style.
172///
173/// ### Safety
174/// The underlying window classes involved may impose soundness requirements on:
175/// *   `param`     Might need to be a valid reference to a specific type, might need to be non-null
176/// *   `parent`    Might not tolerate children, or the class might not tolerate having a parent
177/// *   `hmenu`     Might need to be valid
178/// *   undocumented global or thread local state
179///
180/// ### Examples
181/// ```rust
182/// # use hwnd::*;
183/// # use std::ptr::*;
184/// unsafe {
185///
186///     let mw = create_window_ex_w(
187///         0, abistr::cstr16!("Message"), (), 0,
188///         0, 0, 0, 0,
189///         HWnd::MESSAGE, null_mut(), None, null_mut()
190///     ).unwrap();
191///
192///     destroy_window(mw).unwrap();
193///
194/// }
195/// ```
196pub unsafe fn create_window_ex_w<'a>(
197    ex_style:       impl Into<WindowStyleExtended>,
198    class_name:     impl Into<NameOrAtom<'a, u16>>,
199    window_name:    impl TryIntoAsOptCStr<u16>,
200    style:          impl Into<WindowStyle>,
201    x:              i32,
202    y:              i32,
203    width:          i32,
204    height:         i32,
205    parent:         impl TryInto<HWnd>,
206    hmenu:          HMENU,
207    hinstance:      impl Into<HInstance<'static>>,
208    param:          *mut c_void,
209) -> Result<HWnd, Error> {
210    fn_context!(create_window_ex_w => CreateWindowExW);
211    let parent      = parent        .try_into().map_err(|_| fn_param_error!(parent,         ERROR::INVALID_WINDOW_HANDLE))?.into();
212    let window_name = window_name   .try_into().map_err(|_| fn_param_error!(window_name,    ERROR::INVALID_WINDOW_HANDLE))?;
213    let hwnd = unsafe { CreateWindowExW(
214        ex_style.into().into(), class_name.into().as_atom_or_cstr_ptr(), window_name.as_opt_cstr(), style.into().into(),
215        x, y, width, height,
216        parent, hmenu, hinstance.into().into(), param,
217    )};
218    fn_succeeded!(!hwnd.is_null())?;
219    Ok(hwnd.into())
220}