c# - 永远不会调用 OnPaint 覆盖

标签 c# winforms

我已经做了几天了,这让我抓狂。我有一个继承自 System.Windows.Forms.Panel 的控件,我正试图覆盖 OnPaint。它很简单,完全忽略了它。

public class CollapsiblePanel : System.Windows.Forms.Panel
{
  public CollapsiblePanel()
  {
   //
   // Required for the Windows Form Designer
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
   SetStyle
    (
     ControlStyles.AllPaintingInWmPaint | 
     ControlStyles.UserPaint      | ControlStyles.DoubleBuffer   |
     ControlStyles.ResizeRedraw     | ControlStyles.Selectable ,
     true
    );
        }

  protected override void OnPaint(PaintEventArgs e)
  {
            // This never runs no matter what I try!
            base.OnPaint(e);
        }
}

最佳答案

当我尝试覆盖 OnPaint 时,我遇到了与 ProgressBar 相同的问题。它从未被调用。


我在这里找到了解决方案:http://web.archive.org/web/20140214234801/http://osix.net/modules/article/?id=826

你必须像这样创建一个构造函数并启用用户绘画:

SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);

默认值,可能因框架版本和操作系统而异。

关于c# - 永远不会调用 OnPaint 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2215393/

相关文章:

c# - Windows 窗体标签页事件

c# - 如何使用参数运行 Access 查询

c# - 使用 Single.TryParse 解析 float 失败

c# - 从 VisualStudio 2005 升级到 2010(对于 WinForms 项目)是否容易/安全?

c# - 基于两点绘制工字梁

C#,FormBorderStyle :None Restore Issue

c# - 错误 9 无法加载 Bootstrap 。设备连接组件

c# - 在 configSource 中使用外部 .config 文件会产生错误

c# - 如何检测 1 个 Entity Framework 对象的变化

c# - 声明按位枚举(标志)- "DWORD style"最近的 c# 语法怪癖?