RSS
StartseiteKnowledge LibraryTop 10Impressum

4.3 Wie halte ich ein Formular immer im Vordergrund?

VB4-VB6

Von:  [Home]

Um ein Formular immer im Vordergrund zu halten, d.h. vor allen anderen Anwendungen, wird die API Funktion SetWindowPos benötigt. Der erste Parameter hWndInsertAfter bestimmt dabei den Status des Form, immer im Vordergrund - HWND_TOPMOST oder gleichberechtigt neben allen anderen Fenstern - HWND_NOTOPMOST. Die anderen Parameter, wie die Größe und Position des Form, lassen wir unberührt, was wir der Funktion über die Flags SWP_NOMOVE und SWP_NOSIZE mitteilen.

Im Endeffekt wird also eine einfache Prozedur benötigt, die zwei Parameter erwartet: Das betreffende Form und sein Status:

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_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1

Public Property Let AlwaysOnTop(ByRef Form As Form, ByVal New_Value As Boolean)
  Dim lngInsertAfter As Long
  
  If (New_Value) Then
    lngInsertAfter = HWND_TOPMOST
  Else
    lngInsertAfter = HWND_NOTOPMOST
  End If
  
  SetWindowPos Form.hWnd, lngInsertAfter, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Property

Aufruf, um ein Form immer im Vordergrund zu halten:

AlwaysOnTop(Me) = True

Aufruf, um es wieder gleichberechtigt neben alle anderen Fenster zu setzen:

AlwaysOnTop(Me) = False