Professional Documents
Culture Documents
A Rust Temp
A Rust Temp
#[cfg(windows)]
fn print_message(msg: &str) -> Result<i32, Error> {
use std::ffi::OsStr;
use std::iter::once;
use std::os::windows::ffi::OsStrExt;
use std::ptr::null_mut;
use winapi::um::winuser::{MB_OK, MessageBoxW};
let wide: Vec<u16> = OsStr::new(msg).encode_wide().chain(once(0)).collect();
let ret = unsafe {
MessageBoxW(null_mut(), wide.as_ptr(), wide.as_ptr(), MB_OK)
};
if ret == 0 { Err(Error::last_os_error()) }
else { Ok(ret) }
}
#[cfg(not(windows))]
fn print_message(msg: &str) -> Result<(), Error> {
println!("{}", msg);
Ok(())
}
fn main() {
//println!("Hello, world!");
print_message("Hello, world!").unwrap();
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::iter::once;
use std::mem;
use std::ptr::null_mut;
use std::io::Error;
use self::user32::{
DefWindowProcW,
RegisterClassW,
CreateWindowExW,
TranslateMessage,
DispatchMessageW,
GetMessageW,
};
use self::winapi::HWND;
use self::kernel32::GetModuleHandleW;
use self::winapi::winuser::{
MSG,
WNDCLASSW,
CS_OWNDC,
CS_HREDRAW,
CS_VREDRAW,
CW_USEDEFAULT,
WS_OVERLAPPEDWINDOW,
WS_VISIBLE,
};
struct Window {
handle : HWND,
}
unsafe {
let hinstance = GetModuleHandleW( null_mut() );
let wnd_class = WNDCLASSW {
style : CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
lpfnWndProc : Some( DefWindowProcW ),
hInstance : hinstance,
lpszClassName : name.as_ptr(),
cbClsExtra : 0,
cbWndExtra : 0,
hIcon: null_mut(),
hCursor: null_mut(),
hbrBackground: null_mut(),
lpszMenuName: null_mut(),
};
RegisterClassW( &wnd_class );
if handle.is_null() {
Err( Error::last_os_error() )
} else {
Ok( Window { handle } )
}
}
}
true
} else {
false
}
}
}
fn main() {
let mut window = create_window( "my_window", "Hello Windows" ).unwrap();
loop {
if !handle_message( &mut window ) {
break;
}
}
}
//=============================================================
//lightern
//winapi has been merged with user32 and kernel32 and I was thinking it would be
great, if you could update your guide for others, since I bet there are many people
like me wrestling with this.
// Let's put this so that it won't open console
#![windows_subsystem = "windows"]
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::iter::once;
use std::mem;
use std::ptr::null_mut;
use std::io::Error;
use self::winapi::shared::windef::HWND;
use self::winapi::um::libloaderapi::GetModuleHandleW;
use self::winapi::um::winuser::{
DefWindowProcW,
RegisterClassW,
CreateWindowExW,
TranslateMessage,
DispatchMessageW,
GetMessageW,
};
use self::winapi::um::winuser::{
MSG,
WNDCLASSW,
CS_OWNDC,
CS_HREDRAW,
CS_VREDRAW,
CW_USEDEFAULT,
WS_OVERLAPPEDWINDOW,
WS_VISIBLE,
};
// ----------------------------------------------------
// Window struct
#[cfg(windows)]
struct Window {
handle : HWND,
}
unsafe {
// Create handle instance that will call GetModuleHandleW, which grabs the
instance handle of WNDCLASSW (check third parameter)
let hinstance = GetModuleHandleW( null_mut() );
// Create "class" for window, using WNDCLASSW struct (different from Window
our struct)
let wnd_class = WNDCLASSW {
style : CS_OWNDC | CS_HREDRAW | CS_VREDRAW, // Style
lpfnWndProc : Some( DefWindowProcW ), // The
callbackfunction for any window event that can occur in our window!!! Here you
could react to events like WM_SIZE or WM_QUIT.
hInstance : hinstance, // The
instance handle for our application which we can retrieve by calling
GetModuleHandleW.
lpszClassName : name.as_ptr(), // Our class
name which needs to be a UTF-16 string (defined earlier before unsafe). as_ptr()
(Rust's own function) returns a raw pointer to the slice's buffer
cbClsExtra : 0,
cbWndExtra : 0,
hIcon: null_mut(),
hCursor: null_mut(),
hbrBackground: null_mut(),
lpszMenuName: null_mut(),
};
// We have to register this class for Windows to use
RegisterClassW( &wnd_class );
// More info:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx
// Create a window based on registered class
let handle = CreateWindowExW(
0, // dwExStyle
name.as_ptr(), // lpClassName, name of
the class that we want to use for this window, which will be the same that we have
registered before.
title.as_ptr(), // lpWindowName
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // dwStyle
CW_USEDEFAULT, // Int x
CW_USEDEFAULT, // Int y
CW_USEDEFAULT, // Int nWidth
CW_USEDEFAULT, // Int nHeight
null_mut(), // hWndParent
null_mut(), // hMenu
hinstance, // hInstance
null_mut() ); // lpParam
if handle.is_null() {
Err( Error::last_os_error() )
} else {
Ok( Window { handle } )
}
}
}
#[cfg(windows)]
// Create message handling function with which to link to hook window to Windows
messaging system
// More info:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx
fn handle_message( window : &mut Window ) -> bool {
unsafe {
let mut message : MSG = mem::uninitialized();
true
} else {
false
}
}
}
#[cfg(windows)]
fn main() {
let mut window = create_window( "my_window", "Portfolio manager
pro" ).unwrap();
loop {
if !handle_message( &mut window ) {
break;
}
}
}
//=============================================================
//rokit
//modules like winuser are gated behind a feature flag. You have to explicitly add
them to your Cargo.toml.
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.5", features = ["winuser", "libloaderapi"] }
//=============================================================
//bobahop
// I would suggest a tweak to handle_message to eliminate the deprecated call to
mem::uninitialized() by replacing it with mem::MaybeUninit.
fn handle_message(window: &mut Window) -> bool {
unsafe {
//let mut message: MSG = mem::uninitialized();
let message = mem::MaybeUninit::<MSG>::uninit();
// Get message from message queue with GetMessageW
if GetMessageW(message.as_ptr() as *mut MSG, window.handle, 0, 0) > 0 {
TranslateMessage(message.as_ptr() as *const MSG); // Translate message
into something meaningful with TranslateMessage
DispatchMessageW(message.as_ptr() as *const MSG); // Dispatch message
with DispatchMessageW
true
} else {
false
}
}
}
//=============================================================
//bobahop
#![windows_subsystem = "windows"]
// #![allow(unused_assignments)]
// #![allow(unused_imports)]
// #![allow(unused_variables)]
// #![allow(dead_code)]
#![allow(non_snake_case)]
extern crate winapi;
use std::ffi::OsStr;
use std::io::Error;
use std::iter::once;
use std::mem;
use std::mem::{size_of, zeroed};
use std::os::raw::c_void;
use std::os::windows::ffi::OsStrExt;
use std::ptr::null_mut;
fn open_file_dialog(hWnd: HWND) {
unsafe {
let msg: &str;
let retval = CoInitializeEx(null_mut(), COINIT_APARTMENTTHREADED);
let msgother = &format!("{}{}", "return value: ", retval);
match retval {
S_OK => msg = "OK",
S_FALSE => msg = "False",
E_INVALIDARG => msg = "Invalid argument",
RPC_E_CHANGED_MODE => msg = "Changed modes",
_ => msg = msgother,
}
if retval != S_OK && retval != S_FALSE {
msg_box(hWnd, msg, "CoInitialize", MB_OK);
return;
}
let riid: LPIID = &mut zeroed::<GUID>();
let retval = IIDFromString(
wstr("{D57C7288-D4AD-4768-BE02-9D969532D960}").as_ptr(),
riid,
);
if retval == E_INVALIDARG {
//let msgother = &format!("{}{}", "return value: ", retval);
msg_box(hWnd, "Invalid argument", "IIDFromString", MB_OK);
return;
}
let mut pFileOpen: *mut IFileOpenDialog = &mut zeroed::<IFileOpenDialog>();
let retval = CoCreateInstance(
&CLSID_FileOpenDialog,
null_mut(),
CLSCTX_ALL,
riid,
<*mut *mut IFileOpenDialog>::cast(&mut pFileOpen),
);
if !SUCCEEDED(retval) {
let msgother = &format!("{}{}", "return value: ", retval);
msg_box(hWnd, msgother, "CoCreateInstance", MB_OK);
return;
}
(*pFileOpen).Show(hWnd);
}
}
struct Window {
handle: HWND,
}
struct BobWindow {
inWindow: isize,
}
enum BtnId {
Btn1 = 1,
Btn2 = 2,
}
impl BtnId {
fn from_u32(value: u32) -> BtnId {
match value {
1 => BtnId::Btn1,
2 => BtnId::Btn2,
_ => panic!("Unknown value {}", value),
}
}
}
fn btn1_click(hWnd: HWND) {
msg_box(hWnd, "You touched me!", "Clicked button 1!", MB_OK);
}
fn btn2_click(hWnd: HWND) {
open_file_dialog(hWnd);
}
fn msg_box(hWnd: HWND, msg: &str, title: &str, btns: UINT) -> i32 {
unsafe { MessageBoxW(hWnd, wstr(msg).as_ptr(), wstr(title).as_ptr(), btns) }
}
unsafe {
//More info:
https://docs.microsoft.com/en-us/windows/win32/learnwin32/creating-a-window
let wnd_class = WNDCLASSW {
style: CS_OWNDC | CS_HREDRAW | CS_VREDRAW, // Style
lpfnWndProc: Some(MyWindowProcW), // The callbackfunction for any
window event that can occur in our window!!! Here you could react to events like
WM_SIZE or WM_QUIT.
hInstance: hinstance, // The instance handle for our application which
we can retrieve by calling GetModuleHandleW.
lpszClassName: name.as_ptr(), // Our class name which needs to be a
UTF-16 string (defined earlier before unsafe). as_ptr() (Rust's own function)
returns a raw pointer to the slice's buffer
cbClsExtra: 0,
cbWndExtra: 0,
hIcon: null_mut(),
hCursor: null_mut(),
hbrBackground: null_mut(),
lpszMenuName: null_mut(),
};
// More info:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx
// Create a window based on registered class
let handle = CreateWindowExW(
0, // dwExStyle
name.as_ptr(), // lpClassName, name of the class that we want to use
for this window, which will be the same that we have registered before.
title.as_ptr(), // lpWindowName
WS_OVERLAPPEDWINDOW | WS_VISIBLE, // dwStyle
CW_USEDEFAULT, // Int x
CW_USEDEFAULT, // Int y
CW_USEDEFAULT, // Int nWidth
CW_USEDEFAULT, // Int nHeight
null_mut(), // hWndParent
null_mut(), // hMenu
hinstance, // hInstance
//now that it's on the heap, unbox the struct into a pointer
Box::into_raw(bobbie) as *mut BobWindow as *mut c_void,
);
if handle.is_null() {
Err(Error::last_os_error())
} else {
Ok(Window { handle })
}
}
}
// Create message handling function with which to link to hook window to Windows
messaging system
// More info:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx
fn handle_message(window: &mut Window) -> bool {
unsafe {
let message = mem::MaybeUninit::<MSG>::uninit();
if GetMessageW(message.as_ptr() as *mut MSG, window.handle, 0, 0) > 0 {
TranslateMessage(message.as_ptr() as *const MSG); // Translate message
into something meaningful with TranslateMessage
DispatchMessageW(message.as_ptr() as *const MSG); // Dispatch message
with DispatchMessageW
true
} else {
false
}
}
}
fn main() {
show_window();
}
fn show_window() {
//Create handle instance that will call GetModuleHandleW, which grabs the
instance handle of WNDCLASSW (check third parameter)
let hinstance = unsafe { GetModuleHandleW(null_mut()) };