Const HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_NOSIZE = &H1 Const SWP_NOMOVE = &H2 Const SWP_NOACTIVATE = &H10 Const SWP_SHOWWINDOW = &H40 Private Declare Sub 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)Private Sub Form_Activate() 'Set the window position to topmost SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE End Sub
窗体总在最前,我的方法是: Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, Y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long ' Various constants used by the above functions Public Const HWND_TOPMOST = -1 Public Const HWND_NOTOPMOST = -2 Public Const SWP_NOMOVE = &H2 Public Const SWP_NOSIZE = &H1 Public Const SWP_NOACTIVATE = &H10 Public Const SWP_SHOWWINDOW = &H40 Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZEPublic Function PutWindowOnTop(pFrm As Form) Dim lngWindowPosition As Long lngWindowPosition = SetWindowPos(pFrm.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE) End Function
Private Const SWP_NOSIZE = &H1 Private Const SWP_NOMOVE = &H2 Private Const SWP_NOZORDER = &H4 Private Const SWP_NOREDRAW = &H8 Private Const SWP_NOACTIVATE = &H10 Private Const SWP_FRAMECHANGED = &H20 Private Const SWP_SHOWWINDOW = &H40 Private Const SWP_NOCOPYBITS = &H80 Private Const SWP_NOOWNERZORDER = &H200 Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED Private Const SWP_NOREPOSITION = SWP_NOOWNERZORDER Private Const HWND_TOP = 0 Private Const HWND_BOTTOM = 1 Private Const HWND_TOPMOST = -1 Private Const HWND_NOTOPMOST = -2 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 mbOnTop As Boolean Private Property Let OnTop(Setting As Boolean) if Setting Then SetWindowPos hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Else SetWindowPos hwnd, -2, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE End If mbOnTop = Setting End Property Private Property Get OnTop() As Boolean 'Return the private variable set in Property Let OnTop = mbOnTop End Property 调用 OnTop=True 即可让窗口 Always OnTop。
用不着那么复杂,用个TOPMOST顶天了,其他的没用。
'声明API函数 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'定义函数 Public Sub SetFormOnTop(hwnd As Long, isFormOnTp As Boolean) '设置窗体层的位置,hwnd-窗口的hwnd属性,isFormOnTp-True 表示置顶,false表示正常显示 If isFormOnTp Then SetWindowPos hwnd, -1, 0, 0, 0, 0, 3 ' 设置窗体总在最前面 Else SetWindowPos hwnd, -2, 0, 0, 0, 0, 3 ' 正常显示 End If End Sub'使窗体Form1置顶 Call SetFormOnTop(Form1.hwnd,True) '使窗体Form1正常显示 Call SetFormOnTop(Form1.hwnd,False)
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub 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)Private Sub Form_Activate() 'Set the window position to topmost
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, Y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
' Various constants used by the above functions
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOACTIVATE = &H10
Public Const SWP_SHOWWINDOW = &H40
Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZEPublic Function PutWindowOnTop(pFrm As Form)
Dim lngWindowPosition As Long
lngWindowPosition = SetWindowPos(pFrm.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Function
你可以在timer中 总执行上面的程序
比如 qq和一些游戏
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOREDRAW = &H8
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOCOPYBITS = &H80
Private Const SWP_NOOWNERZORDER = &H200
Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED
Private Const SWP_NOREPOSITION = SWP_NOOWNERZORDER
Private Const HWND_TOP = 0
Private Const HWND_BOTTOM = 1
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
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 mbOnTop As Boolean
Private Property Let OnTop(Setting As Boolean)
if Setting Then
SetWindowPos hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
Else
SetWindowPos hwnd, -2, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If
mbOnTop = Setting
End Property
Private Property Get OnTop() As Boolean
'Return the private variable set in Property Let
OnTop = mbOnTop
End Property 调用 OnTop=True 即可让窗口 Always OnTop。
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'定义函数
Public Sub SetFormOnTop(hwnd As Long, isFormOnTp As Boolean)
'设置窗体层的位置,hwnd-窗口的hwnd属性,isFormOnTp-True 表示置顶,false表示正常显示
If isFormOnTp Then
SetWindowPos hwnd, -1, 0, 0, 0, 0, 3 ' 设置窗体总在最前面
Else
SetWindowPos hwnd, -2, 0, 0, 0, 0, 3 ' 正常显示
End If
End Sub'使窗体Form1置顶
Call SetFormOnTop(Form1.hwnd,True)
'使窗体Form1正常显示
Call SetFormOnTop(Form1.hwnd,False)