在 C#/.NET 2.0 中控制双缓冲所有者绘制 UserControl 的设计器外观
我有一个所有者绘制的用户控件,我在其中实现了双缓冲。 为了让双缓冲在没有闪烁的情况下工作,我必须像这样重写 OnPaintBackground 事件:
protected override void OnPaintBackground(PaintEventArgs e)
{
// don't even have to do anything else
}
这在运行时效果很好。 问题是,当我在设计时在窗体上有一个控件实例时,它会变成一个黑洞,显示在其上拖动的任何窗口的轨迹(因为 OnPaintBackground 事件的重写也控制设计时外观)。 这只是一个表面问题,但它在视觉上很不和谐,并且总是导致项目的新开发人员认为出现了严重的错误。
有没有办法让这样的重写方法在设计时不被重写,或者还有其他解决方案吗?
I have an owner-drawn UserControl where I've implemented double-buffering. In order to get double-buffering to work without flicker, I have to override the OnPaintBackground event like so:
protected override void OnPaintBackground(PaintEventArgs e)
{
// don't even have to do anything else
}
This works great at runtime. The problem is that when I have an instance of the control on a form at design time, it becomes a black hole that shows trails of whatever windows are dragged over it (because the override of the OnPaintBackground event also governs design-time appearance). It's just a cosmetic problem, but it's visually jarring and it always leads new developers on the project to assume something has gone horribly wrong.
Is there any way to have an overridden method like this not be overridden at design time, or is there another solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,Steven Lowe 的解决方案涵盖了所有场景,特别是当用户控件出现时。
this.DesignMode 标志非常具有欺骗性。 它的唯一范围是检查直接父级是否在设计器内。
例如,如果您有一个 Form A 和一个 UserControl B,则在设计器中:
解决这个问题的方法是检查一个特殊的标志(对丑陋的 C++ 代码表示抱歉):
这个变量将始终显示正确的设计布尔值。
Steven Lowe's solution unfortunately cover all scenarios, espcially when user controls come into the picture.
The this.DesignMode flag is very deceptive. Its only scope is to check if the direct parent is within the designer.
For instance, if you have a Form A, and a UserControl B, in the designer:
The solution to this is a special flag to check (sorry for the ugly C++ code):
This variable will always show the proper design boolean.
C#中
greggorob64
的解决方案:The solution of
greggorob64
in C#: