Setting a Macro's Window to Always be On Top

The display behavior of Visual CommBasic forms changed with Windows 2000 and XP. On macro startup, the macro form paints underneath, with a lower z-order than the OutsideView window. It is necessary to click on the macro's taskbar icon to bring it to focus.

You can add a few lines of code to your Visual CommBasic macro so that its form is always on top regardless of the OutsideView Window. For this technique to work correctly, it is also necessary to define the caption property of the macro's form.

Add these lines to the form_common event:

Dim hWnd as Long

'Declarations of Windows API calls
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As Long, ByVal lpWindowName As String) As Long

Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _
(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

 

Add these lines to the form_load event:

hWnd = FindWindow(0, me.caption) 'Get the handle for the macro form
SetWindowPos hWnd, -1, 0, 0, 0, 0, &h43 'Set the z-order for topmost