How To Keep A Window Always On Top Of All Applications

In order to setup the camera, it is necessary to have the following:

  1. Not allow any other windows to be running on top of the application. (On Top is optional)
  2. Not allow the application to be re-sized.
  3. Not allow the application to be moved.
  4. Not have Minimize/Maximize buttons. (Minimize button is optional)
  5. Have a visible menu bar available.

Seems like Windows (VB?) allows you to either have a menu with a title bar that can be moved, or no menu with a title bar that does not move. I could not get a maximized window with a menu bar, that was not moveable. Selecting various combinations of the Maximize and Minimize buttons did not help. Usually, double-clicking on the title bar restored the window to its original size and you could then drag it around with the mouse.

The code below allows you to create a Window which is OnTop, or normal, depending on what your application is. It does not respond when the title bar is double clicked for maximize/restore. A visible menu bar is also available.

Form Properties necessary for this implementation:

—————————————————————————

MaxButton False Disabled in the code

Minbutton True/False Depending on the needs of the application

ControlBox True Needed for maximize/restore to work

WindowState Maximized To maximize the form on loading

BorderStyle 0-None Disables Alt+F4 for closing application

BorderStyle 1-Fixed Single Worked best for this application

—————————————————————————

BorderStyle of 2-Sizable is not a good choice, because the frame of the window shows up when you double-click on the title bar.

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)
Declare Function GetSystemMenu Lib "User" (ByVal hWnd As Integer, ByVal bRevert As Integer) As Integer
Declare Function DeleteMenu Lib "User" (ByVal hMenu As Integer, ByVal iditem As Integer, ByVal wflags As Integer) As Integer
Const SC_SIZE = &HF000
Const SC_MOVE = &HF010
Const MF_BYCOMMAND = &H0
Sub Form_Load ()
Dim hMenu, iSuccess As Integer
'-------------------------------------------------------
' This will not allow the window to be moved or re-sized
'-------------------------------------------------------
hMenu = GetSystemMenu(Me.hWnd, 0)
iSuccess = DeleteMenu(hMenu, SC_SIZE, MF_BYCOMMAND)
iSuccess = DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND)
'-----------------------------------------------------------------------
' This will set the Window to the size of the screen no matter what it 
' is. If the user tries to double click the window, it will appear to 
' not do anything, though it is in restored mode.
'-----------------------------------------------------------------------
Me.Height = Screen.Height + 45
Me.Width = Screen.Width + 60
Me.Left = -15
Me.Top = -15
'--------------------------------------------
' This will make the window always be visible
'--------------------------------------------
SetWindowPos Me.hWnd, -1, 0, 0, 0, 0, &H50
'----------------------------------------------------------------
' This will remove the always visible attribute, use where needed
'----------------------------------------------------------------
'SetWindowPos Me.hWnd, -2, 0, 0, 0, 0, &H50
End Sub

 

Tip Submitted By: Sunny Jamshedji


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.