首页 /编程语言和算法/VB6/VBA/ASP
 VB6 利用API FindWindow,通过窗体部分标题,激活窗体,子程序 f_ActivateForm(ByVal strWindowTitle$)
昨天 11:17

新建From1(窗体),新建Command1(按钮CommandButton),代码:

Private Sub Command1_Click()
    Call f_ActivateForm("Microsoft Visual Basic")
End Sub

新建模块 modActivateForm.bas,代码:

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Private m_lngTargetHwnd&, m_strWindowTitle$

'------------------------------------------------------------------------------
' 函数: f_ActivateForm
' 功能: 激活标题包含"Microsoft Visual Basic"的窗体
' 参数: 无
' 返回: Boolean - 是否成功激活窗体
' 举例: Call f_ActivateForm("Microsoft Visual Basic")
'------------------------------------------------------------------------------
Public Function f_ActivateForm(ByVal strWindowTitle$) As Boolean
    m_strWindowTitle = strWindowTitle
    Dim blnResult As Boolean
        
    ' 初始化变量
    m_lngTargetHwnd = 0
    
    ' 枚举所有窗口查找目标窗体
    Call EnumWindows(AddressOf EnumWindowsProc, 0)
    
    ' 检查是否找到目标窗体
    If m_lngTargetHwnd <> 0 Then
        ' 尝试激活窗体
        If SetForegroundWindow(m_lngTargetHwnd) <> 0 Then
            blnResult = True
        Else
            blnResult = False
        End If
    Else
        blnResult = False
    End If
    
    f_ActivateForm = blnResult
End Function

'------------------------------------------------------------------------------
' 函数: EnumWindowsProc
' 功能: 枚举窗口的回调函数
' 参数:
'   hwnd - 窗口句柄
'   lParam - 用户定义参数
' 返回: Boolean - 是否继续枚举
'------------------------------------------------------------------------------
Private Function EnumWindowsProc(ByVal hwnd&, ByVal lParam&) As Boolean
    Dim lngLength&, strTitle$, blnContinue As Boolean
        
    ' 获取窗口标题长度
    lngLength = GetWindowTextLength(hwnd)
    
    If lngLength > 0 Then
        ' 分配缓冲区并获取窗口标题
        strTitle = Space$(lngLength + 1)
        lngLength = GetWindowText(hwnd, strTitle, Len(strTitle))
        
        If lngLength > 0 Then
            ' 截取实际标题文本
            strTitle = Left$(strTitle, lngLength)
            
            ' 检查标题是否包含目标字符串
            If InStr(1, strTitle, m_strWindowTitle, vbTextCompare) > 0 Then
                ' 找到目标窗体,保存句柄
                m_lngTargetHwnd = hwnd
                ' 停止枚举
                blnContinue = False
            Else
                ' 继续枚举
                blnContinue = True
            End If
        Else
            ' 继续枚举
            blnContinue = True
        End If
    Else
        ' 继续枚举
        blnContinue = True
    End If
    
    EnumWindowsProc = blnContinue
End Function


 
全部回复(0)
首页 | 电脑版 |