我已经做了几天了,这让我抓狂。我有一个继承自 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/