注册的MouseEnter /鼠标离开事件,在Windows窗体禁用的控件
it1352
帮助1人
问题说明
我要注册的MouseEnter /鼠标离开事件被禁止的按钮。它不工作allthough它的工作为启用的按钮。
I want to register MouseEnter/MouseLeave events for disabled buttons. It does not work allthough it does work for enabled buttons..
//Enable Disable controls on form load
EnableDisableControls("Load");
var grupButtons = control.Controls.OfType<Button>();
foreach (Button btns in grupButtons)
{
//btns.MouseMove = new MouseEventHandler(MainframeDataExchangeTool_MouseMove);
btns.MouseEnter = new EventHandler(btns_MouseEnter);
btns.MouseLeave = new EventHandler(btns_MouseLeave);
}
private void btns_MouseEnter(object sender, EventArgs e)
{
}
private void btns_MouseLeave(object sender, EventArgs e)
{
var parent = sender as Control;
string tipstring = string.Empty;
if (parent == null)
{
return;
}
string enter = sender.GetType().ToString() ": MouseEnter";
}
这是工作的启动按钮...但要做些什么禁用按钮......我必须表明对的mouseenter提示操作,使之即消失立即在鼠标离开?
It is working for enable button ...but what to do for disable button ... I have to show tooltip operation on mouseenter and make it disapper immediately on mouseleave ?
正确答案
#1
您可以尝试一些表范围内的鼠标消息
这样的解决方案:
You can try some Form-wide Mouse message
solution like this:
//Suppose your disabled Button is button1
public partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
button1.Enabled = false;
button1.BackColor = Color.Green;
//Try this to see it in action
button1.MouseEnter = (s, e) => {
button1.BackColor = Color.Red;
};
button1.MouseLeave = (s, e) => {
button1.BackColor = Color.Green;
};
Application.AddMessageFilter(this);//Add the IMessageFilter to the current Application
}
bool entered;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x200) //WM_MOUSEMOVE = 0x200
{
if (Control.FromHandle(m.HWnd) == button1.Parent &&
button1.ClientRectangle.Contains(button1.PointToClient(MousePosition)))
{
if (!entered) {
entered = true;
//Raise the MouseEnter event via Reflection
typeof(Button).GetMethod("OnMouseEnter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.Invoke(button1, new[] { EventArgs.Empty });
}
}
else if (entered) {
//Raise the MouseLeave event via Reflection
typeof(Button).GetMethod("OnMouseLeave", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.Invoke(button1, new []{EventArgs.Empty});
entered = false;
}
}
return false;
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfhjaa
系列文章
更多
同类精品
更多
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20