






|
VB Mania |

|
Force On Top – Always On Top |
|
Information: In this example, you will be able to make a window force on top of other windows. |
|
Const HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_NOMOVE = &H2 Const SWP_NOSIZE = &H1 Const SWP_NOACTIVATE = &H10 Const SWP_SHOWWINDOW = &H40 Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
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 Function MakeTopMost(hWnd As Long) SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS End Function
Private Sub Form_Load() Maketopmost (me.hWnd) End Sub |
|
Illustration:
|
|
What You Need?: One Form named Form1 |
|
Code:
|
|
Explanation: We created a function called MakeTopMost that gets that handle of a specific window; here it is Form1, to be used in SetWindowPos private function. To make a window on top of others, the second parameter of SetWindowPos (hWndInsertAfter) has to be set into -1. Otherwise, if you want the window not to be always on top you can change it into -2. As it is illustrated in Const HWND_TOPMOST = -1 and Const HWND_NOTOPMOST = -2.
Now, when form1 loaded it will call MakeTopMost function and will be on top of other windows even if it lost focus |
|
Hint: You can create a Timer with an interval of, let's say, 250 millisecond and put the MakeTopMost function inside it, so that in any case, Form1 will be ALWAYS on top. Try it. |
|
VB-Pedia: · Form: a member of VB Library which is a window or dialog box that makes up part of an application's user interface. Properties used: ¨ Property hWnd As Long It returns a handle (from Microsoft Windows) to an object's window and is read-only.
· SetWindowPos: a private function of the windows Library "user32" that change the size, position, and Z-order of child, pop-up, and top-level windows Parameters ¨ pWndInsertAfter Identifies the CWnd object that will precede this CWnd object in the Z-order. This parameter can be a pointer to a CWnd or a Pointer to one of the following values: * wndBottom Places the window at the bottom of the Z-order. If this CWnd is a topmost window, the window loses its topmost status; the system places the window at the bottom of all other windows. * wndTop Places the window at the top of the Z-order. * wndTopMost Places the window above all nontopmost windows. The window maintains its topmost position even when it is deactivated. * wndNoTopMost Repositions the window to the top of all nontopmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a nontopmost window. ¨ x Specifies the new position of the left side of the window. ¨ y Specifies the new position of the top of the window. ¨ cx Specifies the new width of the window. ¨ cy Specifies the new height of the window. ¨ nFlags Specifies sizing and positioning options. This parameter can be a combination of the following: * SWP_NOACTIVATE Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or the nontopmost group (depending on the setting of the pWndInsertAfter parameter). * SWP_NOMOVE Retains current position (ignores the x and y parameters). * SWP_NOSIZE Retains current size (ignores the cx and cy parameters). * SWP_SHOWWINDOW Displays the window. |

|
Sponsored Links |