Sometimes I need a custom appearence of a control. Or do alot of custom painting. I know I can do so with OnPaint
(see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx )
What is the right way to custom paint stuff on your .net application using OnPaint
? Are there any rules I have to keep in mind to keep my application optimized, and keep render time to a minimum?
Note: I have seen, and experienced, a lot of inefficient uses of OnPaint from time to time, therefore I created this Q&A.
To use OnPaint
efficiently you have to know a couple things:
OnPaint
of a control, eg of Form1
, is executed everytime the control is painted (duhh...) OnPaint
of Form1
is executed everytime a child control of Form1
is drawn. eg If you draw a dot in the upper right corner of Form1
using the OnPaint
of Form1
, while you have 150 child controls on Form1
, the dot will be drawn at least 150 times! It increases render time drastically. Especially if you do alot of custom drawing and calculations in the OnPaint
.
OnPaint
of a control, when that control has one or more child controls. Instead you should make a custom control, which holds no more child controls on it, that does the paint job. And place that as a child control on the parent control on the location where the custom drawing is needed. Form1
, you must use Form1.SuspendLayout()
(see: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.suspendlayout.aspx ) before you are adding the child controls. And Form1.ResumeLayout()
when you are done adding controls. This temporarily supresses the OnPaint
event, and decreases render time. OnPaint
events in the parent control. Eg place 4 textboxes beneath eachother so that they thouch eachother. So there is no background in between them and the controls are all painted in one OnPaint
event instead of 4 OnPaint
events. Of course this is not always possible as you do not want to glue all your components side by side. But it is worth to do, if performance is more important than looks, for example in a large custom 'datagrid' of somekind. OnPaint
event, as this invokes new OnPaint
events. If you must relocate/resize controls, you will have to add that somewhere else in your code, before the OnPaint
is invoked. For example, place relocation/resizing code in the OnLayout
or OnResize
or similar events. If you still think you must place that relocation/resizing code in the OnPaint
event in order for you application to work, than you are wrong, and you need to revise the logic of your code. Well I guess thats all, if something comes to mind that I forgot, i will update this Q&A. If I have forgotten something, or I made a mistake I would be glad to take note of it!
Hopefully this will give someone a headstart in using custom paint stuff in .Net, as I was looking for this information some time ago.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.