delphi抓取QQ聊天窗口实例

function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): Boolean ;stdcall;
// EnumWindows 专用的回调函数的格式:
// function EnumWindowsProc(
// hwnd: HWND;    {找到的窗口句柄}
// lParam: LPARAM   {EnumWindows 传给的参数; 因为它是指针, 可传入, 但一般用作传出数据}
// ): Boolean; stdcall; {函数返回 False 时, 调用它的 EnumWindows 将停止遍历并返回 False}
function EnumWindowsProc(hwnd: HWND; lParam: LPARAM): Boolean ;stdcall;
var
  WindowText    : string  ;       // 窗体标题
  WindowClass   : string  ;       // 窗体类名
  SendHwnd      : DWORD   ;       // 发送按钮
  tdiahandle    : THandle ;
  tedithandle   : THandle ;
  tduihuakhandle: THandle ;
 begin
  if ( IsWindowVisible(hwnd) or IsIconic(hwnd) ) and
       (
        (GetWindowLong(hwnd, GWL_HWNDPARENT) = 0) or
        (GetWindowLong(hwnd, GWL_HWNDPARENT) = Longint(GetDesktopWindow))
       ) and
     ( GetWindowLong(hwnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0)  then
  begin
   {-----标题文字------}
    SetLength(WindowText, GetWindowTextLength(hwnd)+2);
    Getwindowtext(hwnd, Pchar(WindowText), GetWindowTextLength(hwnd)+2);
    WindowText := string(Pchar(WindowText));

    {-----窗体类名------}
    SetLength(WindowClass, 512);
    GetClassName(hwnd, Pchar(WindowClass), 512);
    WindowClass := string(Pchar(WindowClass) );
    if SameStr(WindowClass , 'TXGuiFoundation') then
    begin
      Form1.mmo.Lines.Add(' 窗口标题:' + WindowText +
                          ' 窗体类名: ' + WindowClass);
      BringWindowToTop(hwnd);
      SetWndText(hwnd,'hello,delphi test');
//      tduihuakhandle := GetDlgItem(hwnd, 0);
//      SendMessage()
//      SendHwnd := FindWindowEx(hwnd , 0, nil, '发送(S)');   //找到送信息按钮,
//      ShowMessage(string(SendHwnd));
    end;
  end;
  Result := True;
 end;
procedure TForm1.btn1Click(Sender: TObject);
begin
   Form1.mmo.Clear;
// EnumWindows 的功能是遍历所有顶层窗口
// function EnumWindows(
// lpEnumFunc: TFNWndEnumProc; {回调函数指针}
// lParam: LPARAM       {给回调函数的参数, 它对应回调函数的第二个参数}
// ): BOOL; stdcall; //成功与否, 其实是返回了回调函数的返回值   
   EnumWindows(@EnumWindowsProc ,0);
   FQQMessage := edtQQMessage.Text;
end;


你可能感兴趣的:(Delphi)