Expand Graphics Object Bounds ?

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
I have a Graphics clipping issue that I can't seem to figure out on my own, here... On a form that uses "Me.CreateGraphics" to draw a bargraph to the form, I want to create a graphics object in the Form.Load sub and use that same graphics object throughout the entire class. If the user starts the form at its smallest size and then resizes to a larger size, the graphics are clipped to what the form's size was when the graphics object was created. I could create a new graphics object every time the graph needs to be redrawn or resized to solve this, but it seems like a waste and I'm not entirely sure I'm not leaking memory resources by doing that. I've tried "graphics.SetClip" in an attempt to expand the bounds of the graphics object without success. Any insights ???
 
I didn't read your post past the half-way mark in your second sentence because what you're trying to do is just wrong in the first place. Do not EVER call CreateGraphics. It is unfortunate that a lot of GDI+ examples on the web do so for simplicity without mentioning that it is for simplicity and that it should NEVER be done in practice. If you want to draw on a form or other control then you do so on its Paint event only. That means overriding the OnPaint method inside the object or handling the Paint event outside the object. In the case of a form, you can do either. You put all your drawing code inside the OnPaint method or Paint event handler and then, whenever you want to change what's drawn, you call Refresh or Invalidate and Update. Here are a few examples of mine:

Very Simple Drawing Program
Manipulating GDI+ Drawings
Draw Common "Picture" on All Controls
 
Thanks for the reply JM, and in theory, I agree with your assessment. I have other form classes that override the paint event to handle the graphics, but on this particular form I had issues with flickering that Double Buffering wasn't suppressing as the process goes through various records and updates the graph as the data is processed. By using the Paint Event, the page is cleared prior to repainting and that creates the flicker. I found that updating the graph manually produced the kind of results I was looking for and that's why I chose to use the "CreateGraphics" function here. This way I can alter the graph without first clearing the page. In spite of the unconventional use of this function, I'm still trying to find a way to make this happen. If the user starts the form class in a maximized state, then everything works fine. I'm thinking, maybe I need to set the graphics object to it's maximum size when it is created... Not too sure how to do that, tho...
 
By using the Paint Event, the page is cleared prior to repainting and that creates the flicker.
You can invalidate only the parts you want to paint.
 
Back
Top