Using the Escape button to stop drawing? Where should I put it in my code?

  • Thread starter Thread starter JoshuaAT123
  • Start date Start date
J

JoshuaAT123

Guest
Hello. I'm trying to use the escape button to stop my application from drawing. However, its not working. I thought I could make a key down event for the form with the escape button to change the boolean Snapo to false, so that it would stop the code in my mouse move event because that's what makes my line look like its following the mouse. What should I do?


Imports System.Drawing.Drawing2D




Public Class Form1

Dim g As System.Drawing.Graphics
Dim r As System.Drawing.Graphics
Dim a As System.Drawing.Graphics


Dim col As Color
Dim D As Integer = 2
Dim p As Pen = New Pen(col, D)


Dim q As Pen = New Pen(Color.Black, 0)
Dim t As Pen = New Pen(Color.Blue, 5)
Dim n As Pen = New Pen(Color.Black, 5)
Dim count As Integer = 0
Dim trac As Integer = 1
Dim point1 As New Point()
Dim point2 As New Point()
Dim point3 As New Point()

Dim myPointsArray As New List(Of PointF)
Dim myPointsArrays(100)

Dim ShowGrid As Boolean = False
Dim Draw As Boolean = False
Dim TurnOnDrawL As Boolean = False
Dim StopConD As Boolean = False
Dim Snapo As Boolean = False
Dim Pushed As Boolean = True
Dim truethat As Boolean = True
Dim StopLoop As Boolean = False
Dim Key As Boolean = False


Dim isDrag As Boolean = False
Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
Dim startPoint As Point

Dim bmp As Bitmap
Dim SS As Bitmap






Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load


Me.Text = "Drawing Application Version 1.0"

bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)

PictureBox1.Image = bmp

g = PictureBox1.CreateGraphics


End Sub




Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint



End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim H, W As Integer



r = PictureBox1.CreateGraphics

D = Depth.Text
W = Width.Text
H = Height.Text

p.Color = col
p.Width = D

g = PictureBox1.CreateGraphics

gridon_Checked(sender, e)

g.DrawRectangle(p, 100, 30, 60 * W, 60 * H)

g.DrawRectangle(p, 100, 450, 60 * W, 60 * H)

g.DrawRectangle(p, 588, 449, 60 * W, 60 * H)

r.DrawLine(n, 500, 0, 500, 900)

r.DrawLine(n, 0, 313, 878, 313)

Lineop.Enabled = True

ResetButton.Enabled = True

Button1.Enabled = False








End Sub

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles Red.CheckedChanged


col = Color.Red
p.Color = col

End Sub


Private Sub White_CheckedChanged(sender As Object, e As EventArgs) Handles White.CheckedChanged

col = Color.White
p.Color = col

End Sub

Private Sub Blue_CheckedChanged(sender As Object, e As EventArgs) Handles Blue.CheckedChanged

col = Color.Blue
p.Color = col

End Sub

Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
p.DashStyle = Drawing2D.DashStyle.Solid
End Sub

Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
p.DashStyle = Drawing2D.DashStyle.Dash
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged

Draw = CheckBox1.Checked


End Sub

Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick



If Draw = True Then

count = count + 1

If count = 1 Then


point1 = New Point(e.X, e.Y)

End If


If count = 2 Then

point2 = New Point(e.X, e.Y)

g.DrawLine(p, point1, point2)

point1.X = point2.X
point1.Y = point2.Y

count = 1

End If


End If


End Sub


Private Sub About_Click(sender As Object, e As EventArgs) Handles About.Click

MessageBox.Show("Hi, my name is Joshua. Check out unpopularthemovie.com (It has a Bible message). Thanks for using this application. All rights, privaleges, and royalties are for Joshua Tashbar(maker of this application). Copyright created in 2018",
"About", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)


End Sub


Private Sub gridon_Checked(sender As Object, e As EventArgs) Handles gridon.CheckedChanged

ShowGrid = gridon.Checked

g = PictureBox1.CreateGraphics

If ShowGrid = True Then
q.Color = Color.Black
Else
q.Color = Color.LightGray
End If

For i As Integer = 1 To 44
g.DrawLine(q, 50 * i, 0, 50 * i, 735)
Next

For j As Integer = 1 To 37
g.DrawLine(q, 0, 50 * j, 878, 50 * j)
Next




End Sub



Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove

Dim q As New Pen(Color.LightGray, D)

'This z variable holds the remainder of the x coordinate of the mouse
Dim z As Integer

'This b variable holds the remainder of the y coordiante of the mouse
Dim b As Integer


If Snapo = True Then

If count = 1 Then

point3 = New Point(e.X, e.Y)

'Here is where I do the math for the lines to go on the grid

'This are the variables to hold the remainders of the division
z = e.X / 50
b = e.Y / 50

'The reason I divide and multiply by 50 is because thats the height and width of my squares in my grid
'These are the x and y coordinate values for the point 3
point3.X = z * 50
point3.Y = b * 50


'This makes a line of whatever color the user chooses (red for example)
g.DrawLine(p, point1, point3)

gridon_Checked(sender, e)

Button1_Click(sender, e)

'This line makes a line of lightgray so that the user cannot see it because thats what is the color the picture box
g.DrawLine(q, point1, point3)



End If

End If


End Sub

Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click

PictureBox1.Refresh()


End Sub

'Button to save picture
Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click

Dim SaveDLG As New SaveFileDialog

Take_Clip()

SaveDLG.Filter = "JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"

SaveDLG.Title = "Save File As"
SaveDLG.InitialDirectory = "C:\"

DialogResult = SaveDLG.ShowDialog

If DialogResult = Windows.Forms.DialogResult.OK Then

SS.Save(SaveDLG.FileName)



SS.Save("Drawing.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)

ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
End If



End Sub


Private Sub BStop_Click(sender As Object, e As EventArgs)

StopConD = True


End Sub

Private Sub Snapon_CheckedChanged(sender As Object, e As EventArgs) Handles Snapon.CheckedChanged


Snapo = Snapon.Checked

End Sub

Private Sub Lineop_Click(sender As Object, e As EventArgs) Handles Lineop.Click

While trac = 1



If trac = 1 Then
btnun.Enabled = True

btnre.Enabled = True

lblmessage.Text = "Exit New Line Drawing"


End If

trac = trac + 1

End While

If trac = 3 Then
btnun.Enabled = False

btnre.Enabled = False

lblmessage.Text = "..."

trac = 0

End If

trac = trac + 1



End Sub

Private Function CaptureControlArea(ctrl As Control) As Bitmap
Dim size As Size = ctrl.ClientSize
Dim tmpBmp As New Bitmap(size.Width, size.Height)
Dim g As Graphics
g = Graphics.FromImage(tmpBmp)
g.CopyFromScreen(ctrl.PointToScreen(New Drawing.Point(0, 0)), New Drawing.Point(0, 0), New Size(size.Width, size.Height))
Return tmpBmp
End Function

Private Sub Take_Clip()
SS = CaptureControlArea(PictureBox1)
End Sub

Private Sub lblmessage_KeyDown(sender As Object, e As KeyEventArgs) Handles lblmessage.KeyDown

End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then
Snapo = False
End If
End Sub

' Public Shared Function IsKeyDown(key As Key) As Boolean

'End Function

End Class

Continue reading...
 
Back
Top