Not sure what you mean, but if you want to add a verticle line as a seperator between controls on a toolbar then you add a Seperator control.
Or, are you talking about putting a button on the toolbar that when clicked allows the user to draw a line somewhere on the form You'd probably want to build a UserControl for drawing on the form...
There are a couple problems with this code that I want to point out. Don't get me wrong, you've got some good examples of the math involved with plotting shapes by points (as opposed to drawing the shapes with the appropriate method). But there are some issues with the way you do the drawing itself.
Number one is that you're using the CreateGraphics method of the picturebox to do your drawing. This is not desirable becuase any time the PictureBox is repainted, your drawing is lost. For any GDI drawing to be permanent it must be done either in response to the control's Paint event, or better yet, in an override of the control's base OnPaint method. This is why I suggested that the OP create a UserControl to do the drawing.
Another big issue is that your calling the CreateGraphics() method using inline code. This is a bad, bad, idea. When you use inline code such as:
You are not maintaining a reference to the Graphics object that is created; therefore you cannot call Dispose() on the object. This can lead to unreleased GDI Objects that waste system resources. Whenever you create a Graphics object, it should always have the Dispose() method called on it when your are finished. You also should not create a Graphics object inside of a loop. Delcare the Graphics object once outside the loop and then reuse it inside the loop. Dispose of it when the loop finishes.
Now, you may be thinking: "But the GarbageCollector will clean up those Graphics objects if I don't dispose of them". I thought the same thing but a long time ago there was a healthy forum discussion that proved that not disposing of Graphics objects causes wasted resources.
Never .Dispose a Graphics object unless you created it yourself.
In this code the Paint method includes the Graphics object in the
PaintEventArgs. You should not Dispose this object.
Private Sub ToolStrip1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ToolStrip1.Paint
Not sure on that one entirely but if you start with a code window in VB.Net and delete ALL of the code so you have an empty code window. The following code PASTED all at once will give you a working program. ;-)
Then take a look at my graphics demo code which has some comments.
The line command i have highlighted in ORANGE LIKE THIS
I know you can draw directly on a form too as i learnt some of the graphics colour change stuff from another user on >>
'-----------------------------START OF CODE--------------------------------------------------------------
Public
Class Form1
Inherits System.Windows.Forms.Form
#
Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents picBoxOutput As System.Windows.Forms.PictureBox
Friend WithEvents btnDemo1 As System.Windows.Forms.Button
Friend WithEvents btnDemo2 As System.Windows.Forms.Button
Friend WithEvents btnDemo3 As System.Windows.Forms.Button
Friend WithEvents btnDemo4 As System.Windows.Forms.Button
Friend WithEvents btnDemo5 As System.Windows.Forms.Button
Friend WithEvents btnDemo6 As System.Windows.Forms.Button
Friend WithEvents btnDemo7 As System.Windows.Forms.Button
Friend WithEvents btndemo8 As System.Windows.Forms.Button
Friend WithEvents btndemo9 As System.Windows.Forms.Button
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents btnDemo10 As System.Windows.Forms.Button
Friend WithEvents btnDemo11 As System.Windows.Forms.Button
Friend WithEvents btnDemo12 As System.Windows.Forms.Button
Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
Friend WithEvents txtColor As System.Windows.Forms.TextBox
Friend WithEvents btnColorDialog As System.Windows.Forms.Button
Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar
Friend WithEvents TrackBar2 As System.Windows.Forms.TrackBar
Friend WithEvents btnBackColour As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.picBoxOutput = New System.Windows.Forms.PictureBox
Me.btnExit = New System.Windows.Forms.Button
Me.btnDemo1 = New System.Windows.Forms.Button
Me.btnDemo2 = New System.Windows.Forms.Button
Me.btnDemo3 = New System.Windows.Forms.Button
Me.btnDemo4 = New System.Windows.Forms.Button
Me.btnDemo5 = New System.Windows.Forms.Button
Me.btnDemo6 = New System.Windows.Forms.Button
Me.btnDemo7 = New System.Windows.Forms.Button
Me.btndemo8 = New System.Windows.Forms.Button
Me.btndemo9 = New System.Windows.Forms.Button
Me.btnDemo10 = New System.Windows.Forms.Button
Me.btnDemo11 = New System.Windows.Forms.Button
Me.btnDemo12 = New System.Windows.Forms.Button
Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
Me.txtColor = New System.Windows.Forms.TextBox
Me.btnBackColour = New System.Windows.Forms.Button
Me.btnColorDialog = New System.Windows.Forms.Button
Me.TrackBar1 = New System.Windows.Forms.TrackBar
Me.TrackBar2 = New System.Windows.Forms.TrackBar
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'picBoxOutput
'
Me.picBoxOutput.BackColor = System.Drawing.Color.Black
Me.picBoxOutput.Location = New System.Drawing.Point(88, 8)
Me.picBoxOutput.Name = "picBoxOutput"
Me.picBoxOutput.Size = New System.Drawing.Size(780, 320)
Me.picBoxOutput.TabIndex = 0
Me.picBoxOutput.TabStop = False
'
'btnExit
'
Me.btnExit.BackColor = System.Drawing.Color.White
Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnExit.Location = New System.Drawing.Point(808, 384)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(75, 40)
Me.btnExit.TabIndex = 4
Me.btnExit.Text = "Exit"
'
'btnDemo1
'
Me.btnDemo1.BackColor = System.Drawing.Color.White
Me.btnDemo1.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo1.Location = New System.Drawing.Point(16, 384)
Me.btnDemo1.Name = "btnDemo1"
Me.btnDemo1.Size = New System.Drawing.Size(104, 40)
Me.btnDemo1.TabIndex = 5
Me.btnDemo1.Text = "Circle."
'
'btnDemo2
'
Me.btnDemo2.BackColor = System.Drawing.Color.White
Me.btnDemo2.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo2.Location = New System.Drawing.Point(16, 432)
Me.btnDemo2.Name = "btnDemo2"
Me.btnDemo2.Size = New System.Drawing.Size(104, 40)
Me.btnDemo2.TabIndex = 6
Me.btnDemo2.Text = "Star."
'
'btnDemo3
'
Me.btnDemo3.BackColor = System.Drawing.Color.White
Me.btnDemo3.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo3.Location = New System.Drawing.Point(16, 488)
Me.btnDemo3.Name = "btnDemo3"
Me.btnDemo3.Size = New System.Drawing.Size(104, 40)
Me.btnDemo3.TabIndex = 7
Me.btnDemo3.Text = "Cone."
'
'btnDemo4
'
Me.btnDemo4.BackColor = System.Drawing.Color.White
Me.btnDemo4.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo4.Location = New System.Drawing.Point(128, 384)
Me.btnDemo4.Name = "btnDemo4"
Me.btnDemo4.Size = New System.Drawing.Size(232, 40)
Me.btnDemo4.TabIndex = 8
Me.btnDemo4.Text = "Clockwise spiral."
'
'btnDemo5
'
Me.btnDemo5.BackColor = System.Drawing.Color.White
Me.btnDemo5.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo5.Location = New System.Drawing.Point(128, 432)
Me.btnDemo5.Name = "btnDemo5"
Me.btnDemo5.Size = New System.Drawing.Size(232, 40)
Me.btnDemo5.TabIndex = 9
Me.btnDemo5.Text = "Anticlockwise spiral."
'
'btnDemo6
'
Me.btnDemo6.BackColor = System.Drawing.Color.White
Me.btnDemo6.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo6.Location = New System.Drawing.Point(128, 488)
Me.btnDemo6.Name = "btnDemo6"
Me.btnDemo6.Size = New System.Drawing.Size(112, 40)
Me.btnDemo6.TabIndex = 10
Me.btnDemo6.Text = "Ellipse1."
'
'btnDemo7
'
Me.btnDemo7.BackColor = System.Drawing.Color.White
Me.btnDemo7.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo7.Location = New System.Drawing.Point(128, 536)
Me.btnDemo7.Name = "btnDemo7"
Me.btnDemo7.Size = New System.Drawing.Size(112, 40)
Me.btnDemo7.TabIndex = 11
Me.btnDemo7.Text = "Ellipse2."
'
'btndemo8
'
Me.btndemo8.BackColor = System.Drawing.Color.White
Me.btndemo8.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btndemo8.Location = New System.Drawing.Point(248, 488)
Me.btndemo8.Name = "btndemo8"
Me.btndemo8.Size = New System.Drawing.Size(184, 40)
Me.btndemo8.TabIndex = 12
Me.btndemo8.Text = "Starry ellipse1."
'
'btndemo9
'
Me.btndemo9.BackColor = System.Drawing.Color.White
Me.btndemo9.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btndemo9.Location = New System.Drawing.Point(248, 536)
Me.btndemo9.Name = "btndemo9"
Me.btndemo9.Size = New System.Drawing.Size(184, 40)
Me.btndemo9.TabIndex = 13
Me.btndemo9.Text = "Starry ellipse2."
'
'btnDemo10
'
Me.btnDemo10.BackColor = System.Drawing.Color.White
Me.btnDemo10.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo10.Location = New System.Drawing.Point(368, 384)
Me.btnDemo10.Name = "btnDemo10"
Me.btnDemo10.Size = New System.Drawing.Size(232, 40)
Me.btnDemo10.TabIndex = 14
Me.btnDemo10.Text = "Clockwise shell."
'
'btnDemo11
'
Me.btnDemo11.BackColor = System.Drawing.Color.White
Me.btnDemo11.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo11.Location = New System.Drawing.Point(368, 432)
Me.btnDemo11.Name = "btnDemo11"
Me.btnDemo11.Size = New System.Drawing.Size(232, 40)
Me.btnDemo11.TabIndex = 15
Me.btnDemo11.Text = "Anticlockwise shell."
'
'btnDemo12
'
Me.btnDemo12.BackColor = System.Drawing.Color.White
Me.btnDemo12.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDemo12.Location = New System.Drawing.Point(616, 384)
Me.btnDemo12.Name = "btnDemo12"
Me.btnDemo12.Size = New System.Drawing.Size(120, 40)
Me.btnDemo12.TabIndex = 16
Me.btnDemo12.Text = "Polygon."
'
'txtColor
'
Me.txtColor.Location = New System.Drawing.Point(640, 432)
Me.txtColor.Multiline = True
Me.txtColor.Name = "txtColor"
Me.txtColor.Size = New System.Drawing.Size(304, 40)
Me.txtColor.TabIndex = 18
Me.txtColor.Tag = "-1"
Me.txtColor.Text = ""
'
'btnBackColour
'
Me.btnBackColour.BackColor = System.Drawing.Color.White
Me.btnBackColour.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnBackColour.Location = New System.Drawing.Point(640, 528)
Me.btnBackColour.Name = "btnBackColour"
Me.btnBackColour.Size = New System.Drawing.Size(304, 40)
Me.btnBackColour.TabIndex = 19
Me.btnBackColour.Tag = "0"
Me.btnBackColour.Text = "Select background colour."
'
'btnColorDialog
'
Me.btnColorDialog.BackColor = System.Drawing.Color.White
Me.btnColorDialog.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnColorDialog.Location = New System.Drawing.Point(640, 480)
Me.btnColorDialog.Name = "btnColorDialog"
Me.btnColorDialog.Size = New System.Drawing.Size(304, 40)
Me.btnColorDialog.TabIndex = 20
Me.btnColorDialog.Text = "Select plot colour."
'
'TrackBar1
'
Me.TrackBar1.BackColor = System.Drawing.Color.DeepSkyBlue
Me.TrackBar1.Location = New System.Drawing.Point(72, 336)
Me.TrackBar1.Maximum = 780
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.Size = New System.Drawing.Size(808, 45)
Me.TrackBar1.TabIndex = 21
Me.TrackBar1.TickFrequency = 780
Me.TrackBar1.TickStyle = System.Windows.Forms.TickStyle.Both
Me.TrackBar1.Value = 180
'
'TrackBar2
'
Me.TrackBar2.BackColor = System.Drawing.Color.DeepSkyBlue
Me.TrackBar2.Location = New System.Drawing.Point(888, 8)
Me.TrackBar2.Maximum = 320
Me.TrackBar2.Name = "TrackBar2"
Me.TrackBar2.Orientation = System.Windows.Forms.Orientation.Vertical
Me.TrackBar2.Size = New System.Drawing.Size(45, 336)
Me.TrackBar2.TabIndex = 22
Me.TrackBar2.TickFrequency = 320
Me.TrackBar2.TickStyle = System.Windows.Forms.TickStyle.Both
Me.TrackBar2.Value = 160
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.RoyalBlue
Me.ClientSize = New System.Drawing.Size(952, 637)
Me.Controls.Add(Me.TrackBar2)
Me.Controls.Add(Me.TrackBar1)
Me.Controls.Add(Me.txtColor)
Me.Controls.Add(Me.btnColorDialog)
Me.Controls.Add(Me.btnBackColour)
Me.Controls.Add(Me.btnDemo12)
Me.Controls.Add(Me.btnDemo11)
Me.Controls.Add(Me.btnDemo10)
Me.Controls.Add(Me.btndemo9)
Me.Controls.Add(Me.btndemo8)
Me.Controls.Add(Me.btnDemo7)
Me.Controls.Add(Me.btnDemo6)
Me.Controls.Add(Me.btnDemo5)
Me.Controls.Add(Me.btnDemo4)
Me.Controls.Add(Me.btnDemo3)
Me.Controls.Add(Me.btnDemo2)
Me.Controls.Add(Me.btnDemo1)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.picBoxOutput)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Graphics demo."
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#
End Region
Dim m_Pen As New Pen(Color.Black, 1)
Dim m_GR As Graphics
Dim v_color As Integer = -1
Private Sub CLS()
Dim myBrush As New SolidBrush(Color.Black)
End Sub
Private Sub PlotCirc(ByVal X As Double, ByVal Y As Double, ByVal R As Double)
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = R
PI = Math.PI
'In this example the circumference is divided by
'10 and each point is then drawn to the centre.
'Clear the screen after last "DEMO" button click.
Next Count
End Sub
Private Sub btnDemo1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo1.Click
PlotCirc(160, 160, 150)
End Sub
Private Sub btnDemo2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo2.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
Dim convFactor As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
'
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = Math.PI
convFactor = 360 / (2 * PI)
'In this example the circumference is divided by
'30 and each point is then drawn to the centre.
'Clear the screen after last "DEMO" button click.
CLS()
For Count = 0 To 360 / convFactor Step (360 / convFactor) / 30
Next Count
End Sub
Private Sub btnDemo3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo3.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
'In this example the circumference is divided by
'50 and each point is then drawn to the point at 690,10
'giving a cone effect.
'Clear the screen after last "DEMO" button click.
Next Count
End Sub
Private Sub btnDemo4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo4.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together
'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians.
'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle).
'Clear the screen after last "DEMO" button click.
CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise
'starting the loop at zero makes the graphic start at 6 o'clock.
For Count = PI To 31 * PI Step 0.01
'Note the step value in the FOR loop is = to the value at the
'end of this radius reduction expression.
'The following line gradually decreases the radius
' by 10 pixels on each loop.
'Be careful here. You could use a While loop to check Radius is >=0
'otherwise plotting heads back outwards!!
Radius = Radius - (10 / ((2 * PI) / 0.01))
'Forcing the X2 direction plot negative with the "-Radius" part here forces
'plotting in a clockwise direction in relation to the Y2 value.
Next Count
End Sub
Private Sub btnDemo5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo5.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together
'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians.
'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle).
'Clear the screen after last "DEMO" button click.
CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise
'starting the loop at zero makes the graphic start at 6 o'clock.
For Count = PI To 31 * PI Step 0.01
'Note the step value in the FOR loop is = to the value at the
'end of this radius reduction expression.
'The following line gradually decreases the radius
' by 10 pixels on each loop.
'Be careful here. You could use a While loop to check Radius is >=0
'otherwise plotting heads back outwards!!
Radius = Radius - (10 / ((2 * PI) / 0.01))
'Forcing the X2 direction plot negative with the "-Radius" part here forces
'plotting in a clockwise direction in relation to the Y2 value.
Next Count
End Sub
Private Sub btnDemo6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo6.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference.
'Ellipse is created by dividing y2 by 2. Please see 2nd line within the FOR<>NEXT
'loop.
'Clear the screen after last "DEMO" button click.
Next Count
End Sub
Private Sub btnDemo7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo7.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference.
'Ellipse is created by dividing y2 by 2. Please see 2nd line within the FOR<>NEXT
'loop.
'Clear the screen after last "DEMO" button click.
Next Count
End Sub
Private Sub btndemo8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndemo8.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference.
'Ellipse is created bye dividing y2 by 2. Please see 2nd line within the FOR<>NEXT
'loop.
'Clear the screen after last "DEMO" button click.
Next Count
End Sub
Private Sub btndemo9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndemo9.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference.
'Ellipse is created bye dividing y2 by 2. Please see 2nd line within the FOR<>NEXT
'loop.
'Clear the screen after last "DEMO" button click.
Next Count
End Sub
Private Sub btnDemo10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo10.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together
'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians.
'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle).
'Clear the screen after last "DEMO" button click.
CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise
'starting the loop at zero makes the graphic start at 6 o'clock.
For Count = PI To 3 * PI Step 0.005
'Note the step value in the FOR loop is = to the value at the
'end of this radius reduction expression.
'The following line gradually decreases the radius
' by 10 pixels on each loop.
'Be careful here. You could use a While loop to check Radius is >=0
'otherwise plotting heads back outwards!!
Radius = Radius - (75 / ((2 * PI) / 0.005))
'Forcing the X2 direction plot negative with the "-Radius" part here forces
'plotting in a clockwise direction in relation to the Y2 value.
Next Count
End Sub
Private Sub btnDemo11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo11.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together
'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians.
'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle).
'Clear the screen after last "DEMO" button click.
CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise
'starting the loop at zero makes the graphic start at 6 o'clock.
For Count = PI To 3 * PI Step 0.005
'Note the step value in the FOR loop is = to the value at the
'end of this radius reduction expression.
'The following line gradually decreases the radius
' by 10 pixels on each loop.
'Be careful here. You could use a While loop to check Radius is >=0
'otherwise plotting heads back outwards!!
Next Count
End Sub
Private Sub btnDemo12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo12.Click
'x1 and y1 are the centre coordinates of the circle.
Dim x1 As Integer
Dim y1 As Integer
'x2 and y2 are the plotted positions that are calculated.
Dim x2 As Integer
Dim y2 As Integer
Dim Radius As Double
Dim PI As Double
'Just a loop Count variable to go "2*PI radians"=360 degrees
'for each circle loop.
Dim Count As Double
Dim blackBrush As New SolidBrush(Color.Black)
Next Count
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
Private Sub btnColorDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorDialog.Click
Me.ColorDialog1.ShowDialog()
txtColor.Text =
Me.ColorDialog1.Color.ToString
txtColor.Tag =
Me.ColorDialog1.Color.ToArgb
End Sub
Private Sub btnBackColour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackColour.Click
Me.ColorDialog1.ShowDialog()
txtColor.Text =
Me.ColorDialog1.Color.ToString
btnBackColour.Tag =
Me.ColorDialog1.Color.ToArgb
End Sub
Private Sub draw1(ByVal v_Color As Integer)
Dim m_GR = picBoxOutput.CreateGraphics
m_Pen.Color = ColorTranslator.FromHtml(v_Color)
m_GR.DrawLine(m_Pen, 20, 30, 800, 30)
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
TrackBar1.SmallChange = 1
TrackBar1.SetRange(0, 780)
End Sub
Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
TrackBar2.SmallChange = 1
TrackBar2.SetRange(0, 320)
End Sub
End
Class
'-----------------------------END OF CODE------------------------------------------
Sub ToolStrip1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ToolStrip1.Paint
Dim g As Graphics = e.Graphics
g.DrawLine(Pens.Black, 0, 200, 200, 0)
End Sub
Drawing a line
Martin Smyth
Not sure what you mean, but if you want to add a verticle line as a seperator between controls on a toolbar then you add a Seperator control.
Or, are you talking about putting a button on the toolbar that when clicked allows the user to draw a line somewhere on the form You'd probably want to build a UserControl for drawing on the form...
Giber
S_DS,
There are a couple problems with this code that I want to point out. Don't get me wrong, you've got some good examples of the math involved with plotting shapes by points (as opposed to drawing the shapes with the appropriate method). But there are some issues with the way you do the drawing itself.
Number one is that you're using the CreateGraphics method of the picturebox to do your drawing. This is not desirable becuase any time the PictureBox is repainted, your drawing is lost. For any GDI drawing to be permanent it must be done either in response to the control's Paint event, or better yet, in an override of the control's base OnPaint method. This is why I suggested that the OP create a UserControl to do the drawing.
Another big issue is that your calling the CreateGraphics() method using inline code. This is a bad, bad, idea. When you use inline code such as:
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x2 + 1, y2)
You are not maintaining a reference to the Graphics object that is created; therefore you cannot call Dispose() on the object. This can lead to unreleased GDI Objects that waste system resources. Whenever you create a Graphics object, it should always have the Dispose() method called on it when your are finished. You also should not create a Graphics object inside of a loop. Delcare the Graphics object once outside the loop and then reuse it inside the loop. Dispose of it when the loop finishes.
Now, you may be thinking: "But the GarbageCollector will clean up those Graphics objects if I don't dispose of them". I thought the same thing but a long time ago there was a healthy forum discussion that proved that not disposing of Graphics objects causes wasted resources.
White Hawk
Never .Dispose a Graphics object unless you created it yourself. In this code the Paint method includes the Graphics object in the PaintEventArgs. You should not Dispose this object.
Private Sub ToolStrip1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ToolStrip1.Paint
Dim g As Graphics = e.Graphics
g.DrawLine(Pens.Black, 0, 200, 200, 0)
g.Dispose() '<---------------REMOVE THIS LINE
End Sub
Luminita
Hi,
Not sure on that one entirely but if you start with a code window in VB.Net and delete ALL of the code so you have an empty code window. The following code PASTED all at once will give you a working program. ;-)
Then take a look at my graphics demo code which has some comments.
The line command i have highlighted in ORANGE LIKE THIS
I know you can draw directly on a form too as i learnt some of the graphics colour change stuff from another user on >>
http://www.programmersheaven.com/c/MsgBoard/wwwboard.asp Board=39&src=20&Setting=
'-----------------------------START OF CODE--------------------------------------------------------------
Public
Class Form1 Inherits System.Windows.Forms.Form#
Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer.InitializeComponent()
'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Thencomponents.Dispose()
End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents picBoxOutput As System.Windows.Forms.PictureBox Friend WithEvents btnDemo1 As System.Windows.Forms.Button Friend WithEvents btnDemo2 As System.Windows.Forms.Button Friend WithEvents btnDemo3 As System.Windows.Forms.Button Friend WithEvents btnDemo4 As System.Windows.Forms.Button Friend WithEvents btnDemo5 As System.Windows.Forms.Button Friend WithEvents btnDemo6 As System.Windows.Forms.Button Friend WithEvents btnDemo7 As System.Windows.Forms.Button Friend WithEvents btndemo8 As System.Windows.Forms.Button Friend WithEvents btndemo9 As System.Windows.Forms.Button Friend WithEvents btnExit As System.Windows.Forms.Button Friend WithEvents btnDemo10 As System.Windows.Forms.Button Friend WithEvents btnDemo11 As System.Windows.Forms.Button Friend WithEvents btnDemo12 As System.Windows.Forms.Button Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog Friend WithEvents txtColor As System.Windows.Forms.TextBox Friend WithEvents btnColorDialog As System.Windows.Forms.Button Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar Friend WithEvents TrackBar2 As System.Windows.Forms.TrackBar Friend WithEvents btnBackColour As System.Windows.Forms.Button<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent() Me.picBoxOutput = New System.Windows.Forms.PictureBox Me.btnExit = New System.Windows.Forms.Button Me.btnDemo1 = New System.Windows.Forms.Button Me.btnDemo2 = New System.Windows.Forms.Button Me.btnDemo3 = New System.Windows.Forms.Button Me.btnDemo4 = New System.Windows.Forms.Button Me.btnDemo5 = New System.Windows.Forms.Button Me.btnDemo6 = New System.Windows.Forms.Button Me.btnDemo7 = New System.Windows.Forms.Button Me.btndemo8 = New System.Windows.Forms.Button Me.btndemo9 = New System.Windows.Forms.Button Me.btnDemo10 = New System.Windows.Forms.Button Me.btnDemo11 = New System.Windows.Forms.Button Me.btnDemo12 = New System.Windows.Forms.Button Me.ColorDialog1 = New System.Windows.Forms.ColorDialog Me.txtColor = New System.Windows.Forms.TextBox Me.btnBackColour = New System.Windows.Forms.Button Me.btnColorDialog = New System.Windows.Forms.Button Me.TrackBar1 = New System.Windows.Forms.TrackBar Me.TrackBar2 = New System.Windows.Forms.TrackBar CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'picBoxOutput ' Me.picBoxOutput.BackColor = System.Drawing.Color.Black Me.picBoxOutput.Location = New System.Drawing.Point(88, 8) Me.picBoxOutput.Name = "picBoxOutput" Me.picBoxOutput.Size = New System.Drawing.Size(780, 320) Me.picBoxOutput.TabIndex = 0 Me.picBoxOutput.TabStop = False ' 'btnExit ' Me.btnExit.BackColor = System.Drawing.Color.White Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnExit.Location = New System.Drawing.Point(808, 384) Me.btnExit.Name = "btnExit" Me.btnExit.Size = New System.Drawing.Size(75, 40) Me.btnExit.TabIndex = 4 Me.btnExit.Text = "Exit" ' 'btnDemo1 ' Me.btnDemo1.BackColor = System.Drawing.Color.White Me.btnDemo1.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo1.Location = New System.Drawing.Point(16, 384) Me.btnDemo1.Name = "btnDemo1" Me.btnDemo1.Size = New System.Drawing.Size(104, 40) Me.btnDemo1.TabIndex = 5 Me.btnDemo1.Text = "Circle." ' 'btnDemo2 ' Me.btnDemo2.BackColor = System.Drawing.Color.White Me.btnDemo2.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo2.Location = New System.Drawing.Point(16, 432) Me.btnDemo2.Name = "btnDemo2" Me.btnDemo2.Size = New System.Drawing.Size(104, 40) Me.btnDemo2.TabIndex = 6 Me.btnDemo2.Text = "Star." ' 'btnDemo3 ' Me.btnDemo3.BackColor = System.Drawing.Color.White Me.btnDemo3.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo3.Location = New System.Drawing.Point(16, 488) Me.btnDemo3.Name = "btnDemo3" Me.btnDemo3.Size = New System.Drawing.Size(104, 40) Me.btnDemo3.TabIndex = 7 Me.btnDemo3.Text = "Cone." ' 'btnDemo4 ' Me.btnDemo4.BackColor = System.Drawing.Color.White Me.btnDemo4.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo4.Location = New System.Drawing.Point(128, 384) Me.btnDemo4.Name = "btnDemo4" Me.btnDemo4.Size = New System.Drawing.Size(232, 40) Me.btnDemo4.TabIndex = 8 Me.btnDemo4.Text = "Clockwise spiral." ' 'btnDemo5 ' Me.btnDemo5.BackColor = System.Drawing.Color.White Me.btnDemo5.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo5.Location = New System.Drawing.Point(128, 432) Me.btnDemo5.Name = "btnDemo5" Me.btnDemo5.Size = New System.Drawing.Size(232, 40) Me.btnDemo5.TabIndex = 9 Me.btnDemo5.Text = "Anticlockwise spiral." ' 'btnDemo6 ' Me.btnDemo6.BackColor = System.Drawing.Color.White Me.btnDemo6.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo6.Location = New System.Drawing.Point(128, 488) Me.btnDemo6.Name = "btnDemo6" Me.btnDemo6.Size = New System.Drawing.Size(112, 40) Me.btnDemo6.TabIndex = 10 Me.btnDemo6.Text = "Ellipse1." ' 'btnDemo7 ' Me.btnDemo7.BackColor = System.Drawing.Color.White Me.btnDemo7.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo7.Location = New System.Drawing.Point(128, 536) Me.btnDemo7.Name = "btnDemo7" Me.btnDemo7.Size = New System.Drawing.Size(112, 40) Me.btnDemo7.TabIndex = 11 Me.btnDemo7.Text = "Ellipse2." ' 'btndemo8 ' Me.btndemo8.BackColor = System.Drawing.Color.White Me.btndemo8.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btndemo8.Location = New System.Drawing.Point(248, 488) Me.btndemo8.Name = "btndemo8" Me.btndemo8.Size = New System.Drawing.Size(184, 40) Me.btndemo8.TabIndex = 12 Me.btndemo8.Text = "Starry ellipse1." ' 'btndemo9 ' Me.btndemo9.BackColor = System.Drawing.Color.White Me.btndemo9.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btndemo9.Location = New System.Drawing.Point(248, 536) Me.btndemo9.Name = "btndemo9" Me.btndemo9.Size = New System.Drawing.Size(184, 40) Me.btndemo9.TabIndex = 13 Me.btndemo9.Text = "Starry ellipse2." ' 'btnDemo10 ' Me.btnDemo10.BackColor = System.Drawing.Color.White Me.btnDemo10.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo10.Location = New System.Drawing.Point(368, 384) Me.btnDemo10.Name = "btnDemo10" Me.btnDemo10.Size = New System.Drawing.Size(232, 40) Me.btnDemo10.TabIndex = 14 Me.btnDemo10.Text = "Clockwise shell." ' 'btnDemo11 ' Me.btnDemo11.BackColor = System.Drawing.Color.White Me.btnDemo11.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo11.Location = New System.Drawing.Point(368, 432) Me.btnDemo11.Name = "btnDemo11" Me.btnDemo11.Size = New System.Drawing.Size(232, 40) Me.btnDemo11.TabIndex = 15 Me.btnDemo11.Text = "Anticlockwise shell." ' 'btnDemo12 ' Me.btnDemo12.BackColor = System.Drawing.Color.White Me.btnDemo12.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnDemo12.Location = New System.Drawing.Point(616, 384) Me.btnDemo12.Name = "btnDemo12" Me.btnDemo12.Size = New System.Drawing.Size(120, 40) Me.btnDemo12.TabIndex = 16 Me.btnDemo12.Text = "Polygon." ' 'txtColor ' Me.txtColor.Location = New System.Drawing.Point(640, 432) Me.txtColor.Multiline = True Me.txtColor.Name = "txtColor" Me.txtColor.Size = New System.Drawing.Size(304, 40) Me.txtColor.TabIndex = 18 Me.txtColor.Tag = "-1" Me.txtColor.Text = "" ' 'btnBackColour ' Me.btnBackColour.BackColor = System.Drawing.Color.White Me.btnBackColour.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnBackColour.Location = New System.Drawing.Point(640, 528) Me.btnBackColour.Name = "btnBackColour" Me.btnBackColour.Size = New System.Drawing.Size(304, 40) Me.btnBackColour.TabIndex = 19 Me.btnBackColour.Tag = "0" Me.btnBackColour.Text = "Select background colour." ' 'btnColorDialog ' Me.btnColorDialog.BackColor = System.Drawing.Color.White Me.btnColorDialog.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnColorDialog.Location = New System.Drawing.Point(640, 480) Me.btnColorDialog.Name = "btnColorDialog" Me.btnColorDialog.Size = New System.Drawing.Size(304, 40) Me.btnColorDialog.TabIndex = 20 Me.btnColorDialog.Text = "Select plot colour." ' 'TrackBar1 ' Me.TrackBar1.BackColor = System.Drawing.Color.DeepSkyBlue Me.TrackBar1.Location = New System.Drawing.Point(72, 336) Me.TrackBar1.Maximum = 780 Me.TrackBar1.Name = "TrackBar1" Me.TrackBar1.Size = New System.Drawing.Size(808, 45) Me.TrackBar1.TabIndex = 21 Me.TrackBar1.TickFrequency = 780 Me.TrackBar1.TickStyle = System.Windows.Forms.TickStyle.Both Me.TrackBar1.Value = 180 ' 'TrackBar2 ' Me.TrackBar2.BackColor = System.Drawing.Color.DeepSkyBlue Me.TrackBar2.Location = New System.Drawing.Point(888, 8) Me.TrackBar2.Maximum = 320 Me.TrackBar2.Name = "TrackBar2" Me.TrackBar2.Orientation = System.Windows.Forms.Orientation.Vertical Me.TrackBar2.Size = New System.Drawing.Size(45, 336) Me.TrackBar2.TabIndex = 22 Me.TrackBar2.TickFrequency = 320 Me.TrackBar2.TickStyle = System.Windows.Forms.TickStyle.Both Me.TrackBar2.Value = 160 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.BackColor = System.Drawing.Color.RoyalBlue Me.ClientSize = New System.Drawing.Size(952, 637) Me.Controls.Add(Me.TrackBar2) Me.Controls.Add(Me.TrackBar1) Me.Controls.Add(Me.txtColor) Me.Controls.Add(Me.btnColorDialog) Me.Controls.Add(Me.btnBackColour) Me.Controls.Add(Me.btnDemo12) Me.Controls.Add(Me.btnDemo11) Me.Controls.Add(Me.btnDemo10) Me.Controls.Add(Me.btndemo9) Me.Controls.Add(Me.btndemo8) Me.Controls.Add(Me.btnDemo7) Me.Controls.Add(Me.btnDemo6) Me.Controls.Add(Me.btnDemo5) Me.Controls.Add(Me.btnDemo4) Me.Controls.Add(Me.btnDemo3) Me.Controls.Add(Me.btnDemo2) Me.Controls.Add(Me.btnDemo1) Me.Controls.Add(Me.btnExit) Me.Controls.Add(Me.picBoxOutput) Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Graphics demo." CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit() CType(Me.TrackBar2, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub#
End Region Dim m_Pen As New Pen(Color.Black, 1) Dim m_GR As Graphics Dim v_color As Integer = -1 Private Sub CLS() Dim myBrush As New SolidBrush(Color.Black)v_color =
CInt(btnBackColour.Tag)myBrush.Color = ColorTranslator.FromHtml(v_color)
'picBoxOutput.CreateGraphics.FillRectangle(myBrush, 0, 0, 780, 320)picBoxOutput.CreateGraphics.Clear(Color.FromArgb(v_color))
End Sub Private Sub PlotCirc(ByVal X As Double, ByVal Y As Double, ByVal R As Double) 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = R
PI = Math.PI
'In this example the circumference is divided by '10 and each point is then drawn to the centre. 'Clear the screen after last "DEMO" button click.CLS()
For Count = 0 To 2 * PI Step 0.05x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x2 + 1, y2)
Next Count End Sub Private Sub btnDemo1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo1.ClickPlotCirc(160, 160, 150)
End Sub Private Sub btnDemo2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo2.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double Dim convFactor As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black) 'x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = Math.PI
convFactor = 360 / (2 * PI)
'In this example the circumference is divided by '30 and each point is then drawn to the centre. 'Clear the screen after last "DEMO" button click.CLS()
For Count = 0 To 360 / convFactor Step (360 / convFactor) / 30x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x1, y1)
Next Count End Sub Private Sub btnDemo3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo3.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
'In this example the circumference is divided by '50 and each point is then drawn to the point at 690,10 'giving a cone effect. 'Clear the screen after last "DEMO" button click.CLS()
For Count = 0 To 2 * PI Step (2 * PI) / 50x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, 400, 10)
Next Count End Sub Private Sub btnDemo4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo4.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together 'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians. 'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle). 'Clear the screen after last "DEMO" button click.CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise 'starting the loop at zero makes the graphic start at 6 o'clock. For Count = PI To 31 * PI Step 0.01 'Note the step value in the FOR loop is = to the value at the 'end of this radius reduction expression. 'The following line gradually decreases the radius ' by 10 pixels on each loop. 'Be careful here. You could use a While loop to check Radius is >=0 'otherwise plotting heads back outwards!!Radius = Radius - (10 / ((2 * PI) / 0.01))
'Forcing the X2 direction plot negative with the "-Radius" part here forces 'plotting in a clockwise direction in relation to the Y2 value.x2 = Math.Round(-Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x2 + 1, y2 + 1)
Next Count End Sub Private Sub btnDemo5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo5.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together 'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians. 'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle). 'Clear the screen after last "DEMO" button click.CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise 'starting the loop at zero makes the graphic start at 6 o'clock. For Count = PI To 31 * PI Step 0.01 'Note the step value in the FOR loop is = to the value at the 'end of this radius reduction expression. 'The following line gradually decreases the radius ' by 10 pixels on each loop. 'Be careful here. You could use a While loop to check Radius is >=0 'otherwise plotting heads back outwards!!Radius = Radius - (10 / ((2 * PI) / 0.01))
'Forcing the X2 direction plot negative with the "-Radius" part here forces 'plotting in a clockwise direction in relation to the Y2 value.x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x2 + 1, y2 + 1)
Next Count End Sub Private Sub btnDemo6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo6.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference. 'Ellipse is created by dividing y2 by 2. Please see 2nd line within the FOR<>NEXT 'loop. 'Clear the screen after last "DEMO" button click.CLS()
For Count = 0 To 2 * PI Step 0.05x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius / 2 * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x2 + 1, y2 + 1)
Next Count End Sub Private Sub btnDemo7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo7.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference. 'Ellipse is created by dividing y2 by 2. Please see 2nd line within the FOR<>NEXT 'loop. 'Clear the screen after last "DEMO" button click.CLS()
For Count = 0 To 2 * PI Step 0.05x2 = Math.Round(Radius / 2 * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x2 + 1, y2 + 1)
Next Count End Sub Private Sub btndemo8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndemo8.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference. 'Ellipse is created bye dividing y2 by 2. Please see 2nd line within the FOR<>NEXT 'loop. 'Clear the screen after last "DEMO" button click.CLS()
For Count = 0 To 2 * PI Step (2 * PI) / 40x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius / 2 * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x1, y1)
Next Count End Sub Private Sub btndemo9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndemo9.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points on an ellipse circumference. 'Ellipse is created bye dividing y2 by 2. Please see 2nd line within the FOR<>NEXT 'loop. 'Clear the screen after last "DEMO" button click.CLS()
For Count = 0 To 2 * PI Step (2 * PI) / 40x2 = Math.Round(Radius / 2 * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x1, y1)
Next Count End Sub Private Sub btnDemo10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo10.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together 'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians. 'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle). 'Clear the screen after last "DEMO" button click.CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise 'starting the loop at zero makes the graphic start at 6 o'clock. For Count = PI To 3 * PI Step 0.005 'Note the step value in the FOR loop is = to the value at the 'end of this radius reduction expression. 'The following line gradually decreases the radius ' by 10 pixels on each loop. 'Be careful here. You could use a While loop to check Radius is >=0 'otherwise plotting heads back outwards!!Radius = Radius - (75 / ((2 * PI) / 0.005))
'Forcing the X2 direction plot negative with the "-Radius" part here forces 'plotting in a clockwise direction in relation to the Y2 value.x2 = Math.Round(-Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x1, y1)
Next Count End Sub Private Sub btnDemo11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo11.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 150
PI = 355 / 113
' This is a maths method to plot lots of points joined together 'to produce a spiral of 15 turns (31*PI)-PI=(30*PI) radians. 'Note if you see the earlier examples. 2*PI= 360 degrees ( a full circle). 'Clear the screen after last "DEMO" button click.CLS()
' The loop starts at PI value to start at 12 o'clock position otherwise 'starting the loop at zero makes the graphic start at 6 o'clock. For Count = PI To 3 * PI Step 0.005 'Note the step value in the FOR loop is = to the value at the 'end of this radius reduction expression. 'The following line gradually decreases the radius ' by 10 pixels on each loop. 'Be careful here. You could use a While loop to check Radius is >=0 'otherwise plotting heads back outwards!!Radius = Radius - (75 / ((2 * PI) / 0.005))
x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLine(m_Pen, x2, y2, x1, y1)
Next Count End Sub Private Sub btnDemo12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo12.Click 'x1 and y1 are the centre coordinates of the circle. Dim x1 As Integer Dim y1 As Integer 'x2 and y2 are the plotted positions that are calculated. Dim x2 As Integer Dim y2 As Integer Dim Radius As Double Dim PI As Double 'Just a loop Count variable to go "2*PI radians"=360 degrees 'for each circle loop. Dim Count As Double Dim blackBrush As New SolidBrush(Color.Black)x1 = TrackBar1.Value
y1 = 320 - TrackBar2.Value
Radius = 160
PI = 355 / 113
'Clear the screen after last "DEMO" button click.CLS()
Dim Pen1 As New Pen(Color.Blue, 1)x1 = Math.Round(Radius * Math.Sin(0) + x1, 3) - Radius
For Count = 0 To 2 * PI Step (2 * PI) / 10x2 = Math.Round(Radius * Math.Sin(Count) + x1, 3)
y2 = Math.Round(Radius * Math.Cos(Count) + y1, 3)
' Create array of points that define lines to draw. Dim points As PointF() = {New PointF(x2, y2), _ New PointF(x1, y1)} 'Draw lines to screen.v_color =
CInt(txtColor.Tag)m_Pen.Color = ColorTranslator.FromHtml(v_color)
picBoxOutput.CreateGraphics.DrawLines(m_Pen, points)
x1 = x2
y1 = y2
Next Count End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click End End Sub Private Sub btnColorDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColorDialog.Click Me.ColorDialog1.ShowDialog()txtColor.Text =
Me.ColorDialog1.Color.ToStringtxtColor.Tag =
Me.ColorDialog1.Color.ToArgb End Sub Private Sub btnBackColour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackColour.Click Me.ColorDialog1.ShowDialog()txtColor.Text =
Me.ColorDialog1.Color.ToStringbtnBackColour.Tag =
Me.ColorDialog1.Color.ToArgb End Sub Private Sub draw1(ByVal v_Color As Integer) Dim m_GR = picBoxOutput.CreateGraphicsm_Pen.Color = ColorTranslator.FromHtml(v_Color)
m_GR.DrawLine(m_Pen, 20, 30, 800, 30)
End Sub Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.ScrollTrackBar1.SmallChange = 1
TrackBar1.SetRange(0, 780)
End Sub Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.ScrollTrackBar2.SmallChange = 1
TrackBar2.SetRange(0, 320)
End SubEnd
Class'-----------------------------END OF CODE------------------------------------------
Regards,
S_DS.
A__alex10
Use the paint event of your toolbar...
Private
Sub ToolStrip1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles ToolStrip1.Paint Dim g As Graphics = e.Graphics g.DrawLine(Pens.Black, 0, 200, 200, 0) End Sub