禁用按钮捕获鼠标 [英] Disable mouse capturing by Button

查看:173
本文介绍了禁用按钮捕获鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用其中的Button创建自定义ContextMenuStrip:

I create custom ContextMenuStrip with Button in it:

ContextMenuStrip _contextMenu = new ContextMenuStrip();
_contextMenu.Items.Add(new ToolStripMenuItem("Item"));
_contextMenu.Items.Add(new ToolStripControlHost(new Button()));

当我打开此上下文菜单并将鼠标移到项目"上时,它将突出显示.但是,当我单击Button然后再次将鼠标移到项目"上时,它不再突出显示.看起来Button捕获鼠标. Button单击后如何避免这种情况或释放捕获?

When I open this context menu and move mouse over 'Item' it is highlighted. But after I clicked Button and then move mouse over 'Item' again it isn't highlighted anymore. Looks like Button captures mouse. How can I avoid this or release capturing after Button clicking?

推荐答案

您可以创建从Button继承的自己的按钮类,并将ControlStyles.Selectable设置为false,这将防止它成为焦点:

You can create your own button class inherited from Button and set ControlStyles.Selectable to false, this will prevent it from taking focus:

public class MyButton : Button
{
    public MyButton()
    {
        SetStyle(ControlStyles.Selectable, false);
    }
}

然后使用它代替Button:

_contextMenu.Items.Add(new ToolStripControlHost(new MyButton()));

这篇关于禁用按钮捕获鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆