如何知道菜单项何时被点击?

How to know when a menu item has been clicked?

我正在使用 winsafe crate,想知道何时在 window 中单击菜单项。

如果是,

flags & MF_MOUSESELECT as u16 应该是 1,如果不是,则应该是 0,但每次触发事件时它总是 32768,即使它只是来自用户悬停一个菜单项,甚至单击以将其关闭。

为什么?

self.wnd.on().wm(winsafe::co::WM::MENUSELECT, {
  move |params| {
    let wparam = params.wparam;
    let lparam = params.lparam;
    let flags = (wparam >> 16 & 0xffff) as u16;
    let MF_MOUSESELECT = 0x00008000 as u32;
    
    println!("{}", flags & MF_MOUSESELECT as u16);
    // always 32768

    0
  }
});

菜单由程序编译嵌入的资源脚本生成:

1 MENU
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
{
  POPUP "&File"
  {
    MENUITEM "&Open",  1
    MENUITEM "&Save",  2
  }
  POPUP "&Help"
  {
    MENUITEM "&About",  3
  }
}

你应该处理 WM_COMMAND。实际上有内置的菜单功能。只需提供您在资源文件中指定的 id 即可检查相应菜单项何时被单击。

用这些替换您的事件侦听器:

self.wnd.on().wm_command(co::CMD::Menu, 1, {
  move || {
    println!("Open clicked.")
  }
});

self.wnd.on().wm_command(co::CMD::Menu, 2, {
  move || {
    println!("Save clicked.")
  }
});

self.wnd.on().wm_command(co::CMD::Menu, 3, {
  move || {
    println!("About clicked.")
  }
});