Comments (1)
Defining new WM
constant can be done just like any other constant:
pub const MAKE_TOAST: co::WM = unsafe { co::WM::from_raw(co::WM::USER.raw() + 20) };
However, this is just the tip of the iceberg.
One of the greatest challenges while writing WinSafe's GUI module was how to deal with type-safe messages, since they have to be:
- sent;
- received;
- contain data;
- extensible.
So, in order to properly declare a custom message, you must fulfill the bullets above. As an example, imagine that MAKE_TOAST
message, whose data is simply how many toasts you should make. It could be implemented like this:
use winsafe::{self as w, prelude::*, co, msg};
pub const MAKE_TOAST: co::WM = unsafe { co::WM::from_raw(co::WM::USER.raw() + 20) };
struct MakeToast {
how_many: u32,
}
unsafe impl MsgSend for MakeToast {
type RetType = ();
fn convert_ret(&self, _: isize) -> Self::RetType {
()
}
fn as_generic_wm(&mut self) -> msg::WndMsg {
msg::WndMsg {
msg_id: MAKE_TOAST,
wparam: self.how_many as _,
lparam: 0,
}
}
}
unsafe impl MsgSendRecv for MakeToast {
fn from_generic_wm(p: msg::WndMsg) -> Self {
Self {
how_many: p.wparam as _,
}
}
}
First, you define the struct with the data it contains, then you implement MsgSend
, so you message can be sent through SendMessage
. And finally you implement MsgSendRecv
, so your message can also be received in an on().wm(...)
handler.
Now sending becomes trivial:
self.wnd.hwnd().SendMessage(
MakeToast {
how_many: 3,
},
);
And when receiving, you must manually cast the generic parameters to the specialized struct:
self.wnd.on().wm(MAKE_TOAST, move |p| {
let p = MakeToast::from_generic_wm(p);
println!("Make {}", p.how_many);
Ok(None)
});
In fact, you made me realize that there is no clear documentation like this in the docs. This example is now here.
from winsafe.
Related Issues (20)
- validate_retrieved_reg_val always fails
- [Question] - Calling windows APIs twice best practice HOT 3
- RegQueryValueEx returning TRANSACTION_REQUEST_NOT_VALID for valid data HOT 6
- nm_custom_draw should provide a mutable reference? HOT 6
- `WC::NoValue`? HOT 1
- I don't think `MultiByteToWideChar` and `WideCharToMultiByte` need to add 1 to `num_bytes` HOT 4
- resizable_layout not work as expect HOT 8
- When the new_dlg function is used to create a window, the wm_create event does not work HOT 6
- Async IO should be marked as "unsafe" HOT 1
- CreateToolhelp32Snapshot error when compiling for x86
- Panic in wstring/ QueryFullProcessImageName HOT 3
- How to set up a transparent window? HOT 1
- Public access ot LocalFreeGuard::new or LocalFree HOT 2
- [BUG?QUESTION?]SendInput example not working HOT 1
- [Question] How can I set the pszText into NMLVDISPINFO correctly when handling lvn_get_disp_info event? HOT 19
- [Question] Using MutFn as WinApi callback
- WString pointers cause issues on stack for certain API calls HOT 5
- Build fails for the i686-pc-windows-(msvc/gnu) targets HOT 8
- winsvc.h HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from winsafe.