在 C#/.NET 2.0 中控制双缓冲所有者绘制 UserControl 的设计器外观

发布于 2024-07-09 03:59:18 字数 417 浏览 5 评论 0原文

我有一个所有者绘制的用户控件,我在其中实现了双缓冲。 为了让双缓冲在没有闪烁的情况下工作,我必须像这样重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-07-16 03:59:19

不幸的是,Steven Lowe 的解决方案涵盖了所有场景,特别是当用户控件出现时。

this.DesignMode 标志非常具有欺骗性。 它的唯一范围是检查直接父级是否在设计器内。

例如,如果您有一个 Form A 和一个 UserControl B,则在设计器中:

  • A.DesignMode 在设计器中查看时为 true
  • B.DesignMode 在查看 A 时为 false,但在设计器中直接查看 B 时为 true。

解决这个问题的方法是检查一个特殊的标志(对丑陋的 C++ 代码表示抱歉):

 if(this->DesignMode || 
    LicenseManager::UsageMode == LicenseUsageMode::Designtime) 
    return;

这个变量将始终显示正确的设计布尔值。

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:

  • A.DesignMode is true when viewed in designer
  • B.DesignMode is false when viewing A, but true when looking directly at B in the designer.

The solution to this is a special flag to check (sorry for the ugly C++ code):

 if(this->DesignMode || 
    LicenseManager::UsageMode == LicenseUsageMode::Designtime) 
    return;

This variable will always show the proper design boolean.

我乃一代侩神 2024-07-16 03:59:19
if (this.DesignMode)
{
    return;    //or call base.OnPaintBackground()
}
if (this.DesignMode)
{
    return;    //or call base.OnPaintBackground()
}
幼儿园老大 2024-07-16 03:59:19

C#中greggorob64的解决方案:

if (DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime) 
{
    return;
}

The solution of greggorob64 in C#:

if (DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime) 
{
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文