Resolved Graphics in paint only?

gmillwater

New member
Joined
Jul 26, 2024
Messages
2
Programming Experience
10+
Not new to code, but new to vb net. Am I to understand that all my graphics routines are to be in the paint handler. If so, I need to stack all my subs and functions here? That seems pretty burdening on a graphic intensive program. Perhaps classing my subs and do a paint override in the class. I just don't know. This is new to me. Any insight would be helpful.

Thanks in advance.
 
Solution
In a Windows Forms app, any drawing on controls should be done on the Paint event, yes. There is the option for drawing on an Image object, which can be done any time, and then displaying that Image in/on the control. The latter can be done by either assigning that Image to an appropriate property or by drawing it onto the control on its Paint event. You might choose that option if the drawing is complex and/or it doesn;t change much or at all.

The reason it's done this way is because, when something changes or may have changed in the UI, some or all of a control will be redrawn and repainted on the screen. When that happens, anything that you have drawn previously will be erased and must be redrawn in...
In a Windows Forms app, any drawing on controls should be done on the Paint event, yes. There is the option for drawing on an Image object, which can be done any time, and then displaying that Image in/on the control. The latter can be done by either assigning that Image to an appropriate property or by drawing it onto the control on its Paint event. You might choose that option if the drawing is complex and/or it doesn;t change much or at all.

The reason it's done this way is because, when something changes or may have changed in the UI, some or all of a control will be redrawn and repainted on the screen. When that happens, anything that you have drawn previously will be erased and must be redrawn in order to remain visible. The part of the control - note that forms are controls too - that has or may have changed is invalidated and only that part gets repainted, but your drawing code should draw everything required. That means that your drawing code will work no matter what part of the control is being repainted. The execution of the drawing code is generally quite fast and it is the actual painting of pixels to the screen that is slow, so that part should be kept to a minimum, i.e. if you need to invalidate part of a control to be repainted, you shopuld specify the smallest area possible.

You have two choices of where to put your drawing code to be executed on a Paint event. If it is a one-off situation then you can create a handler in the relevant form or user control for the Paint event of that control, just as you would do for the Click event of a Button, for example. If you want to do the same thing in various places, either in the same project or in different projects, then you should create a custom control and override the OnPaint method, which is where the Paint even is raised. Any time you want to customise the benahiour of a derived class on a particular event, you do it in pretty much the same way: you override the method that raises the event and then add your code either before or after the event is raised, e.g.
VB.NET:
Protected Overrides Sub OnPaint(e As PaintEventArgs)
    'Add code here to draw before any client drawing is perfromed.

    MyBase.OnPaint(e)

    'Add code here to draw after any client drawing is perfromed.
End Sub
Note that you can still draw in the Paint event handler as well, so it's important to realise that any drawing you do before the event is raised will appear under that and any you do after the event is raised will apear over that.
 
Solution
Thank you for that informative description. It's very helpful. I can now determine the best methods for my madness.
 
Last edited by a moderator:
For future reference, please don't quote anything from other posts that you don't have to. Doing so makes threads - especially long threads, harder to follow. If you're just providing a general reply, especially to the last post, you don't need to quote anything at all because it adds nothing to your reply. If you're replying to a specific part of a particular post, possibly including the last post, then you should quote only that specific part and only if it's not otherwise obvious what you're responding to.
 
Back
Top