Struct WindowClassBuilder

Source
pub struct WindowClassBuilder { /* private fields */ }
Expand description

A builder for registering new window classes.

Implementations§

Source§

impl WindowClassBuilder

Source

pub const DLGWINDOWEXTRA: c_int = 30i32

The number of extra bytes needed for dialogs.

See wnd_extra_bytes.

Source

pub fn build(self) -> Result<WindowClass, Error>

Create the window class.

Note: the window class is leaked, as its lifetime is most commonly that of the application. Somebody who really wants to reclaim that memory can call UnregisterClass manually and deal with the soundness consequences.

Examples found in repository?
examples/hello-win.rs (line 42)
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 class_style(self, style: DWORD) -> Self

Set the window class style.

The argument is the bitwise OR of a number of CS_ values from the Window Class Styles enumeration. It becomes style field in the WNDCLASSEX passed to RegisterClassEx. See Class Styles for more explanation.

Source

pub unsafe fn wnd_extra_bytes(self, extra_bytes: c_int) -> Self

Allocate extra bytes in window instances.

The argument becomes the cbWndExtra field in the WNDCLASSEX passed to RegisterClassEx.

Generally this isn’t that useful unless creating a dialog, in which case it should be DLGWINDOWEXTRA.

Note: there is no corresponding method to set cbClsExtra, as I can’t think of a good reason why it would ever be needed.

§Safety

The argument must be a reasonable size (no idea what happens if negative, for example).

Source

pub unsafe fn instance(self, instance: HINSTANCE) -> Self

Set the instance handle.

The argument becomes the hInstance field in the WNDCLASSEX passed to RegisterClassEx.

See the instance method on WindowBuilder for more details.

§Safety

The argument must be a valid HINSTANCE reference.

Source

pub unsafe fn icon(self, icon: HICON) -> Self

Set the icon.

The argument becomes the hIcon field in the WNDCLASSEX passed to RegisterClassEx.

§Safety

The argument must be a valid HICON reference.

Examples found in repository?
examples/hello-win.rs (line 39)
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 unsafe fn small_icon(self, icon: HICON) -> Self

Set the small icon.

The argument becomes the hIconSm field in the WNDCLASSEX passed to RegisterClassEx.

§Safety

The argument must be a valid HICON reference.

Source

pub unsafe fn cursor(self, cursor: HCURSOR) -> Self

Set the cursor.

The argument becomes the hCursor field in the WNDCLASSEX passed to RegisterClassEx.

The default implementation of WM_SETCURSOR applies this cursor. In the old-school approach where each control has its own HWND, it’s reasonable to use this to set the cursor, then everything should just work (even without explicit handling of WM_SETCURSOR). However, in the modern approach where there’s a single window for the application, probably a more useful strategy is to set the cursor on WM_MOUSEMOVE, which reports the cursor position (rather than relying on hit testing with the HWND bounds). In that case, setting a default cursor on the window will likely result in flashing, as the two window message handlers will compete.

This Stack overflow question contains more details.

Of course, if the entire window is to have a single cursor, setting it here is quite reasonable.

§Safety

The argument must be a valid HCURSOR reference.

Examples found in repository?
examples/hello-win.rs (line 40)
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 unsafe fn background(self, brush: HBRUSH) -> Self

Set the background brush.

The argument becomes the hBrBackground field in the WNDCLASSEX passed to RegisterClassEx.

§Safety

The argument must be a valid HBRUSH reference.

Examples found in repository?
examples/hello-win.rs (line 41)
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 menu_name(self, menu_name: impl AsRef<OsStr>) -> Self

Set the default menu.

The argument becomes the lpszClassName field in the WNDCLASSEX passed to RegisterClassEx.

The string references the resource name of the class menu. There is no mechanism to support the MAKEINTRESOURCE macro.

Auto Trait Implementations§

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.