先看这个代码,没有问题,能正常运行
Option Explicit
  
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  
Private Const BM_CLICK = &HF5
  
Private Sub Command1_Click()
    Dim hStartButton     As Long
    Dim hTaskBar     As Long
    hTaskBar = FindWindow(vbNullString, "显示 属性")
    hStartButton = FindWindowEx(hTaskBar, 0&, "Button", "取消")
    SendMessage hStartButton, BM_CLICK, 0, 0
End Sub--------------------------------------------------------------
而获取自身的按钮,却找不到。
Option Explicit
  
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  
Private Const BM_CLICK = &HF5
  
Private Sub Command1_Click()
    Dim hStartButton     As Long
    Dim hTaskBar     As Long
    hTaskBar = FindWindow(vbNullString, "Form1")
    hStartButton = FindWindowEx(hTaskBar, 0&, "Button", "Command2")
    SendMessage hStartButton, BM_CLICK, 0, 0
End SubPrivate Sub Command2_Click()
MsgBox "ok"
End Sub
试了好几次hStartButton均为0.。各位大侠帮忙看下,小弟对api不是很熟悉。

解决方案 »

  1.   

    Option ExplicitPrivate Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
      
    Private Const BM_CLICK = &HF5
      
    Private Sub Command1_Click()
        Dim hStartButton    As Long
        Dim hTaskBar    As Long
        hTaskBar = FindWindow(vbNullString, "Form1")
        hStartButton = FindWindowEx(hTaskBar, 0&, "ThunderCommandButton", "Command2")
        'VB6.0中Command的类名叫  "ThunderCommandButton"  不叫 "Button"
        SendMessage hStartButton, BM_CLICK, 0, 0
    End SubPrivate Sub Command2_Click()
    MsgBox "ok"
    End Sub