Module winsafe::msg

source ·
Available on crate feature user only.
Expand description

Parameters of window messages.

WndMsg is the generic message, with WPARAM and LPARAM fields. Other messages belong to a module according to its prefix, for example, BM_CLICK can be found in bm module.

All messages can be sent, so they all implement the MsgSend trait. Some messages can also be received – that means you can handle them if you’re implementing a custom window from scratch –, and these implement the MsgSendRecv trait.

§Sending messages

We want to delete the 3rd element of a ListView control. This can be done by sending it an LVM_DELETEITEM message via HWND::SendMessage. The message itself is a struct, which is initialized with the specific message parameters.

The message struct also defines the data type returned by SendMessage. In the example below, LVM_DELETEITEM returns SysResult<()>.

use winsafe::{self as w, prelude::*, msg};

let hlistview: w::HWND; // initialized somewhere

hlistview.SendMessage(
    msg::lvm::DeleteItem {
        index: 2,
    },
).expect("Failed to delete item 2.");

Messages are organized into modules according to their prefixes: wm (window messages), lvm (list view messages), and so on.

§Custom messages

In order to create a custom message, you must create a struct with the data it contains (if any) and implement the MsgSend and MsgSendRecv traits:

use winsafe::{self as w, prelude::*, co, msg};

/// The integer value of our message ID.
pub const MAKE_TOAST: co::WM = unsafe { co::WM::from_raw(co::WM::USER.raw() + 20) };

/// Our message with its parameter.
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 _,
        }
    }
}

Modules§

Structs§