pub struct WindowBuilder<'a> { /* private fields */ }Expand description
A builder for creating new windows.
Implementations§
Source§impl<'a> WindowBuilder<'a>
impl<'a> WindowBuilder<'a>
Sourcepub fn new(
window_proc: impl WindowProc + 'static,
window_class: &WindowClass,
) -> WindowBuilder<'_>
pub fn new( window_proc: impl WindowProc + 'static, window_class: &WindowClass, ) -> WindowBuilder<'_>
Create a new window builder.
The window procedure and window class are set here.
Discussion question: would it ever make sense to create a window without a window procedure?
Examples found in repository?
33fn main() {
34 unsafe {
35 let icon = LoadIconW(0 as HINSTANCE, IDI_APPLICATION);
36 let cursor = LoadCursorW(0 as HINSTANCE, IDC_ARROW);
37 let brush = CreateSolidBrush(0xff_ff_ff);
38 let win_class = WindowClass::builder("rust")
39 .icon(icon)
40 .cursor(cursor)
41 .background(brush)
42 .build()
43 .unwrap();
44 let hwnd = WindowBuilder::new(MyWindowProc, &win_class)
45 .name("win-win example")
46 .style(WS_OVERLAPPEDWINDOW)
47 .build();
48 ShowWindow(hwnd, SW_SHOWNORMAL);
49 win_win::runloop(null_mut());
50 }
51}Sourcepub fn build(self) -> HWND
pub fn build(self) -> HWND
Build a window.
The return value is the HWND for the window, or 0 on error.
The lifetime of the window is until WM_NCDESTROY is called,
at which point the window procedure is dropped.
Examples found in repository?
33fn main() {
34 unsafe {
35 let icon = LoadIconW(0 as HINSTANCE, IDI_APPLICATION);
36 let cursor = LoadCursorW(0 as HINSTANCE, IDC_ARROW);
37 let brush = CreateSolidBrush(0xff_ff_ff);
38 let win_class = WindowClass::builder("rust")
39 .icon(icon)
40 .cursor(cursor)
41 .background(brush)
42 .build()
43 .unwrap();
44 let hwnd = WindowBuilder::new(MyWindowProc, &win_class)
45 .name("win-win example")
46 .style(WS_OVERLAPPEDWINDOW)
47 .build();
48 ShowWindow(hwnd, SW_SHOWNORMAL);
49 win_win::runloop(null_mut());
50 }
51}Sourcepub fn name(self, name: impl AsRef<OsStr>) -> Self
pub fn name(self, name: impl AsRef<OsStr>) -> Self
Set the window name.
This becomes the lpWindowName parameter to CreateWindowEx.
Examples found in repository?
33fn main() {
34 unsafe {
35 let icon = LoadIconW(0 as HINSTANCE, IDI_APPLICATION);
36 let cursor = LoadCursorW(0 as HINSTANCE, IDC_ARROW);
37 let brush = CreateSolidBrush(0xff_ff_ff);
38 let win_class = WindowClass::builder("rust")
39 .icon(icon)
40 .cursor(cursor)
41 .background(brush)
42 .build()
43 .unwrap();
44 let hwnd = WindowBuilder::new(MyWindowProc, &win_class)
45 .name("win-win example")
46 .style(WS_OVERLAPPEDWINDOW)
47 .build();
48 ShowWindow(hwnd, SW_SHOWNORMAL);
49 win_win::runloop(null_mut());
50 }
51}Sourcepub fn style(self, style: DWORD) -> Self
pub fn style(self, style: DWORD) -> Self
Set the window style.
The argument is the bitwise OR of a number of WS_ values from the Window Styles enumeration.
It becomes the dwStyle parameter to CreateWindowEx.
Examples found in repository?
33fn main() {
34 unsafe {
35 let icon = LoadIconW(0 as HINSTANCE, IDI_APPLICATION);
36 let cursor = LoadCursorW(0 as HINSTANCE, IDC_ARROW);
37 let brush = CreateSolidBrush(0xff_ff_ff);
38 let win_class = WindowClass::builder("rust")
39 .icon(icon)
40 .cursor(cursor)
41 .background(brush)
42 .build()
43 .unwrap();
44 let hwnd = WindowBuilder::new(MyWindowProc, &win_class)
45 .name("win-win example")
46 .style(WS_OVERLAPPEDWINDOW)
47 .build();
48 ShowWindow(hwnd, SW_SHOWNORMAL);
49 win_win::runloop(null_mut());
50 }
51}Sourcepub fn ex_style(self, style: DWORD) -> Self
pub fn ex_style(self, style: DWORD) -> Self
Set the extended window style.
The argument is the bitwise OR of a number of WS_EX values from the Extended Window Styles enumeration.
It becomes the dwExStyle parameter to CreateWindowEx.
An interesting parameter is WS_EX_NOREDIRECTIONBITMAP, which disables the redirection bitmap.
It is useful to set when the window will contain a swapchain and no GDI content (in particular, no
menus). There is a particular source of artifacting on window resize that is reduced when the
redirection bitmap is disabled. It should almost always be set when using DirectComposition,
see this article by Kenny Kerr.
Sourcepub fn position(self, x: c_int, y: c_int) -> Self
pub fn position(self, x: c_int, y: c_int) -> Self
Set the window position.
The arguments become the x and y parameters to CreateWindowEx. To set one but not the other,
use CW_USEDEFAULT. These are in raw pixel values.
The position is relative to the top left corner of the primary monitor. See EnumDisplayMonitors
for more information about multiple monitors.
Sourcepub fn size(self, width: c_int, height: c_int) -> Self
pub fn size(self, width: c_int, height: c_int) -> Self
Set the window size.
The arguments become the nWidth and nHeight parameters to CreateWindowEx. To set one but not
the other, use CW_USEDEFAULT. These are in raw pixel values.
Sourcepub unsafe fn parent_hwnd(self, parent: HWND) -> Self
pub unsafe fn parent_hwnd(self, parent: HWND) -> Self
Set the parent window.
The argument becomes the hWndParent parameter to CreateWindowEx.
§Safety
The argument must be a valid HWND reference.
Set the menu.
The argument becomes the hMenu parameter to CreateWindowEx.
§Safety
The argument must be a valid HMENU reference.
Sourcepub unsafe fn instance(self, instance: HINSTANCE) -> Self
pub unsafe fn instance(self, instance: HINSTANCE) -> Self
Set the instance handle.
The argument becomes the hInstance parameter to CreateWindowEx.
Instance handles are a namespace mechanism, so that components (in a DLL, for example) don’t interfere with each other. For a top-level application, it is safe to leave this unset.
Raymond Chen’s blog has a bit of information about HINSTANCE, including its historical distinction from HMODULE (they are now the same).
§Safety
The argument must be a valid HINSTANCE reference.