Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const SWP_NOMOVE = &H2, SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1, HWND_NOTOPMOST = -2
Sub SetTopmostWindow(ByVal hwnd As Long, Optional topmost As Boolean = True)
Const HWND_NOTOPMOST = -2
Const HWND_TOPMOST = -1
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
SetWindowPos hwnd, IIf(topmost, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, _
SWP_NOMOVE + SWP_NOSIZE
End Sub
Private Sub Check1_Click()
If Check1.Value = vbChecked Then
SetTopmostWindow Me.hwnd, True
Else
SetTopmostWindow Me.hwnd, False
End If
End Sub
Private Const SWP_NOMOVE = &H2, SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1, HWND_NOTOPMOST = -2
Sub SetTopmostWindow(ByVal hwnd As Long, Optional topmost As Boolean = True)
Const HWND_NOTOPMOST = -2
Const HWND_TOPMOST = -1
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
SetWindowPos hwnd, IIf(topmost, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, _
SWP_NOMOVE + SWP_NOSIZE
End Sub
Private Sub Check1_Click()
If Check1.Value = vbChecked Then
SetTopmostWindow Me.hwnd, True
Else
SetTopmostWindow Me.hwnd, False
End If
End Sub
本文介绍了一种使用Visual Basic编程语言实现窗口置顶功能的方法。通过调用Windows API函数SetWindowPos来改变窗口的位置和属性,使窗口始终显示在其他窗口之上或取消此状态。示例代码展示了如何创建一个简单的开关按钮,用于切换窗口的置顶状态。
652

被折叠的 条评论
为什么被折叠?



