This paper describes the implementation of the mouse drawing example for VB, this instance enables line color and line width to be set, and when the mouse button is pressed, the drawing begins and records the initial starting point, and if it is not in the drawing state, exit the process, and if it is in the drawing state, draw a line from the starting point to the point where the mouse is present, The current mouse location is then used as a new starting point, and the drawing ends when the mouse button is released.
The specific functional code is as follows:
VERSION 5.00 Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB} #1.2#0"; "Comdlg32.ocx" Begin VB. Form Form1 Caption = "mouse drawing" clientheight = 6420 ClientLeft = ClientTop = 345 ClientWidth = 7710 LinkTopic = "Form1" ScaleHeight = 6420 ScaleWidth = 7710 startupposition = 3 ' window default Begin VB. CommandButton Command2 Caption = "clear" Height = 495 left = 5640 TabIndex = 7 Top = 1440 Width = 1335 End Begin VB.
Frame Frame1 Caption = "line width" Height = 2655 left = 5520 TabIndex = 2 top = 2880 Width = 1935 Begin VB. OptionButton Option4 Caption = "8" Height = 495 left = The 6 top = 1800 Width = 1215 End Begin VB. OptionButton Option3 Caption = "4" Height = 375 left = the 5 top
= 1320 Width = 1335 End Begin VB. OptionButton Option2 Caption = "2" Height = 375 left = the 4 top = 840 Width = 1095 End Begin VB. OptionButton Option1 Caption = "1" Height = 255 left = the 3 top = The Value = 1 ' True Width = 1335 End Begin VB. CommandButton Command1 Caption = "Set color" Height = 495 left = 5640 TabIndex = 1 Top = Width = 1215 End Begin Mscomdlg.commondialog CommonDialog1 left = 4200 top = 384 0 _extentx = 847 _extenty = 847 _version = 393216 end Begin VB. PictureBox Picture1 Height = 5535 left = ScaleHeight = 5475 ScaleWidth = 4515 Tabinde x = 0 top = The 4575 end attribute vb_name = "Form1" attribute Vb_globalnamespace = F Alse Attribute Vb_creatable = false Attribute Vb_predeclaredid = True attribute vb_exposed = False Dim x1 as Integer ' start x coordinate Dim y1 as Integ Er ' start y coordinate Dim x2 As Integer ' endpoint x coordinate Dim y2 As Integer ' End y coordinate Dim flag as Boolean ' drawing mark ' set Line color Private Sub command1_cli CK () on Error Resume Next commondialog1.cancelerror = True commondialog1.dialogtitle = "Color" Commondialog1.showcolo R If Err <> 32755 Then picture1.forecolor = Commondialog1.color End If End Sub ' clears graphics Private Sub C in Picture1 Ommand2_click () Picture1.cls End Sub ' set line width Private Sub Option1_click () Picture1.drawwidth = 1 End Sub Private Sub Opti On2_click () Picture1.drawwidth = 2 End Sub Private Sub Option3_click () Picture1.drawwidth = 4 End Sub Private Sub Opti On4_click () Picture1.drawwidth = 8 End Sub Private Sub Form_Load () Picture1.scale (0, 0)-(a) flag = False End
Sub Private Sub Picture1_mousedown (Button As Integer, Shift As Integer, _x as single, Y as single) ' When the mouse button is pressed, the drawing starts and records the initial starting point
Flag = True X1 = X Y1 = Y End Sub Private Sub Picture1_mousemove (Button As Integer, Shift As Integer, _x as single, Y as single) ' If it's not in the drawing state
Exit the procedure ' If you are in a drawing state, draw a line from the start point to the current mouse point ' and then use the current mouse location as the new starting point if flag = False Then Exit Sub End If flag = True Then x2 = X y2 = Y picture1.line (x1, y1)-(x2, y2) x1 = x2 y1 = y2 end If End Sub Private Sub Picture1_mouseu
P (Button As Integer, Shift As Integer, _x as single, Y as single) ' drawing ends when the mouse button is released flag = False End Sub
The program with more detailed comments, I believe that readers can not be difficult to understand, the reader may be in accordance with their preferences for the program to modify to make it more perfect!