有一個程序, 
title 名含有 text1.text 中的字
如何搜索含有text1.text 的程序,並focus

解决方案 »

  1.   

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPrivate Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End TypePrivate Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As LongPrivate Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZEPrivate Sub Command1_Click()
    Dim hwnd As Long, ret As Long
    Dim mrect As RECThwnd = FindWindow(vbNullString, Text1.Text)If hwnd Then
    GetWindowRect hwnd, mrect
    SetWindowPos hwnd, HWND_NOTOPMOST, mrect.Left, mrect.Top, mrect.Right - mrect.Left, mrect.Bottom - mrect.Top, TOPMOST_FLAGS
    End IfEnd Sub