Tools - Window Utility Class
cToolsWindow - Window Operation Tools
Overview
Provides window topmost, window switching, forced focus acquisition, and other functionality.
Methods
TopMost
Sets window to topmost/remove topmost.
vb
Public Sub TopMost(ByVal hWnd As Long, Optional Cancel As Boolean)Parameters:
| Parameter | Type | Description |
|---|---|---|
hWnd | Long | Window handle |
Cancel | Boolean | Whether to cancel topmost (default False) |
Example:
vb
' Set to topmost
VBMAN.ToolsWindow.TopMost Me.hWnd
' Remove topmost
VBMAN.ToolsWindow.TopMost Me.hWnd, TrueSwitchToThis
Switches to specified window (simulates Alt+Tab effect).
vb
Public Sub SwitchToThis(ByVal hWnd As Long, Optional IsAltTab As Boolean = True)Example:
vb
' Switch to current window
VBMAN.ToolsWindow.SwitchToThis Me.hWndActiveForm
Forcibly sets window as foreground window (bypasses system restrictions).
vb
Public Sub ActiveForm(ByVal hWnd As Long)Description:
This method bypasses Windows restrictions on SetForegroundWindow by simulating Alt key press. It is recommended to call in Form_LostFocus event to achieve the effect of window never losing focus.
Example:
vb
Private Sub Form_LostFocus()
' Force acquire focus
VBMAN.ToolsWindow.ActiveForm Me.hWnd
End SubComplete Example
vb
Private Sub Form_Load()
' Window topmost
VBMAN.ToolsWindow.TopMost Me.hWnd
' Switch to this window
VBMAN.ToolsWindow.SwitchToThis Me.hWnd
End Sub
Private Sub cmdToggleTopmost_Click()
Static IsTopmost As Boolean
IsTopmost = Not IsTopmost
VBMAN.ToolsWindow.TopMost Me.hWnd, Not IsTopmost
cmdToggleTopmost.Caption = IIf(IsTopmost, "Remove Topmost", "Set Topmost")
End SubUse Cases
| Scenario | Method to Use |
|---|---|
| Floating Window | TopMost keeps window on top |
| Popup Notification | SwitchToThis switches to notification window |
| Modal Dialog | ActiveForm ensures dialog keeps focus |
| Fullscreen Application | TopMost + ActiveForm combined |