我想问一下当鼠标光标指向特定对象时是否可以在picbox上拖动图像,所以当我选择X文本框,y文本框和选择图钉文本时 它将取代我选择的坐标n引脚号码。
但我想把它们拖到图片框上。当鼠标光标触摸某个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
答案 0 :(得分: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重叠实施留在你身上:)。