View Single Post
 
Old 02-24-2004, 03:29 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,747
Default An Introduction to GDI+ in .Net, Part 2

GDI+ in Visual Basic .Net - Part 2

Final Recap from Part 1
Here is the final product from Part 1:
Code:
Imports System.Drawing Public Class Form1 Inherits System.Windows.Forms.Form 'The Windows-Designer code region would be here Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) 'This is the declare of the Graphics object Dim g As Graphics = e.Graphics 'Declare the pen Dim pPen As Pen = New Pen(Color.Maroon, 4) 'Let's draw us a line g.DrawLine(pPen, 10, 10, 110, 110) 'Declare the Rectangle Dim rRect As Rectangle = New Rectangle(150, 150, 100, 100) 'Fill it! g.FillRectangle(New SolidBrush(Color.Blue), rRect) 'Let's draw us a rectangle! g.DrawRectangle(pPen, rRect) 'Dim the font Dim fFont As Font = New Font("Verdana", 10, FontStyle.Bold) 'Draw some text g.DrawString("Hello World! I'm GDI!", fFont, New SolidBrush(Color.Orange), 30, 10) End Sub End Class

The Invalidate() Method and Its Practical Application
What is Invalidate()? Invalidate() will call the OnPaint event of the form again. This way, you can make an update on anything you have draw. Consider it as a way to redraw the form. Now what could one practical application of this be? How about a clock that is directly drawn onto the form? We can accomplish this with a timer created from System.Threading, and use the Invalidate() method every second.

Here’s what that would look like:
Code:
Imports System.Drawing Imports System.Threading Public Class Form1 Inherits System.Windows.Forms.Form Dim ThreadedTimer As System.Threading.Timer 'Windows designer code would be here #Region " Timer Code " 'This starts the timer Private Sub EnableTimer(ByVal iInterval As Integer) 'Declare the callback Dim tCallBack As New Threading.TimerCallback(AddressOf TimerTick) 'Create the timer ThreadedTimer = New System.Threading.Timer(tCallBack, Nothing, 0, iInterval) End Sub 'This will get rid of the timer Private Sub DisableTimer() 'Dispose of the timer and set the object to nothing ThreadedTimer.Dispose() ThreadedTimer = Nothing End Sub 'The timer callback sub Private Sub TimerTick(ByVal State As Object) 'This will cause us to redraw the form Me.Invalidate() End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Enable the timer EnableTimer(1000) End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing 'Disable the timer DisableTimer() End Sub #End Region Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) 'This is the declare of the Graphics object Dim g As Graphics = e.Graphics 'Declare the font Dim fFont As Font = New Font("Verdana", 10, FontStyle.Bold) 'Draw the text to the form g.DrawString(Now.Now, fFont, New SolidBrush(Color.DarkOrange), 10, 10) End Sub End Class
All this does is create a timer and a callback. In the form load, I set the interval to 1000 milliseconds, so our timer will tick once each second. And, when it will Tick, we Invalidate() the form. If you run this code, you will see that it clears the form, and draws the new time on it. This lets us make a clock without any controls!

This seems like such a simple method, but can really be helpful whenever we need to make our form redraw itself. If you have any questions regarding this tutorial, or any technical issues arise, just contact me through the forum’s PM system, or if I happen to be online (MSN) then just feel free to talk to me there.
Attached Files
File Type: zip GDIPlus_with_timer.zip (6.6 KB, 130 views)
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!

Last edited by PlenoJure; 02-24-2004 at 03:50 PM. Reason: Edit per request
Reply With Quote