Option Explicit Private IsDragging As Boolean '現在ドラッグ中か Private curX As Integer '現在の図形の左上のX座標 Private curY As Integer '現在の図形の左上のY座標 Private diffX As Integer 'ドラッグ開始時点のcurXとマウスX座標の差 Private diffY As Integer 'ドラッグ開始時点のcurYとマウスY座標の差 Private Sub Form_Load() Picture1.BackColor = vbWhite Picture1.ScaleMode = vbPixels Picture1.AutoRedraw = True curX = 10 curY = 10 Call Draw(10, 10) End Sub Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 'ドラッグ開始時点の図形とマウス位置の差を保存 diffX = curX - CInt(X) diffY = curY - CInt(Y) '無条件でドラッグ開始としているが、 '本来は対象図形上にマウスがあればドラッグ開始といった条件が必要 IsDragging = True Me.Caption = "ドラッグ中 - " & Me.Caption End Sub Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If IsDragging Then Call Draw(CInt(X) + diffX, CInt(Y) + diffY) End If End Sub Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) IsDragging = False Me.Caption = Replace(Me.Caption, "ドラッグ中 - ", "") curX = CInt(X) + diffX curY = CInt(Y) + diffY End Sub Private Sub Draw(posX As Integer, posY As Integer) Picture1.Cls Picture1.Line (posX, posY)-(posX + 50, posY + 50), vbRed, BF End Sub