- 通过消息的发送实现移动无标题窗体。当鼠标按下、移动或释放时,将鼠标在窗体上按下的消息(消息值为HTCAPTION)发出,就可以拖动窗体了。代码如下:
- 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 Any) As Long
- Private Const WM_NCLBUTTONDOWN = &&HA1
- Private Const HTCAPTION = 2
- '以上API函数和常数的声明可在VB自带的“API浏览器”中找到
- Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
- '在窗体的MouseDown事件中添加以下代码
- If Button = 1 Then
- Call ReleaseCapture
- Call SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
- End If
- End Sub
- Private Sub Command1_Click()
- End '退出程序
- End Sub
- 这种方法实现起来比第一种方法更容易,只有几行代码,并且没有那么多的变量,窗体被拖动时与普通窗体一样,只有一个虚框随鼠标的移动而移动,当释放鼠标时窗体才移动到相应的位置。用到了两个API函数。
在VB中实现移动没有标题栏的窗口
最新推荐文章于 2022-05-25 14:43:59 发布