Skip to content

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 handle
  • Cancel - 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, True

SwitchToThis

Switch to specified window

vb
Public Sub SwitchToThis(ByVal hWnd As Long, Optional IsAltTab As Boolean = True)

Parameters:

  • hWnd - Window handle
  • IsAltTab - 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 If

ActiveForm

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_LostFocus event 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 hWnd

Comprehensive 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 Sub

Example 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 Sub

Example 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 Sub

Best Practices

  1. Use Force Activation with Caution: ActiveForm will forcibly take focus, may affect user experience
  2. Check Handle: Check if window handle is valid before operation
  3. Topmost Usage: Topmost windows should provide obvious way to cancel topmost
  4. Resource Release: Cancel topmost before program exit to avoid residual state

VB6 and LOGO copyright of Microsoft Corporation