Option Explicit
Private WithEvents LabDrag As Label
Dim MousX%, MousY%, X1&, Y1&Private Sub Form_Load()
Set LabDrag = Controls.Add("vb.label", "LabDrag")
End SubPrivate Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
X1 = X - MousX: Y1 = Y - MousY
Source.Left = IIf(X1 > 2520 And X1 < 4560, X1, 2521)
Source.Top = IIf(Y1 > 1320 And Y1 < 3840, Y1, 1321)
End SubPrivate Sub label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
MousX = X: MousY = Y
LabDrag.Move Label1.Left, Label1.Top + Y - Label1.Height / 2, Label1.Width, Label1.Height
LabDrag.Drag
End If
End SubPrivate Sub Form_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
If State = 0 Then Source.MousePointer = 5
If State = 1 Then Source.MousePointer = 0
End SubPrivate Sub Label3_DragDrop(Source As Control, X As Single, Y As Single)
Label3.Caption = Source.Caption
End Sub
上次提问的标签拖动并赋值的问题,实际运行时,标签的移动位置不准确!
Private WithEvents LabDrag As Label
Dim MousX%, MousY%, X1&, Y1&Private Sub Form_Load()
Set LabDrag = Controls.Add("vb.label", "LabDrag")
End SubPrivate Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
X1 = X - MousX: Y1 = Y - MousY
Source.Left = IIf(X1 > 2520 And X1 < 4560, X1, 2521)
Source.Top = IIf(Y1 > 1320 And Y1 < 3840, Y1, 1321)
End SubPrivate Sub label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
MousX = X: MousY = Y
LabDrag.Move Label1.Left, Label1.Top + Y - Label1.Height / 2, Label1.Width, Label1.Height
LabDrag.Drag
End If
End SubPrivate Sub Form_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
If State = 0 Then Source.MousePointer = 5
If State = 1 Then Source.MousePointer = 0
End SubPrivate Sub Label3_DragDrop(Source As Control, X As Single, Y As Single)
Label3.Caption = Source.Caption
End Sub
上次提问的标签拖动并赋值的问题,实际运行时,标签的移动位置不准确!
Option ExplicitPrivate MousX&, MousY&, X1&, Y1&Private Sub DragStop(Source As Control, X As Single, Y As Single) X1 = X - MousX: Y1 = Y - MousY
Do
If (X1 < 2520) Then
X1 = 2520
ElseIf (X1 > 4560 - Source.Width) Then
X1 = 4560 - Source.Width
End If
If (Y1 < 1320) Then
Y1 = 1320
ElseIf (Y1 > 3840 - Source.Height) Then
Y1 = 3840 - Source.Height
End If
Exit Do
Loop
Source.Move X1, Y1
Source.MousePointer = 0End SubPrivate Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
DragStop Source, X, Y
End SubPrivate Sub Label1_DragDrop(Source As Control, X As Single, Y As Single)
DragStop Source, Label1.Left + X, Label1.Top + Y
End SubPrivate Sub Label2_DragDrop(Source As Control, X As Single, Y As Single)
DragStop Source, Label2.Left + X, Label2.Top + Y
End SubPrivate Sub Label3_DragDrop(Source As Control, X As Single, Y As Single)
Label3.Caption = Source.Caption
End SubPrivate Sub label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then MousX = X: MousY = Y: Label1.DragEnd SubPrivate Sub Form_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
If State = 0 Then Source.MousePointer = 5
If State = 1 Then Source.MousePointer = 0
End Sub
如果拖出指定区域,则限制 Label1 挨边停靠在指定区域内。
我真不明白楼主说的“拖动位置不准”是怎么回事。建议楼主新建一个标准EXE工程,在窗口中添加 3个标签控件。
并把标签控件填充以不同于窗体的颜色,增加下面的代码,运行看效果!
Private Sub Form_Load() AutoRedraw = True
Me.Line (2520, 1320)-(4560, 3840), vbRed, BEnd Sub还有:你的 Label1 不能比指定的矩形区域宽吧!!!