A Class Question

K

Keith Rebello

I have almost written a class to draw some vectors on either a PictureBox or
to a PrintDocument. Besides all the properties, the class has one method
named Draw.

In my class constructors, I send either a PictureBox or a PrintDocument and
get all the properties of the drawing surface to size my graphics.



The problem is that to actually draw the vectors, I need to have a Graphics
object which is obtained from either the "e as PaintEventArgs" in the
PictureBox_Paint event or "e as PrintPageEventArgs" in the PrintPage event
of the PrintDocument control.



This forces the user of my class to call the Draw method in either the Paint
or PrintPage events.



Is there a way to obtain the Graphics object from anywhere in code? Ideally
I would like to be able to obtain the Graphics object in either the
constructor or in the Draw method. Does this have something to do with
raising events? If so, how does one achieve this?



Any help would be greatly appreciated.
 
C

CJ Taylor

The control Class (System.Windows.Forms.Control) has a method called
CreateGraphics.

Try that out. =)

-CJ
 
K

Keith Rebello

Thanks CJ. The only problem is that I've read (in a few places) that it is
bad practice to use the Graphics object obtained from CreateGraphics to draw
on a control. It is supposed to be used only for getting information about
the Graphics object. Here is a short article on this point -
http://www.bobpowell.net/creategraphics.htm



Anyway, I may have no other choice.
 
C

CJ Taylor

Suddenly one man's opinion is gospel?

Also, what do you think they use to draw the control in the first place. =)
As far as I know, there isn't another way to get a graphics object, except
by the form (which inherits control).

I don't think they would give it to you unless they wanted it. Now, I can
understand that it would be bad because when a control is invalidated you
have an extra step to redrawing whatever it was on there, but thats not
really that big of a deal the way I see it.
 
K

Keith Rebello

I'm not taking anything as gospel - I'm just trying to learn.

I did try using CreateGraphics on a PictureBox and it worked. However, when
the form was moved or hidden the drawing disappeared - as you implied it
would in your reply. How then do I call my class' Draw method every time
the PictureBox is invalidated?

Thanks a lot for your replies.
 
C

CJ Taylor

Add a handler to the Invalidated event of that control.

this happens when the control is notified it needs to repaint...

you could add a handler to paint as well... which will give you your
graphics object if so desired. =)

-CJ
 
K

Keith Rebello

Thanks a million, CJ. That absolutely works and with handling the Paint
event I get the desired Graphics object. Furthermore, if I do my drawing on
a memory bitmap, all I have to do is transfer the image to the PictureBox
everytime the Paint event is fired.

Once again, your help is greatly appreciated.
 
C

CJ Taylor

anytime.

Keith Rebello said:
Thanks a million, CJ. That absolutely works and with handling the Paint
event I get the desired Graphics object. Furthermore, if I do my drawing on
a memory bitmap, all I have to do is transfer the image to the PictureBox
everytime the Paint event is fired.

Once again, your help is greatly appreciated.
 
R

Rafael M. Munoz [MSFT]

Hello CJ,

My name is Rafael M. Munoz and as you can see by my sig I am the MVP Lead
for various .NET products along wtih classic VB. I was hoping to talk to
you about the MVP program and was hoping that you could contact me directly
at (e-mail address removed) so that I could discuss it with you offline. I
look forward to hearing from you.

Thanks.

Rafael M. Munoz
Microsoft PSS Global Community
MVP Lead - .NET / VB / VC# / VJ#

This posting is provided 'AS IS' with no warranties, and confers no rights.
Microsoft Strategic Technology Protection Program for information -
http://www.microsoft.com/security.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top