Thursday, January 10, 2008

This is a Windows Form Graph Sheet script using GDI+ in VB.net 2008. You will need to have 2 labels 2 text boxes and a button on the form with appropriate names to build it.


GraphSheet 


Listing 1.


''''''
'' GraphSheet.vb Application Windows .NET 2008
''''''
'' Created by: Jose F. Sosa josefsosa@yahoo.com
'' Created On: 01/08/2008
''
''
Imports System.Drawing.Drawing2D
Imports System.Math

Public Class GraphSheet
Dim GBoxHeight As Single
Dim GBoxWidth As Single

Private Sub GraphSheet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub GraphSheet_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Using g As Graphics = e.Graphics
' set Back Ground to White Smoke
g.Clear(Color.WhiteSmoke)

' set Grid Height and Width to Input value as single
Dim gHight As Single = GBoxHeight + 0.0F
Dim gWidth As Single = GBoxWidth + 0.0F

' set Grid Height and Width at start
If gHight.ToString < 1 Then
gHight = 250
gWidth = 350
End If

' Draw and Fill BackGround White
Dim blackPen As New Pen(Color.Black, 2)
Dim Rect01 As RectangleF() = {New RectangleF(10.0F, 50.0F, gWidth, gHight)} 'Start1, End1, Start2, End12
e.Graphics.FillRectangles(Brushes.White, Rect01)

' x Axis Draw red Ordinate lines
Dim pnX As Pen = New Pen(Color.Red, 1) ' Red pen 1 pixel
For i As Integer = 50 To gHight + 50 Step 15 ' + 50 to compinstate for the 50 Pixels from the Top Margin + 15 Pixel Steps
g.DrawLine(pnX, 10, i, gWidth + 10, i) ' gWidth + 10 to compensate starting 10 pixels from the left margin
Next i

' y Axis Draw blue Abscissa lines
Dim pnY As Pen = New Pen(Color.Blue, 1) ' Blue Pen 1 Pixel
For i As Integer = 10 To gWidth + 10 Step 15 ' + 10 to compinstate for the 10 Pixels from the Left Margin
g.DrawLine(pnY, i, 50, i, gHight + 50) ' gHeight + 50 to compensate starting 10 pixels from the Top margin
Next i

' Outside border of bounding Rectangle I draw it after the Lines have been drawn
e.Graphics.DrawRectangles(blackPen, Rect01)

' Set up string parameters.
Dim g1 As Graphics = e.Graphics 'Create a Graphics Object or Canvas to draw on.
Dim drStr As New GraphicsPath
drStr.AddString("JFS Sample GraphSheet", New FontFamily("Arial"), 17, 26, New Point(30, 500), StringFormat.GenericTypographic)
g1.SmoothingMode = SmoothingMode.HighQuality
g1.FillPath(Brushes.RoyalBlue, drStr) 'Creat Blue Fill and DrawPath

End Using
End Sub

Private Sub DrawLines001_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Me.Text = e.X & ":" & e.Y 'Get X Y Coordinates
End Sub

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
GBoxHeight = txtHeight.Text
GBoxWidth = txtWidth.Text
Me.Refresh()
End Sub
End
Class

No comments: