CreateGraphics questions

A

active

picInfo is a PictureBox that I write into when the mouse moves. Below is a
snip of the code.

The text flashes on the screen but does not stay there.

Anyone know why?



Second question.- Do I need to dispose of g?

Third question - Seems like Image should refer to something. How does
that happen after I set it to Nothing

........snip

picInfo.Visible = True


picInfo.Location = InfoLoc

picInfo.Image = Nothing 'Clear the image

Dim g As Graphics = picInfo.CreateGraphics

g.DrawString("Start X =" & CStr(mMarqueeStartPoint.X), New Font("Arial", 8),
New SolidBrush(Color.Black), 0, 0)

g.DrawString("Start Y=" & CStr(mMarqueeStartPoint.Y), New Font("Arial", 8),
New SolidBrush(Color.Black), 0, 10)

........snip

End Sub
 
K

Klaus Löffelmann

Whatever your name is,

Drawing in windows won't work the way, you tried it.
Everything you see in windows is not really persistent. If a window's moving
over another one, you have to take care that the overlapped window is
redrawing it's contense.

That means, you have to hook up to the paint-event or overwrite the
paint-method of your form. When you get the paint event, take care of
painting what has to be paint:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawString("Start X =" & CStr(mMarqueeStartPoint.X), New
Font("Arial", 8), New SolidBrush(Color.Black), 0, 0)
e.DrawString("Start Y=" & CStr(mMarqueeStartPoint.Y), New Font("Arial",
8),New SolidBrush(Color.Black), 0, 10)
End Sub

You get all necessary information in the PaintEventArgs-Object which is
accessable via e.

Hope that helps,

Klaus
 
H

Herfried K. Wagner [MVP]

* " active said:
picInfo is a PictureBox that I write into when the mouse moves. Below is a
snip of the code.

The text flashes on the screen but does not stay there.

Draw onto a bitmap instead and paint this bitmap to the picturebox in
its 'Paint' event handler.

How to draw onto the bitmap is shown here:

Second question.- Do I need to dispose of g?

Yes, in this case.
Third question - Seems like Image should refer to something. How does
that happen after I set it to Nothing

?!?

It doesn't refer to anything after setting it to 'Nothing'.
 
A

active

This is just a little feedback box a user gets if he presses a certain key.
It gives a few dimensions.
Persistent is not required since it goes away as soon as he releases the
key.

Thanks
Cal
 
A

active

"> > Third question - Seems like Image should refer to something. How
does
?!?

It doesn't refer to anything after setting it to 'Nothing'.

--
But doesn't the drawstring write on the Image (which now doesn't refer
toanything??)

picInfo.Image = Nothing 'Clear the image

Dim g As Graphics = picInfo.CreateGraphics

g.DrawString("Start X =" & CStr(mMarqueeStartPoint.X), New Font("Arial", 8),
New SolidBrush(Color.Black), 0, 0)



Thanks
Cal
 
A

active

Even though persistent is not required I tried the suggestions from both of
you and used a bitmap and the problem went away!


Thanks
Cal
 
H

Herfried K. Wagner [MVP]

* " active said:
Even though persistent is not required I tried the suggestions from both of
you and used a bitmap and the problem went away!

Don't forget to invalidate the form (for example by calling its
'Invalidate' method) if you make changes to the bitmap.

;-)
 
H

Herfried K. Wagner [MVP]

* " active said:
But doesn't the drawstring write on the Image (which now doesn't refer
toanything??)

picInfo.Image = Nothing 'Clear the image

Dim g As Graphics = picInfo.CreateGraphics

g.DrawString("Start X =" & CStr(mMarqueeStartPoint.X), New Font("Arial", 8),
New SolidBrush(Color.Black), 0, 0)

No, it refers to the device context of the picturebox.
 
A

active

"> > But doesn't the drawstring write on the Image (which now doesn't refer
No, it refers to the device context of the picturebox.

Now we're getting someplace.

Is BackgroundImage a bitmap in the DC?

And Image just another Bitmap in the control?

Thanks for the enlightenment.

Cal
 
H

Herfried K. Wagner [MVP]

* " active said:
Now we're getting someplace.

Is BackgroundImage a bitmap in the DC?

And Image just another Bitmap in the control?

Thanks for the enlightenment.

Both are images which are drawn automatically by the .NET framework if
they are set.
 

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