# Re: 请问如何才能使Form永远保持在最上层呢。

Posted by Someone on December 23, 1998 at 14:20:25:

In Reply to: 请问如何才能使Form永远保持在最上层呢。 posted by poga on December 23, 1998 at 13:18:45:

这个问题已经是老问题了！

Option Explicit
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 HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2

Public Sub SetOnTop(frm As Form, Topmost As Boolean)
If Topmost Then
SetWindowPos frm.hwnd, HWND_TOPMOST, 0, 0, 0, _
0, SWP_NOSIZE Or SWP_NOMOVE
Else
SetWindowPos frm.hwnd, HWND_NOTOPMOST, 0, 0, 0, _
0, SWP_NOSIZE Or SWP_NOMOVE
End If
End Sub
以上的程式码放在一个 Module 中

在 Form_Load 中输入：
SetOnTop Me, True

若非MDIForm直接在Show后面加 1 即可
Form.Show 1

Follow Ups: