I'm trying to do some win32 GUI programming with rust's win32 APIs.
I want to try to create some generic functions for creating windows.
The problem is that some windows will need a custom handler for messages so I need to be able to pass this (or the default handler).
Here's some of the code:
// Application
quick_main!(run);
fn run() -> Result<()> {
let mut window = wui::create_main_window("MainWindow", "Checksum",
&handle_main_event)?;
Ok(wui::event_loop(&mut window)?) // for error_chain
}
unsafe extern "system"
fn handle_main_event(hwnd: HWND, msg: UINT, wparam: WPARAM,
lparam: LPARAM) -> LRESULT {
match msg {
// TODO
_ => DefWindowProcW(hwnd, msg, wparam, lparam)
}
}
// wui crate
pub struct Window {
pub hwnd: HWND,
}
pub type WindowProc = unsafe extern "system" fn(HWND, UINT, WPARAM,
LPARAM)->LRESULT; // or Fn ?
pub fn as_ws(s: &str) -> Vec<u16> {
OsStr::new(s).encode_wide().chain(once(0)).collect()
}
pub fn event_loop(window: &mut Window) -> Result<()> {
loop {
match util::handle_event(window) {
Ok(false) => return Ok(()), // User quit
Err(err) => bail!(err),
_ => () // continue
}
}
}
pub fn create_main_window(class_name: &str, title: &str,
window_proc: &WindowProc) -> Result<Window> {
util::create_window(class_name, title, true, *window_proc)
}
The error I'm getting is this:
R:\rs\Checksum>cargo run --release --
Compiling Checksum v1.0.0 (file:///R:/rs/Checksum)
error[E0308]: mismatched types
--> src\main.rs:27:46
|
27 | &handle_main_event)?;
| ^^^^^^^^^^^^^^^^^^ expected fn pointer,
found fn item
|
= note: expected type `&unsafe extern "system" fn(*mut winapi::shared::windef::HWND__, u32, usize, isize) -> isize`
found type `&unsafe extern "system" fn(*mut winapi::shared::windef::HWND__, u32, usize, isize) -> isize {handle_main_event}`
error: aborting due to previous error
I am guessing that I need to use Fn
but unfortunately simply changing fn
to Fn
doesn't work.