我目前正在设计一个派生自 System.Windows.Forms.Control 的自定义选项卡控件类。
问题是在设计时没有触发任何事件(至少我测试过的事件,包括鼠标事件和键盘事件)。
这对我来说是一个问题,因为无法在设计器中的选项卡页面之间切换对用户来说非常不方便。我一直在做一些研究,看来我想要完成的事情是不可能的。这让我感到好奇,因为 .NET 框架附带的许多控件都支持设计时交互。以TabControl为例。您可以在设计时很好地在其页面之间切换。
所以我的问题是:有没有办法让鼠标和键盘事件在设计时工作?
另外,很抱歉我没有提供代码片段。但我认为其实没有必要。对于那些想要尝试的人,这里是我快速创建的一个简单的按钮类:
public class MyButton : Control
{
private bool hover = false;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Color color = hover ? Color.DarkBlue : Color.Blue;
e.Graphics.FillRectangle(new SolidBrush(color), DisplayRectangle);
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(DisplayRectangle.Location, new Size(DisplayRectangle.Width - 1, DisplayRectangle.Height - 1)));
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
hover = true;
Refresh();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
hover = false;
Refresh();
}
}
您将看到按钮的颜色在设计期间没有改变。
这需要一些工作——设计时交互与运行时是分开的。这可能有帮助:
http://www.codeproject.com/Articles/13531/Targeting-Design-Time-Events-of-User-Controls http://www.codeproject.com/Articles/13531/Targeting-Design-Time-Events-of-User-Controls
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)