如何在 在窗体间拖动控件

gougou1 2003-05-12 09:07:45
可将Command1按钮在窗体内拖动,也可将其拖动到另一个窗体中。
...全文
80 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶帆 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

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧