我正在尝试使用标准 Windows API 窗口和子窗口作为控件(最具体的是静态文本、文本框和按钮)创建一个窗口窗体。
我正在使用 Windows 板条箱,代码编译时没有任何错误并运行,但我只能看到空白窗口,而看不到里面的控件。
我做错了什么?
提前致谢!
#![allow(non_snake_case)]
use windows::{
core::*,
Win32::Foundation::*,
Win32::Graphics::Gdi::*,
Win32::System::LibraryLoader::GetModuleHandleA,
Win32::UI::WindowsAndMessaging::*,
};
fn main() -> Result<()> {
unsafe {
let instance = GetModuleHandleA(None)?;
let window_class = s!("window");
let wc = WNDCLASSA {
hCursor: LoadCursorW(None, IDC_ARROW)?,
hInstance: instance.into(),
lpszClassName: window_class,
style: CS_HREDRAW | CS_VREDRAW,
lpfnWndProc: Some(wndproc),
..Default::default()
};
let atom = RegisterClassA(&wc);
debug_assert!(atom != 0);
let _hwnd = CreateWindowExA(
WINDOW_EX_STYLE::default(),
window_class,
s!("A simple Form"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
None,
None,
instance,
None,
)?;
let _hndLabel = CreateWindowExA(
WINDOW_EX_STYLE::default(),
s!("static"),
s!("What's your name? "),
WINDOW_STYLE::default(),
50, 100, 200, 30, _hwnd, None, instance, None)?;
let _hndCaixa = CreateWindowExA(
WINDOW_EX_STYLE::default(),
s!("edit"),
s!("Type Here!"),
WINDOW_STYLE::default(),
200, 100, 200, 30, _hwnd, None, instance, None)?;
let _hndButton = CreateWindowExA(
WINDOW_EX_STYLE::default(),
s!("Button"),
s!(" OK "),
WINDOW_STYLE::default(),
400, 100, 200, 30, _hwnd, None, instance, None)?;
let _ = ShowWindow(_hwnd, SW_SHOW);
let _ = UpdateWindow(_hwnd);
let mut message = MSG::default();
while GetMessageA(&mut message, None, 0, 0).into() {
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),
}
}
}