0

我想问是否可以在 picbox 上拖动图像,当鼠标光标指向某个对象时,所以当我选择 X-textbox、y-textbox 并选择 pin 文本时,它将根据我的坐标 n pin 编号显示我选择了。

但我想将它们拖到图片框上。当鼠标光标碰到该IC图像时,可以拖动。但并非所有 picbox 上的图像都拖在一起。

谁能给我一些建议?

我的完整代码如下:

Option Explicit
Private mPic        As Picture
Private mPicWidth   As Single
Private mPicHeight  As Single
Private mCurrentX   As Single
Private mCurrentY   As Single
Private mLeft       As Single
Private mTop        As Single

Private Sub Command5_Click()
    Call draw_ic(Val(Text5), Val(Text6), Val(Text7))
End Sub

Private Sub Form_Load()
    Picture1.AutoRedraw = True
    Set mPic = Picture1.Image
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As    Single)
    mPicWidth = Me.ScaleX(mPic.Width, vbHimetric, Picture1.ScaleMode)
    mPicHeight = Me.ScaleY(mPic.Height, vbHimetric, Picture1.ScaleMode)
    ShowPictureAtPosition mLeft, mTop
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 0 Then
        mCurrentX = X
        mCurrentY = Y
    ElseIf Button = vbLeftButton Then
        ShowPictureAtPosition X + mLeft - mCurrentX, Y + mTop - mCurrentY
    End If
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    mLeft = X + mLeft - mCurrentX: mTop = Y + mTop - mCurrentY
End Sub

Private Sub ShowPictureAtPosition(pX As Single, pY As Single)
    With Picture1    
        .Cls
       .PaintPicture mPic, pX + 1, pY + 1, mPicWidth, mPicHeight    
    End With
End Sub

Function draw_ic(X, Y, pincount)
    If pincount = 8 Then
        Picture1.Line (X, Y)-(X + 120, Y + 48), vbBlack, B
        Picture1.Line (X + 8, Y)-(X + 24, Y - 16), vbBlack, B
        Picture1.Line (X + 34, Y)-(X + 50, Y - 16), vbBlack, B
        Picture1.Line (X + 60, Y)-(X + 76, Y - 16), vbBlack, B
        Picture1.Line (X + 86, Y)-(X + 102, Y - 16), vbBlack, B
        Picture1.Line (X + 8, Y + 48)-(X + 24, Y + 64), vbBlack, B
        Picture1.Line (X + 34, Y + 48)-(X + 50, Y + 64), vbBlack, B
        Picture1.Line (X + 60, Y + 48)-(X + 76, Y + 64), vbBlack, B
        Picture1.Line (X + 86, Y + 48)-(X + 102, Y + 64), vbBlack, B
    ElseIf pincount = 12 Then
        Picture1.Line (X, Y)-(X + 158, Y + 64), vbBlack, B
        Picture1.Line (X + 8, Y)-(X + 16, Y - 8), vbBlack, B
        Picture1.Line (X + 32, Y)-(X + 40, Y - 8), vbBlack, B
        Picture1.Line (X + 56, Y)-(X + 64, Y - 8), vbBlack, B
        Picture1.Line (X + 80, Y)-(X + 88, Y - 8), vbBlack, B
        Picture1.Line (X + 104, Y)-(X + 112, Y - 8), vbBlack, B
        Picture1.Line (X + 128, Y)-(X + 136, Y - 8), vbBlack, B
        Picture1.Line (X + 8, Y + 64)-(X + 16, Y + 72), vbBlack, B
        Picture1.Line (X + 32, Y + 64)-(X + 40, Y + 72), vbBlack, B
        Picture1.Line (X + 56, Y + 64)-(X + 64, Y + 72), vbBlack, B
        Picture1.Line (X + 80, Y + 64)-(X + 88, Y + 72), vbBlack, B
        Picture1.Line (X + 104, Y + 64)-(X + 112, Y + 72), vbBlack, B
        Picture1.Line (X + 128, Y + 64)-(X + 136, Y + 72), vbBlack, B
    ElseIf pincount = 16 Then
        Picture1.Line (X, Y)-(X + 222, Y + 72), vbBlack, B
        Picture1.Line (X + 8, Y)-(X + 24, Y - 16), vbBlack, B
        Picture1.Line (X + 34, Y)-(X + 50, Y - 16), vbBlack, B
        Picture1.Line (X + 60, Y)-(X + 76, Y - 16), vbBlack, B
        Picture1.Line (X + 86, Y)-(X + 102, Y - 16), vbBlack, B
        Picture1.Line (X + 112, Y)-(X + 128, Y - 16), vbBlack, B
        Picture1.Line (X + 138, Y)-(X + 154, Y - 16), vbBlack, B
        Picture1.Line (X + 164, Y)-(X + 180, Y - 16), vbBlack, B
        Picture1.Line (X + 190, Y)-(X + 206, Y - 16), vbBlack, B
        Picture1.Line (X + 8, Y + 72)-(X + 24, Y + 88), vbBlack, B
        Picture1.Line (X + 34, Y + 72)-(X + 50, Y + 88), vbBlack, B
        Picture1.Line (X + 60, Y + 72)-(X + 76, Y + 88), vbBlack, B
        Picture1.Line (X + 86, Y + 72)-(X + 102, Y + 88), vbBlack, B
        Picture1.Line (X + 112, Y + 72)-(X + 128, Y + 88), vbBlack, B
        Picture1.Line (X + 138, Y + 72)-(X + 154, Y + 88), vbBlack, B
        Picture1.Line (X + 164, Y + 72)-(X + 180, Y + 88), vbBlack, B
        Picture1.Line (X + 190, Y + 72)-(X + 206, Y + 88), vbBlack, B
    End If
End Function
4

1 回答 1

0

这是一个非常有趣的项目!

考虑下图的坐标。

在此处输入图像描述

每次创建新 IC 时,如果保存其左上角和右下角坐标,将帮助您确定鼠标指针何时到达其区域,使用以下代码。

Private Function MouseCursorInsideRectangle(topLeftX As Integer, topLeftY As Integer, bottomRightX As Integer, bottomRightY As Integer, mouseX As Integer, mouseY As Integer) As Boolean
    If mouseX >= topLeftX And mouseX <= bottomRightX And mouseY >= topLeftY And mouseY <= bottomRightY Then
        MouseCursorInsideRectangle = True
    Else
        MouseCursorInsideRectangle = False
    End If
End Function

这将解决重叠 IC 的问题,因为您可以检查要绘制的 IC 的坐标并禁止绘制,如果它超过另一个。

为了解决只移动某个 IC 而不是整个 PictureBox 图像的问题,您可以在拖动事件正在进行时清除图像,并通过迭代重新绘制所有 IC(使用您在创建过程中保存的坐标)。

(虽然不在你的要求范围内,但也可以考虑保存每个IC的管脚坐标,这样以后可以在IC之间画线)。

编辑

试试下面的代码

Option Explicit
Option Base 0

Private Type ICData
    topLeftX As Integer
    topLeftY As Integer
    bottomRightX As Integer
    bottomRightY As Integer
    pinCount As Integer
End Type

Dim ICs() As ICData
Dim ICsIndex As Integer
Dim DraggedICIndex As Integer
Dim Xdifference As Integer
Dim Ydifference As Integer

Dim InitialX As Integer
Dim InitialY As Integer

Private Sub Form_Load()
    ICsIndex = -1
    DraggedICIndex = -1
    Picture1.ScaleMode = 3
    Picture1.AutoRedraw = True
End Sub

Private Sub Command5_Click()        
    Call save_ic(Val(text5), Val(text6), Val(text7))
    Call draw_ics(-1)
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    DraggedICIndex = GetICIndex(CSng(X), CSng(Y))

    If DraggedICIndex > -1 Then
        InitialX = ICs(DraggedICIndex).topLeftX
        InitialY = ICs(DraggedICIndex).topLeftY

        Xdifference = Abs(X - ICs(DraggedICIndex).topLeftX)
        Ydifference = Abs(Y - ICs(DraggedICIndex).topLeftY)
    End If
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton And DraggedICIndex > -1 Then
        Picture1.Cls

        Call draw_ics(DraggedICIndex)
        Call draw_ic(X - Xdifference, Y - Ydifference, ICs(DraggedICIndex).pinCount)

        Dim ICWidth As Integer
        Dim ICHeight As Integer

        ICWidth = ICs(DraggedICIndex).bottomRightX - ICs(DraggedICIndex).topLeftX
        ICHeight = ICs(DraggedICIndex).bottomRightY - ICs(DraggedICIndex).topLeftY
    End If
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If DraggedICIndex > -1 Then
        Dim ICWidth As Integer
        Dim ICHeight As Integer

        ICWidth = ICs(DraggedICIndex).bottomRightX - ICs(DraggedICIndex).topLeftX
        ICHeight = ICs(DraggedICIndex).bottomRightY - ICs(DraggedICIndex).topLeftY

        ICs(DraggedICIndex).topLeftX = X - Xdifference
        ICs(DraggedICIndex).topLeftY = Y - Ydifference
        ICs(DraggedICIndex).bottomRightX = ICWidth + X - Xdifference
        ICs(DraggedICIndex).bottomRightY = ICHeight + Y - Ydifference
    End If
End Sub

Private Function MouseCursorInsideRectangle(location As ICData, mouseX As Integer, mouseY As Integer) As Boolean
    If mouseX >= location.topLeftX And mouseX <= location.bottomRightX And mouseY >= location.topLeftY And mouseY <= location.bottomRightY Then
        MouseCursorInsideRectangle = True
    Else
        MouseCursorInsideRectangle = False
    End If
End Function

Private Function GetICIndex(mouseX As Integer, mouseY As Integer) As Integer
    Dim i As Integer

    For i = 0 To ICsIndex
        If MouseCursorInsideRectangle(ICs(i), mouseX, mouseY) = True Then
            GetICIndex = i
            Exit Function
        End If
    Next

    GetICIndex = -1
End Function

Sub save_ic(X, Y, pinCount)
    If pinCount = 8 Then
        ICsIndex = ICsIndex + 1
        ReDim Preserve ICs(ICsIndex)

        ICs(ICsIndex).topLeftX = X
        ICs(ICsIndex).topLeftY = Y
        ICs(ICsIndex).bottomRightX = X + 120
        ICs(ICsIndex).bottomRightY = Y + 80
        ICs(ICsIndex).pinCount = pinCount
    ElseIf pinCount = 12 Then
        ICsIndex = ICsIndex + 1
        ReDim Preserve ICs(ICsIndex)

        ICs(ICsIndex).topLeftX = X
        ICs(ICsIndex).topLeftY = Y
        ICs(ICsIndex).bottomRightX = X + 158
        ICs(ICsIndex).bottomRightY = Y + 80
        ICs(ICsIndex).pinCount = pinCount
    ElseIf pinCount = 16 Then
        ICsIndex = ICsIndex + 1
        ReDim Preserve ICs(ICsIndex)

        ICs(ICsIndex).topLeftX = X
        ICs(ICsIndex).topLeftY = Y
        ICs(ICsIndex).bottomRightX = X + 222
        ICs(ICsIndex).bottomRightY = Y + 104
        ICs(ICsIndex).pinCount = pinCount
    End If
End Sub

Sub draw_ic(X, Y, pinCount)
    If pinCount = 8 Then
        Picture1.Line (X, Y + 16)-(X + 120, Y + 64), vbBlack, B
        Picture1.Line (X + 8, Y + 16)-(X + 24, Y), vbBlack, B
        Picture1.Line (X + 34, Y + 16)-(X + 50, Y), vbBlack, B
        Picture1.Line (X + 60, Y + 16)-(X + 76, Y), vbBlack, B
        Picture1.Line (X + 86, Y + 16)-(X + 102, Y), vbBlack, B
        Picture1.Line (X + 8, Y + 64)-(X + 24, Y + 80), vbBlack, B
        Picture1.Line (X + 34, Y + 64)-(X + 50, Y + 80), vbBlack, B
        Picture1.Line (X + 60, Y + 64)-(X + 76, Y + 80), vbBlack, B
        Picture1.Line (X + 86, Y + 64)-(X + 102, Y + 80), vbBlack, B
    ElseIf pinCount = 12 Then
        Picture1.Line (X, Y + 8)-(X + 158, Y + 72), vbBlack, B
        Picture1.Line (X + 8, Y + 8)-(X + 16, Y), vbBlack, B
        Picture1.Line (X + 32, Y + 8)-(X + 40, Y), vbBlack, B
        Picture1.Line (X + 56, Y + 8)-(X + 64, Y), vbBlack, B
        Picture1.Line (X + 80, Y + 8)-(X + 88, Y), vbBlack, B
        Picture1.Line (X + 104, Y + 8)-(X + 112, Y), vbBlack, B
        Picture1.Line (X + 128, Y + 8)-(X + 136, Y), vbBlack, B
        Picture1.Line (X + 8, Y + 72)-(X + 16, Y + 80), vbBlack, B
        Picture1.Line (X + 32, Y + 72)-(X + 40, Y + 80), vbBlack, B
        Picture1.Line (X + 56, Y + 72)-(X + 64, Y + 80), vbBlack, B
        Picture1.Line (X + 80, Y + 72)-(X + 88, Y + 80), vbBlack, B
        Picture1.Line (X + 104, Y + 72)-(X + 112, Y + 80), vbBlack, B
        Picture1.Line (X + 128, Y + 72)-(X + 136, Y + 80), vbBlack, B
    ElseIf pinCount = 16 Then
        Picture1.Line (X, Y + 16)-(X + 222, Y + 88), vbBlack, B
        Picture1.Line (X + 8, Y + 16)-(X + 24, Y), vbBlack, B
        Picture1.Line (X + 34, Y + 16)-(X + 50, Y), vbBlack, B
        Picture1.Line (X + 60, Y + 16)-(X + 76, Y), vbBlack, B
        Picture1.Line (X + 86, Y + 16)-(X + 102, Y), vbBlack, B
        Picture1.Line (X + 112, Y + 16)-(X + 128, Y), vbBlack, B
        Picture1.Line (X + 138, Y + 16)-(X + 154, Y), vbBlack, B
        Picture1.Line (X + 164, Y + 16)-(X + 180, Y), vbBlack, B
        Picture1.Line (X + 190, Y + 16)-(X + 206, Y), vbBlack, B
        Picture1.Line (X + 8, Y + 88)-(X + 24, Y + 104), vbBlack, B
        Picture1.Line (X + 34, Y + 88)-(X + 50, Y + 104), vbBlack, B
        Picture1.Line (X + 60, Y + 88)-(X + 76, Y + 104), vbBlack, B
        Picture1.Line (X + 86, Y + 88)-(X + 102, Y + 104), vbBlack, B
        Picture1.Line (X + 112, Y + 88)-(X + 128, Y + 104), vbBlack, B
        Picture1.Line (X + 138, Y + 88)-(X + 154, Y + 104), vbBlack, B
        Picture1.Line (X + 164, Y + 88)-(X + 180, Y + 104), vbBlack, B
        Picture1.Line (X + 190, Y + 88)-(X + 206, Y + 104), vbBlack, B
    End If
End Sub

Sub draw_ics(exceptICIndex As Integer)
    If ICsIndex > -1 Then
        Dim i As Integer

        For i = 0 To ICsIndex
            If i <> exceptICIndex Then
                Call draw_ic(ICs(i).topLeftX, ICs(i).topLeftY, ICs(i).pinCount)
            End If
        Next
    End If
End Sub

我将 IC 重叠实施留给你 :)。

于 2013-06-27T12:23:30.393 回答