如何在 在窗体间拖动控件

gougou1 2003-05-12 09:07:45
可将Command1按钮在窗体内拖动,也可将其拖动到另一个窗体中。
...全文
49 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
叶帆 2003-05-13
’这种拖动效果好
Private Const HTCAPTION = 2

Private Const WM_NCLBUTTONDOWN = &HA1
Private Const WM_NCLBUTTONUP = &HA2

Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Dim G_Index As Integer

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
X = ReleaseCapture()
Call SendMessage(Picture1.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)

End If
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.MousePointer = 5
End Sub
回复
LoveBH 2003-05-12
设计方法:
把要在窗体间拖动的控件分别放置在两个窗体上,且使它们的外观完全相同。当在窗体间拖动该控件时,则将源窗体中的控件置为不可见,将目标窗体中的控件置为可见,这样就好像控件真的被拖动了一样。

代码如下

窗体1

Option Explicit
Dim dragx As Single
Dim dragy As Single
Const BEGIN_DRAG = 1

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
'如果所拖动的控件名称为"Command1"
If Source.Name = "Command1" Then
'将控件移动到指针所在位置
Command1.Move (X - dragx), (Y - dragy)
'控制
Command1.Visible = True
form2.Command1.Visible = False
End If
End Sub

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'记录指针在控件上的位置
dragx = X
dragy = Y
'开始拖动
Command1.Drag BEGIN_DRAG
End Sub

'在窗体加载是同时显示窗体form2
Private Sub Form_Load()
form2.Show
form2.Command1.Visible = False
End Sub

窗体2

Option Explicit
Dim dragx As Single
Dim dragy As Single
Const BEGIN_DRAG = 1

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
'如果所拖动的控件名称为"Command1"
If Source.Name = "Command1" Then
'将控件移动到指针所在位置
Command1.Move (X - dragx), (Y - dragy)
'控制
Command1.Visible = True
form1.Command1.Visible = False
End If
End Sub

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'记录指针在控件上的位置
dragx = X
dragy = Y
'开始拖动
Command1.Drag BEGIN_DRAG
End Sub


欢迎光临电脑爱好者论坛 bbs.cfanclub.net

回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7492

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2003-05-12 09:07
社区公告
暂无公告