C# menustrip打开时不触发MouseEnter事件
我有一个包含多个项目的菜单提示,表单的其他部分有一个按钮。当鼠标进入按钮时,它会执行某些操作(MouseEnter事件)。我遇到的问题是,当menustrip打开时,如果鼠标进入按钮,MouseEnter事件不会被触发。菜单打开时有没有办法开火 从图形上看,这就是我正在做的: 我有一个菜单提示,每个父菜单项都有一个按钮。按钮将位于其上方,因此menustrip的唯一可见部分将是包含其子项的容器 现在,当鼠标进入按钮(例如“系统”)时,它会单击菜单项。这就是容器出现的原因。但一旦打开,若我想打开其他父母的任何其他容器,我必须先单击以失去焦点。然后,我想实现的是不必点击就可以完成 我想要的行为就像menustrip的行为一样。例如,如果系统打开,鼠标进入客户机,它会自动关闭系统并打开客户机C# menustrip打开时不触发MouseEnter事件,c#,winforms,mouseenter,menustrip,C#,Winforms,Mouseenter,Menustrip,我有一个包含多个项目的菜单提示,表单的其他部分有一个按钮。当鼠标进入按钮时,它会执行某些操作(MouseEnter事件)。我遇到的问题是,当menustrip打开时,如果鼠标进入按钮,MouseEnter事件不会被触发。菜单打开时有没有办法开火 从图形上看,这就是我正在做的: 我有一个菜单提示,每个父菜单项都有一个按钮。按钮将位于其上方,因此menustrip的唯一可见部分将是包含其子项的容器 现在,当鼠标进入按钮(例如“系统”)时,它会单击菜单项。这就是容器出现的原因。但一旦打开,若我想打开
让我们看看这是否是一个有效的答案:
#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
#区域IMessageFilter实现
///将WM_鼠标滚轮消息重定向到鼠标下的窗口。
///将WM_鼠标滚轮消息重定向到鼠标下的窗口(而不是
///这与重点)与调整三角洲。
///
///别忘了将其添加到构造函数:
///Application.AddMessageFilter(此);
///
///要筛选和/或处理的Windows消息。
///操作系统成功(真)或失败(假)。
[System.Security.Permissions.PermissionSetAttribute(
System.Security.Permissions.SecurityAction.Demand,Name=“FullTrust”)]
bool-IMessageFilter.PreFilterMessage(参考消息m){
//在这些坐标处确定窗口和控件。
//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){
开关(m.Msg){
默认:返回false;
案例(int)WM.MOUSEWHEEL:
DebugTracing.Trace(TraceFlag.ScrollEvents,true,“-{0}.WM.MouseWheel:{1}”,
Host.Name,((WM)m.Msg.ToString());
如果(ctl是映射面板){
var keyState=WindowsMouseInput.GetKeyStateWParam(m.WParam);
var mult=keyState.HasFlag(MouseKeys.Control)?5:1;
keyState=keyState&=~MouseKeys.Control;
var wheelDelta=WindowsMouseInput.wheelDelta(m.WParam);
//正向增量为+/-30,而不是+/-120;30/120==1/4
var newWparam=WindowsMouseInput.WParam((Int16)(mult*wheelDelta/4),键状态);
SendMessage(hWnd、m.Msg、newWparam、m.LParam);
返回true;
}else if(ctl为MapFormOverview){
var keyState=WindowsMouseInput.GetKeyStateWParam(m.WParam);
var wheelDelta=WindowsMouseInput.wheelDelta(m.WParam);
//正向增量为+/-54,而不是+/-120
//54=3*18(以像素为单位的默认点高度?;54/120==9/20
var newWparam=WindowsMouseInput.WParam((Int16)(wheelDelta*9/20),按键状态);
SendMessage(hWnd、m.Msg、newWparam、m.LParam);
返回true;
}
打破
}
}
返回false;
}
#区域外部声明
///user32.dll.WindowFromPoint的P/Invoke声明
///
///(符号扩展)屏幕坐标作为点结构。
///窗口句柄(hWnd)。
[DllImport(“user32.dll”)]
私有静态外部IntPtr WindowFromPoint(点pt);
///user32.dll.SendMessage的P/Invoke声明
///窗把手
///Windows消息
///WParam
///LParam
///
[DllImport(“user32.dll”,CharSet=CharSet.Auto)]
私有静态外部IntPtr SendMessage(IntPtr hWnd、int Msg、IntPtr wParam、IntPtr lParam);
#端区
#端区
您想要哪种类型的火灾..按键、功能键等等?????当菜单提示处于焦点时,事件将如何触发???您可以一次聚焦一个控件。只需选择菜单提示并按f4功能键…打开属性对话框…转到事件并选择您想要的任何类型…我只希望鼠标进入不同菜单项父项时的行为相同。它只是打开那个,然后关闭其余的。所以,当鼠标放在按钮上时,我想关闭所有打开的菜单项。这是因为该按钮将打开一个特定的菜单项。如果您能显示图像,以便我们更好地了解您的问题。这是对某些问题的回答,而不是这个问题。@Hans:OP询问了IMessageFilter的使用示例。是的,事实是我最终以不同的方式实现了菜单。不过还是要谢谢你:)