开发者

OnPaint override is never called

开发者 https://www.devze.com 2022-12-19 22:18 出处:网络
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.

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
    )开发者_JAVA技巧;
        }

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


I have the same problem just with a ProgressBar, when i try to override the OnPaint.. It's never called.


I found the solution here: http://web.archive.org/web/20140214234801/http://osix.net/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.


Call this.Invalidate() when you want to call OnPaint

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号