【问题标题】:Disable mouse capturing by Button通过 Button 禁用鼠标捕获
【发布时间】:2016-02-12 13:25:09
【问题描述】:

我创建了自定义的ContextMenuStrip,其中包含Button

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

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

【问题讨论】:

    标签: c# .net winforms toolstrip mousecapture


    【解决方案1】:

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

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

    然后就用它代替Button:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-10
      • 2017-04-05
      • 2010-12-26
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      相关资源
      最近更新 更多