In my rust experiments I’m currently trying the following. I want to display a basic empty window using the windows crate and the windows API. The below code compiles and executes. The only thing it does not do is display an actual window. What am I doing wrong here as the code looks find to me and the e.g. c++ equivalent would show a window I believe.
use windows::{
core::*, Win32::{Foundation::*, Graphics::Gdi::{CreateSolidBrush, UpdateWindow, ValidateRect, COLOR_WINDOW, HBRUSH}, System::LibraryLoader::{GetModuleHandleA, GetModuleHandleW}, UI::WindowsAndMessaging::*},
};
fn main() -> Result<()> {
unsafe{
let window_class = w!("the_window");
let window_name = w!("The Factory");
let instance = GetModuleHandleW(None)?;
let kleur = COLORREF{
0: 255,
};
let brush: HBRUSH = CreateSolidBrush(kleur);
debug_assert!(instance.0 != 0);
//define the window
let wc = WNDCLASSW {
hCursor: LoadCursorW(None, IDC_ARROW)?,
style: CS_HREDRAW | CS_VREDRAW,
lpszClassName: window_name,
hInstance: instance.into(),
hbrBackground: brush,
lpfnWndProc: Some(wndproc),
..Default::default()
};
//register the class
let atom = RegisterClassW(&wc);
debug_assert!(atom != 0);
let hwnd_desktop = GetDesktopWindow();
let hwnd = CreateWindowExW(WINDOW_EX_STYLE::default(), window_class, window_name, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 100, 100, hwnd_desktop, None, instance, None);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
let mut message = std::mem::zeroed();
println!("init the msg structure success.");
while GetMessageA(&mut message, None, 0, 0).into() {
TranslateMessage(&message);
DispatchMessageA(&message);
}
Ok(())
}
}
extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
unsafe {
match message {
WM_PAINT => {
println!("WM_PAINT");
_ = ValidateRect(window, None);
LRESULT(0)
}
WM_DESTROY => {
println!("WM_DESTROY");
PostQuitMessage(0);
LRESULT(0)
}
_ => DefWindowProcA(window, message, wparam, lparam),
}
}
}