打开menustrip时,不会触发MouseEnter事件

时间:2013-03-02 19:58:23

标签: c# winforms mouseenter menustrip

我有一个包含多个项目的menuStrip,以及表单其他部分的按钮。当鼠标进入按钮时,它会执行某些操作(MouseEnter事件)。我遇到的问题是,当打开menustrip时,如果鼠标进入按钮,则不会触发MouseEnter事件。在menustrip被打开时有没有办法解雇?

要以图形方式查看,我正在做的是:

enter image description here

我有一个menustrip,每个父菜单项都有一个按钮。按钮将位于其上方,因此menustrip中唯一可见的部分将是带有子项目的容器。

现在,当鼠标进入按钮时,例如“系统”,它会在菜单项中执行单击。这就是容器出现的原因。但是一旦打开,如果我想打开其他父母的任何其他容器,我必须先点击才能失去焦点。然后,我想要实现的目的是无需点击即可。

我想要的行为就像menustrip那样。例如,如果系统已打开,并且鼠标进入客户端,它将自动关闭系统并打开客户端。

enter image description here

1 个答案:

答案 0 :(得分:0)

让我们看看这是否是一个有效的答案:

#region IMessageFilter implementation
/// <summary> Redirect WM_MouseWheel messages to window under mouse.</summary>
    /// <remarks>Redirect WM_MouseWheel messages to window under mouse (rather than 
/// that with focus) with adjusted delta.
/// <see cref="http://www.flounder.com/virtual_screen_coordinates.htm"/>
/// Dont forget to add this to constructor:
///             Application.AddMessageFilter(this);
///</remarks>
    /// <param name="m">The Windows Message to filter and/or process.</param>
    /// <returns>Success (true) or failure (false) to OS.</returns>
    [System.Security.Permissions.PermissionSetAttribute(
        System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
    bool IMessageFilter.PreFilterMessage(ref Message m) {
        // Determine window and control at these coordinates.
  //var pos   = WindowsMouseInput.GetPointLParam(m.LParam);
        var hWnd  = WindowFromPoint( WindowsMouseInput.GetPointLParam(m.LParam) );
        var ctl   = Control.FromHandle(hWnd);
  if (hWnd != IntPtr.Zero  &&  hWnd != m.HWnd  &&  ctl != null) {
    switch(m.Msg) {
      default:  return false;
      case (int)WM.MOUSEWHEEL:
        DebugTracing.Trace(TraceFlag.ScrollEvents, true," - {0}.WM.MouseWheel: {1}", 
                                                    Host.Name, ((WM)m.Msg).ToString()); 
        if (ctl is MapPanel) {
          var keyState    = WindowsMouseInput.GetKeyStateWParam(m.WParam);
          var mult            = keyState.HasFlag(MouseKeys.Control) ? 5 : 1;
          keyState            = keyState &= ~MouseKeys.Control;
          var wheelDelta    = WindowsMouseInput.WheelDelta(m.WParam);
          // forward delta of +/- 30 instead of +/- 120; 30/120 == 1/4
          var newWparam   = WindowsMouseInput.WParam((Int16)(mult*wheelDelta/4), keyState);
          SendMessage(hWnd, m.Msg, newWparam, m.LParam);
          return true;
        } else if (ctl is MapFormOverview) {
          var keyState    = WindowsMouseInput.GetKeyStateWParam(m.WParam);
          var wheelDelta    =  WindowsMouseInput.WheelDelta(m.WParam);
          // forward delta of +/- 54 instead of +/- 120
          // 54 = 3 * 18 (default point height in pixels?); 54/120 == 9/20
          var newWparam   = WindowsMouseInput.WParam((Int16)(wheelDelta*9/20), keyState);
          SendMessage(hWnd, m.Msg, newWparam, m.LParam);
          return true;
        }
        break;
    }
  }
  return false;
    }
#region Extern declarations
/// <summary>P/Invoke declaration for user32.dll.WindowFromPoint</summary>
    /// <remarks><see cref="http://msdn.microsoft.com/en-us/library/windows/desktop/ms633558(v=vs.85).aspx"/></remarks>
    /// <param name="pt">(Sign-extended) screen coordinates as a Point structure.</param>
    /// <returns>Window handle (hWnd).</returns>
    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint(Point pt);
    /// <summary>P/Invoke declaration for user32.dll.SendMessage</summary>
    /// <param name="hWnd">Window handle</param>
    /// <param name="msg">Windows message</param>
    /// <param name="wp">WParam</param>
    /// <param name="lp">LParam</param>
    /// <returns></returns>
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
#endregion
#endregion