【发布时间】:2019-03-15 20:07:49
【问题描述】:
我一直在寻找一些允许用户“单击并拖动”以在无边框表单中移动的代码。我已经在 VB.Net 和 Windows 窗体中的 C# 中实现了这一点,并且我相信,历史上是在 Excel 中完成的(尽管我不记得代码)。我似乎无法翻译成 Access VBA,主要是因为“left”方法不能应用于 Private Sub 中的 Form 对象(我认为?):
Me.Left
没有这个,我很难翻译代码,那么还有其他方法,也许是通过 Windows API 调用或只是表单事件来实现这一点?我真的很想用尽所有可能性,因为无边界表单看起来很不错!
非常感谢任何帮助。
这是有效的 VB.Net 版本:
Dim dragForm As Boolean
Dim xDrag As Integer
Dim yDrag As Integer
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
dragForm = True
xDrag = Windows.Forms.Cursor.Position.X - Me.Left
yDrag = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If dragForm Then
Me.Top = Windows.Forms.Cursor.Position.Y - yDrag
Me.Left = Windows.Forms.Cursor.Position.X - xDrag
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
dragForm = False
End Sub
到目前为止,这是我重新编写的尝试:
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim xx As Long
Dim yy As Long
xx = Me.Left + X - xDrag
yy = Me.Top + Y - yDrag
Me.Left = xx
Me.Top = yy
moveFrm = False
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim xx As Long
Dim yy As Long
If moveFrm = True Then
xx = Me.Left + X - xDrag
yy = Me.Top + Y - yDrag
Me.Left = xx
Me.Top = yy
End If
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
moveFrm = True
xDrag = X
yDrag = Y
End Sub
【问题讨论】:
-
您的问题似乎有点宽泛。如果问题只是:Access 中的
Form.Top和Form.Left是什么,我可以回答这个问题。但我不会使用提供的代码创建一个 VB.Net 应用程序,创建一个 Access 应用程序,并通过一些调整来测试它们的行为是否相同。 -
谢谢@ErikvonAsmuth - 我只在问题中包含了代码以显示我到目前为止所做的尝试。从本质上讲,对于 Access VBA,我只是在寻找一种能够拖动和移动无边框表单的方法。
-
使用windows release capture and send measage API。另请查看this
标签: ms-access vba ms-access-2010