codingdir logo sitemap sitemap |

How do I move a form's onPaint event to a custom class?

By : , Category : c#

The PaintEventHandler delegate (which is the type behind the Paint event) takes a sender parameter of type Object, not Form. (like all other standard event delegates)

If you change your handler method to take object sender (then cast it back to Form), it will work fine.

ReLated :

Here is a quick example of a subclass: It hides the original Image property and replaces it with one that does a rotation before assigning it:

class RotatedPictureBox : PictureBox

    private Image image;

    public new  Image Image {
        get { return image; }  // ?? you may want to undo the rotation here ??
        set {
              Bitmap bmp = value as Bitmap ;
              // use the rotation you need!
              if ( bmp != null )  bmp.RotateFlip(RotateFlipType.Rotate270FlipX);
              image = bmp;
              base.Image = Image;

    public RotatedPictureBox ()

Caveat: Assigning an Image seems to work but I didn't test it for all possible uses.. Known limitations

  • It doesn't rotate images assigned via ImageLocation.
  • I had a crash once, when assigning an image in the Designer, but can't reproduce.

Use the Control.Invalidate() method to force a control to be redrawn.

Shouldn't that be:

SendMessage(f.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);

Try to implement a event handler on Form.ResizeBegin, in which you will remove your OnPaint handler from your control.

Then you would just have to restore it in Form.ResizeEnd.


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .