Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to GDI

Status
Not open for further replies.

simian101

MIS
Joined
Nov 16, 2007
Messages
113
Location
US
I am just learning GDI. I am just trying to draw some lines etc. All the examples on the web occur from screen load. I want to draw when i hit a button. What do I need to change.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
Dim g As Graphics = pe.Graphics
Dim pn As New Pen(Color.Blue)
' Rectangle rect = new Rectangle(50, 50, 200, 100);
Dim pt1 As New Point(30, 30)
Dim pt2 As New Point(110, 100)
g.DrawLine(pn, pt1, pt2)

Dim fnt As New Font("Verdana", 16)
g.DrawString("GDI+ World", fnt, New SolidBrush(Color.Red), 60, 60)
End Sub
I think there is a problem with the sub parameters. But I am not sure how to fix it.

Thanks

Simi
 
As you're not in the Paint event you aren't passed the PaintEventArgs when the procedure is called, so you can't assign the graphics to pe.Graphics (as it's not there). So instead of that, you can get the graphics from the form by changing your code to:
Code:
Dim g As Graphics = Graphics.FromHwndInternal(Me.Handle)
One of the drawback of not using the Paint event however is that the graphics are only temporary, once the Paint is fired again (for example if a user minimizes and then restores the form) they will disappear.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Try this. I use this to write text when I build custom graphs.

g.DrawString("GDI+ World", New Font("Arial", 8, FontStyle.Bold), SystemBrushes.WindowText, New PointF(710, 80))

Let me know if that works!

Thanks,
Chilly442
 
Here's what I would do.

1. Create an in-memory, module-level bitmap variable in your class.

2. In your button click event, get a graphics object from your bitmap and draw on that object.

3. In your button click event make a call to repaint your form.

4. In your paint event of your form, draw the bitmap on the form.
 
Harley, your tip worked. And I see you point on the minimize.

River, I will work on that.

Thanks

Simi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top