0

マウスカーソルが特定のオブジェクトを指しているときに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
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 のピンの座標を保存することも検討してください。そうすれば、将来 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 に答える