Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms: what is the best control for GDI+ drawing surface?

Tags:

winforms

I think you can do GDI+ drawing in just about any window in a .net windows forms project, but what do you recommend as the best built-in control for containing custom drawing? I'm going to be drawing my own x-y plots, text and images. I could use a Panel, UserControl, etc.

like image 530
P a u l Avatar asked Oct 31 '08 21:10

P a u l


People also ask

What method of a control will you override if you want to draw it with your custom code?

OnPaint. A control must provide rendering logic by overriding the OnPaint method that it inherits from Control.

What class is used to aid in drawing a custom control using your custom code?

Summary. User-defined controls can be created by inheriting the control class. UserControl class, or any windows forms existing control's class. The HorizontalScroll and VerticalScroll properties of the UserControl class can be used to define custom scroll bars.

Which handler we use to add graphics in Windows programming?

PaintEventArgs in the Paint Event Handler When programming the PaintEventHandler for controls or the PrintPage for a PrintDocument, a graphics object is provided as one of the properties of PaintEventArgs or PrintPageEventArgs.


2 Answers

If you're just drawing the control and not hosting children, then derive from Control - VS will make you a suitable class (it calls this a 'Custom Control') with the OnDraw handler already stubbed in.

If you're hosting other controls within your control, then derive from UserControl, (VS calls this a 'User Control' and you'll get support for VS designer, so you can drag around other controls on top.

For simple non-scrolling charting I would derive from Control.

like image 119
Will Dean Avatar answered Oct 12 '22 17:10

Will Dean


Do your GDI+ drawing in an offscreen bitmap surface, and then blit it to whatever control you need.

Your controls Paint method only needs to copy the surface to itself.

like image 31
FlySwat Avatar answered Oct 12 '22 16:10

FlySwat