2013-03-02 130 views
1

我有一個menuStrip,其中有幾個項目,並且在窗體的其他部分有一個按鈕。當鼠標進入按鈕時,它執行一些操作(MouseEnter事件)。我遇到的問題是,當菜單欄被打開時,如果鼠標進入按鈕,MouseEnter事件不會被觸發。菜單欄打開時有什麼方法可以啓動嗎?當菜單欄打開時,MouseEnter事件不會觸發

要以圖形方式看到這一點,這就是I'm做:

enter image description here

我有一個MenuStrip中,併爲每個父菜單項,有一個按鈕。該按鈕將位於其上方,因此菜單欄中唯一可見的部分將是帶有其子項的容器。

現在,當鼠標進入按鈕時,例如「系統」,它會在菜單項中執行一次單擊。這就是容器出現的原因。但一旦打開,如果我想打開其他父母的其他容器,我必須先點擊才能失去焦點。然後,我想要達到的目的就是在不點擊的情況下做到這一點。

我想要的行爲就像menutrip所具有的行爲。例如,如果打開系統,鼠標進入客戶端,它會自動關閉系統並打開客戶端。

enter image description here

+0

???????? ???? – 2013-03-02 20:05:07

+0

當您的菜單欄對焦時,事件如何發生?您可以一次集中一個控件。 – Shaharyar 2013-03-02 20:06:20

+0

只需選擇menuStrip然後按f4功能鍵...打開屬性對話框...進入事件並選擇任何你想要的類型....... – 2013-03-02 20:16:15

回答

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 
你會希望像that..key機,功能鍵等哪種類型的火災
+0

這是* something的答案,只是不是這個問題。 – 2013-03-02 22:42:59

+0

@Hans:OP要求提供一個使用IMessageFilter的例子。我手邊有這個。 – 2013-03-03 13:36:22

+0

是的,事實是,我最終以不同的方式實現了菜單。但是,無論如何感謝:) – Andres 2013-03-04 23:04:44

相關問題