VBMAN.ToolsWindow - Window Operation Object
Overview
VBMAN.ToolsWindow provides Windows window operation functions, including window topmost, switching, activation, etc.
Methods
TopMost
Set window topmost status
vb
Public Sub TopMost(ByVal hWnd As Long, Optional Cancel As Boolean)Parameters:
hWnd- Window handleCancel- Whether to cancel topmost (default False, i.e., set to topmost)
Example:
vb
' Set current form to topmost
VBMAN.ToolsWindow.TopMost Me.hWnd
' Cancel topmost
VBMAN.ToolsWindow.TopMost Me.hWnd, TrueSwitchToThis
Switch to specified window
vb
Public Sub SwitchToThis(ByVal hWnd As Long, Optional IsAltTab As Boolean = True)Parameters:
hWnd- Window handleIsAltTab- Whether to simulate Alt+Tab switch (default True)
Example:
vb
' Switch to Notepad
Dim hWnd As Long
hWnd = FindWindow("Notepad", vbNullString)
If hWnd <> 0 Then
VBMAN.ToolsWindow.SwitchToThis hWnd
End IfActiveForm
Force activate window (simulate Alt key to bypass restrictions)
vb
Public Sub ActiveForm(ByVal hWnd As Long)Parameters:
hWnd- Window handle
Description:
- Bypasses system restrictions by simulating Alt key press
- Recommend using in
Form_LostFocusevent to achieve never-lose-focus effect
Example:
vb
' Force keep focus in Form_LostFocus event
Private Sub Form_LostFocus()
VBMAN.ToolsWindow.ActiveForm Me.hWnd
End Sub
' Activate specified window
VBMAN.ToolsWindow.ActiveForm hWndComprehensive Examples
Example 1: Topmost Window
vb
Private Sub CheckTopMost_Click()
If CheckTopMost.Value = vbChecked Then
VBMAN.ToolsWindow.TopMost Me.hWnd
CheckTopMost.Caption = "Cancel Topmost"
Else
VBMAN.ToolsWindow.TopMost Me.hWnd, True
CheckTopMost.Caption = "Window Topmost"
End If
End SubExample 2: Switch and Activate Window
vb
Private Sub BringWindowToFront()
' Find window (need to declare FindWindow API)
Dim targetHwnd As Long
targetHwnd = FindWindow(vbNullString, "Target Window Title")
If targetHwnd <> 0 Then
' Switch first
VBMAN.ToolsWindow.SwitchToThis targetHwnd
' Then activate
VBMAN.ToolsWindow.ActiveForm targetHwnd
Else
MsgBox "Target window not found"
End If
End SubExample 3: Modal Window Effect
vb
Private Sub ShowModalEffect()
' Disable main form
Me.Enabled = False
' Show child form
Form2.Show
' Ensure child form is on top
VBMAN.ToolsWindow.TopMost Form2.hWnd
End Sub
' Restore when Form2 closes
Private Sub Form2_Unload(Cancel As Integer)
Form1.Enabled = True
VBMAN.ToolsWindow.TopMost Form1.hWnd, True
End SubBest Practices
- Use Force Activation with Caution:
ActiveFormwill forcibly take focus, may affect user experience - Check Handle: Check if window handle is valid before operation
- Topmost Usage: Topmost windows should provide obvious way to cancel topmost
- Resource Release: Cancel topmost before program exit to avoid residual state