Git Product home page Git Product logo

Comments (1)

rodrigocfd avatar rodrigocfd commented on June 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.