Skip to main content

WindowBuilder

Struct WindowBuilder 

Source
pub struct WindowBuilder<'a> { /* private fields */ }
Expand description

A builder for creating new windows.

Implementations§

Source§

impl<'a> WindowBuilder<'a>

Source

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?
examples/hello-win.rs (line 44)
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}
Source

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?
examples/hello-win.rs (line 47)
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}
Source

pub fn name(self, name: impl AsRef<OsStr>) -> Self

Set the window name.

This becomes the lpWindowName parameter to CreateWindowEx.

Examples found in repository?
examples/hello-win.rs (line 45)
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}
Source

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?
examples/hello-win.rs (line 46)
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}
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub unsafe fn menu(self, menu: HMENU) -> Self

Set the menu.

The argument becomes the hMenu parameter to CreateWindowEx.

§Safety

The argument must be a valid HMENU reference.

Source

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.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for WindowBuilder<'a>

§

impl<'a> !Send for WindowBuilder<'a>

§

impl<'a> !Sync for WindowBuilder<'a>

§

impl<'a> !UnwindSafe for WindowBuilder<'a>

§

impl<'a> Freeze for WindowBuilder<'a>

§

impl<'a> Unpin for WindowBuilder<'a>

§

impl<'a> UnsafeUnpin for WindowBuilder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.