我已经待了几天,这让我发疯了.我有一个继承自System.Windows.Forms.Panel的控件,并且我试图覆盖OnPaint.只是简单而直接地忽略它.
I have been at this for a few days and it is driving me mad. I have a control that inherits from System.Windows.Forms.Panel and I'm trying to override OnPaint. It just plain, outright IGNORES it.
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的问题.它从未被调用.
I have the same problem just with a ProgressBar, when i try to override the OnPaint.. It's never called.
我在这里找到了解决方案: web.archive/web/20140214234801/osix/modules/article/?id=826
I found the solution here: web.archive/web/20140214234801/osix/modules/article/?id=826
您必须创建一个构造函数并启用用户绘画,如下所示:
You must create a constructor and enable user-painting like this:
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);默认值可能会因框架版本和操作系统而异.
Default values, probably vary depending on framework version and OS.
更多推荐
永远不会调用OnPaint覆盖
发布评论