vb_API函数快速入门

参考:http://www.excelpx.com/thread-153811-1-1.html
自动关闭的对话框

Public Declare Function MsgBoxTimeOut Lib "user32" Alias "MessageBoxTimeoutA" (ByVal hWnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long, ByVal wlange As Long, ByVal dwTimeout As Long) As Long

Public Sub 录入对话()
    '过程,"弹出对话","对话框标题",图标类型,默认参数,N秒后自动关闭
    MsgBoxTimeOut 0, "录入完毕!!", "提示", 64, 0, 1500
    MsgBox 1
End Sub

获取桌面所有一级子窗口的句柄和标题文本

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Sub 获取桌面所有一级子窗口的句柄和标题文本()
    Dim hWnd    As Long
    Dim iNum    As Long
    Dim lpStr   As String * 256
    Dim lgh     As Long
    hWnd = GetWindow(GetDesktopWindow, GW_CHILD)    '取得第1个子窗口句柄
    dd = GetDesktopWindow
    
    Do                                              '循环枚举
        'Debug.Print hWnd
        If hWnd = 0 Then Exit Do                    '句柄为0时结束循环
        iNum = iNum + 1                             '子窗口计数
        Cells(iNum, 1) = iNum                       '记录子窗口序号
        Cells(iNum, 2) = hWnd                       '记录子窗口句柄
        lpStr = ""									
        '下一句代码把标题传给lpStr的前标题长度位,如果lpStr=1234,标题是66,传完后是6634
        lgh = GetWindowText(hWnd, lpStr, 255)       '获取窗口标题文本的长度
        If LCase(lpStr) Like "*notepad*" Then
            fff (hWnd)
            DoEvents
            'Application.Wait (Now + TimeValue("0:00:01"))
        End If
        Cells(iNum, 3) = left(lpStr, lgh)           '记录子窗口标题文本
        hWnd = GetWindow(hWnd, GW_HWNDNEXT)         '查找下一个子窗口
    Loop
End Sub

激活某txt窗口输入并保存

Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As Long) As Long
Declare Function IsWindow Lib "user32" (ByVal hWnd As Long) As Long
'Declare Function GetActiveWindow Lib "user32" () As Long
Declare Sub Sleep Lib "kernel32" (ByVal ms As Long)
Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As rect) As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Type rect
    left As Long
    up As Long
    right As Long
    down As Long
End Type

Function fff(id)
    Dim myrect As rect
    Call GetWindowRect(id, myrect)                 'get id left-up point and right-down point
    'lgh = GetWindowText(id, lpStr, 255)           'get id title text,always raise error
    w3 = SetForegroundWindow(id)                   'set id to Foreground Window
    Application.SendKeys ("XXX")
    Application.SendKeys ("^s")
    
    w1 = GetForegroundWindow
    w2 = IsWindow(w1)
    'Call GetWindowText(w1, strr, 222)
    Call GetWindowRect(w1, myrect)
    Sleep (1111)
    Debug.Print w2
    w3 = SetForegroundWindow(w1)
    Debug.Print w3
End Function

你可能感兴趣的:(vba,excel)