This article describes an example of mouse drawing in VB. This example realizes that the line color and line width can be customized. When the mouse button is pressed, drawing starts and the initial starting point is recorded. If it is not in the drawing state, the process will be exited. In the drawing state, a straight line is drawn from the starting point to the current mouse point, and then the current mouse point is used as the new starting point. The drawing ends when the mouse button is released.
The specific function codes are as follows:
VERSION 5.00Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"Begin VB.Form Form1 Caption = "Mouse drawing" ClientHeight = 6420 ClientLeft = 60 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 = 240 TabIndex = 6 Top = 1800 Width = 1215 End Begin VB.OptionButton Option3 Caption = "4" Height = 375 Left = 240 TabIndex = 5 Top = 1320 Width = 1335 End Begin VB.OptionButton Option2 Caption = "2" Height = 375 Left = 240 TabIndex = 4 Top = 840 Width = 1095 End Begin VB.OptionButton Option1 Caption = "1" Height = 255 Left = 240 TabIndex = 3 Top = 480 Value = -1 'True Width = 1335 End End Begin VB.CommandButton Command1 Caption = "Set Color" Height = 495 Left = 5640 TabIndex = 1 Top = 600 Width = 1215 End Begin MSComDlg.CommonDialog CommonDialog1 Left = 4200 Top = 3840 _ExtentX = 847 _ExtentY = 847 _Version = 393216 End Begin VB.PictureBox Picture1 Height = 5535 Left = 480 ScaleHeight = 5475 ScaleWidth = 4515 TabIndex = 0 Top = 480 Width = 4575 EndEndAttribute VB_Name = "Form1"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseDim x1 As Integer 'Starting point X coordinate Dim y1 As Integer 'Starting point Y coordinate Dim x2 As Integer 'End point X coordinate Dim y2 As Integer 'End point Y coordinate Dim flag As Boolean 'Drawing flag' setting Line colorPrivate Sub Command1_Click() On Error Resume Next CommonDialog1.CancelError = True CommonDialog1.DialogTitle = "Color" CommonDialog1.ShowColor If Err <> 32755 Then Picture1.ForeColor = CommonDialog1.Color End IfEnd Sub'Clear the graphics in Picture1 Private Sub Command2_Click() Picture1.ClsEnd Sub'Set the line width Private Sub Option1_Click() Picture1.DrawWidth = 1End SubPrivate Sub Option2_Click() Picture1.DrawWidth = 2End SubPrivate Sub Option3_Click() Picture1.DrawWidth = 4End SubPrivate Sub Option4_Click() Picture1.DrawWidth = 8End SubPrivate Sub Form_Load() Picture1.Scale (0, 0)-(400, 400 ) flag = FalseEnd SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, _X As Single, Y As Single)'When the mouse button is pressed, drawing starts and records the initial starting point flag = True x1 = X y1 = YEnd SubPrivate Sub Picture1_MouseMove(Button As Integer , Shift As Integer, _X As Single, Y As Single)'If you are not in the drawing state, exit the process'If you are in the drawing state, draw a straight line from the starting point to the current mouse point'Then use the current mouse point as the new starting pointIf flag = False Then Exit Sub End If If flag = True Then x2 = Integer, Shift As Integer, _X As Single, Y As Single)' Drawing ends when the mouse button is released flag = FalseEnd Sub
There are relatively detailed comments in the program. I believe it will be easy for readers to understand. Readers can modify the program according to their own preferences to make it more perfect!