Created
August 22, 2019 07:35
-
-
Save rust-play/854135bfa2a127800002451e97a8985e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use core::{ | |
mem::zeroed, | |
ptr::{null, null_mut}, | |
}; | |
use winapi::{ | |
shared::windef::HWND, | |
um::{ | |
libloaderapi::GetModuleHandleW, | |
winuser::{ | |
CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW, RegisterClassW, | |
TranslateMessage, CW_USEDEFAULT, MSG, WNDCLASSW, WS_CAPTION, WS_MINIMIZEBOX, | |
WS_SYSMENU, WS_VISIBLE, | |
}, | |
}, | |
}; | |
unsafe extern "system" fn the_proc(win: HWND, msg: u32, wparam: usize, lparam: isize) -> isize { | |
match msg { | |
//WM_PAINT => unimplemented!("make Vk draw here I guess?"), | |
_ => DefWindowProcW(win, msg, wparam, lparam), | |
} | |
} | |
fn wide_null(string: &str) -> Vec<u16> { | |
string.encode_utf16().chain(Some(0)).collect() | |
} | |
fn main() { | |
let mut class: WNDCLASSW = unsafe { zeroed() }; | |
class.lpfnWndProc = Some(the_proc); | |
class.hInstance = unsafe { GetModuleHandleW(null()) }; | |
assert!(!class.hInstance.is_null()); | |
let class_name_wide: Vec<u16> = wide_null("ClassName"); | |
class.lpszClassName = class_name_wide.as_ptr(); | |
assert_ne!(unsafe { RegisterClassW(&class) }, 0); | |
let window_name_wide: Vec<u16> = wide_null("Demo Window"); | |
let win = unsafe { | |
CreateWindowExW( | |
0, | |
class_name_wide.as_ptr(), | |
window_name_wide.as_ptr(), | |
WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, | |
CW_USEDEFAULT, | |
CW_USEDEFAULT, | |
800, | |
600, | |
null_mut(), | |
null_mut(), | |
class.hInstance, | |
null_mut(), | |
) | |
}; | |
assert!(!win.is_null()); | |
// then in here we'd start Vk before hitting the main loop I guess? | |
loop { | |
let mut msg: MSG = unsafe { zeroed() }; | |
if unsafe { GetMessageW(&mut msg, win, 0, 0) } > 0 { | |
unsafe { | |
TranslateMessage(&msg); | |
DispatchMessageW(&msg); | |
} | |
} else { | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment