用VB制作绘图板怎样做到点中一个点后,移动鼠标会有一条直线跟随鼠标移动,点击第二个点后确定这条直线?

解决方案 »

  1.   

    下面是个很很简简单单的例子
    在窗体上放个Line控件,然后Visible设为False先Option ExplicitPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Line1.X2 = X
        Line1.Y2 = Y
    End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Line1.Visible = True
        Line1.X1 = X
        Line1.Y1 = Y
    End Sub
      

  2.   

    Option Explicit
    Dim a As BooleanPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Line1.X1 = X
        Line1.Y1 = Y
        a = True
    End SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If a Then
        Line1.X2 = X
        Line1.Y2 = Y
    End If
    End SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
     If a Then
        Line1.X2 = X
        Line1.Y2 = Y
        Line1.Visible = True
        a = Not a
    End If
    End Sub
      

  3.   

    一个例子:
    Dim s As String
    Dim first As Boolean
    Dim showline As Boolean
    Private Sub Form_Load()
    first = True
    showline = False
    End Sub
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If first = True Then
    Dim ct As Control
    For Each ct In Form1.Controls
        If UCase(TypeName(ct)) = "LINE" Then c = c + 1
    Next
    s = "line" & Trim(Str(c + 1)) Form1.Controls.Add "VB.line", s, Picture1
       With Form1.Controls(s)
          
          .X1 = X
          .Y1 = Y    End With
    first = False
    showline = True
    Else
    With Form1.Controls(s)
          
          .X2 = X
          .Y2 = Y
          
        End With
    first = True
    showline = False
    End If
    End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If showline = True Then
    With Form1.Controls(s)
          
          .X2 = X
          .Y2 = Y
          .Visible = True
    End With
    End IfEnd Sub可随意画很多条线。
      

  4.   

    Option Explicit
    Dim DrawNow As Boolean
    Dim originX As Single
    Dim originY As Single
    Dim finalX As Single
    Dim finalY As Single
    Private Sub Form_MouseDown(Button As Integer, _
          Shift As Integer, X As Single, Y As Single)
       DrawNow = True
       originX = X
       originY = Y
    End SubPrivate Sub Form_MouseUp(Button As Integer, _
          Shift As Integer, X As Single, Y As Single)
        Line (originX, originY)-(X, Y)
        DrawNow = False
    End SubPrivate Sub Form_MouseMove(Button As Integer, _
          Shift As Integer, X As Single, Y As Single)
        If DrawNow Then
            Line (originX, originY)-(finalX, finalY), Form1.BackColor
            Line (originX, originY)-(X, Y)
        End If
         finalX = X
        finalY = Y
    End Sub'容器如果不是Form,容器相应事件里添加代码
      

  5.   

    Private Type ZB
        X As Long
        Y As Long
    End Type
    Private PrevPoint As ZBPrivate Sub Form_Load()
        Picture1.AutoRedraw = True
        PrevPoint.X = -1
    End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If PrevPoint.X = -1 Then Exit Sub
        Picture1.Cls
        Picture1.Line (PrevPoint.X, PrevPoint.Y)-(X, Y)
    End SubPrivate Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If PrevPoint.X < 0 Then
            PrevPoint.X = X
            PrevPoint.Y = Y
        Else
            Picture1.Line (PrevPoint.X, PrevPoint.Y)-(X, Y)
            Picture1.Picture = Picture1.Image
            PrevPoint.X = -1
        End If
    End Sub