Keeping A Window On Top

You can keep your window on top by using the code listed below.

Declare

Declare Sub SetWindowPos Lib "User" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, 
ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer,
ByVal wFlags As Integer)
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2

Code

Sub KeepOnTop (frmIn As Form, bOnTop As Integer)
     Dim iTopFlag As Integer
     Const wFlags = SWP_NOMOVE Or SWP_NOSIZE
 
     If bOnTop = True Then
           iTopFlag = HWND_TOPMOST
     Else
           iTopFlag = HWND_NOTOPMOST
     End If
     SetWindowPos frmIn.hWnd, iTopFlag, 0, 0, 0, 0, wFlags
     DoEvents
End Sub

Usage

To put a Form on top of all windows, then call:

KeepOnTop Me, True

To remove the Form from being on top, call:

KeepOnTop Me, False

 

This tip is reprinted from the VB Tips & Tricks Volume 1 book.
Parts of this tip was submitted by: Henk Hakvoort
Compatible With  Visual Basic 3.0, Visual Basic 4.0 16-bit

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s