Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#32

Re: Senden Von Strings an andere Programme

  Alt 3. Feb 2008, 14:02
Zitat von k4ni:
Wie ich rauslesen konnte willst du eigentlich nur einen text an ein ICQ-fenster schicken, richtig?
Da hab ich mir erst gestern was gemacht, hier:
Delphi-Quellcode:
procedure SendText(AText: String);
var lCount : Integer;
    h : HWND;
begin
h := FindWindow('IcqClass...', nil);
h := FindWindowEx(h, 0, 'Class2', nil); // Hier die class vom memo im ICQ chat fenster.
for lCount := 1 To Length(AText) Do
  begin
    PostMessageA(h, WM_Char, ord(AText[lCount]), 0);
  end;
end;
Kann nicht funktionieren denn es handelt sich um die Internet Explorer_Server Klasse.

Zitat:
Nachtrag: Das ICQ Hauptfenster hat den gleichen Klassennamen wie das Chatfenster.
Darum findet es unter Umständen das falsche Fenster.
Mit EnumWindows suchen wir nun das richtige Fenster. Neuer Code:

Delphi-Quellcode:
uses
  ActiveX, MSHTML, SHDocVw;


// Hilfsfunktion zum finden von Childfenstern

function FindWindowEx2(hParent: HWND; ChildClassName: string; ChildNr: Word): HWND;
var
  i: Word;
  hChild: HWND;
begin
  hChild := 0;
  Result := 0;
  ChildNr := ChildNr - 1;
  for i := 0 to ChildNr do
  begin
    hChild := FindWindowEx(hParent, hChild, PChar(ChildClassName), nil);
    if hChild = 0 then
      Exit;
    Result := hChild;
  end;
end;

type
  TObjectFromLResult = function(LRESULT: lResult; const IID: TIID; WPARAM: wParam; out pObject): HRESULT; stdcall;

 // IHTMLDocument2 vom Handle Internet Explorer_Server ermitteln

function GetHTMLDocumentFromHWND(wnd: HWND): IHTMLDocument2;
var
  hInst: HWND;
  lRes: Cardinal;
  MSG: Integer;
  ObjectFromLresult: TObjectFromLresult;
  pDoc: IHTMLDocument2;
begin
  Result := nil;
  hInst := LoadLibrary('Oleacc.dll');
  @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
  if @ObjectFromLresult <> nil then begin
    try
      MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
      SendMessageTimeOut(wnd, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
      if ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc) = S_OK then
        Result := pDoc;
    finally
      FreeLibrary(hInst);
    end;
  end;
end;

 // Das Handle vom ICQ Chat Control ermitteln

function GetICQChatHandle(wndICQChat: HWND; DisplayName: string): HWND;
const
  ICQ_WIN_CLASS = '__oxFrame.class__';
  ICQ_MainWIN_Caption = 'ICQ';
var
  wnd: HWND;
  htmlDoc: IHTMLDocument2;
  Params: OleVariant;
  ClassName, captionName: string;
begin
  Result := 0;
  SetLength(className, 20);
  SetLength(className, GetClassName(wndICQChat, PChar(className), length(ICQ_WIN_CLASS) + 1));
  SetLength(captionName, 20);
  SetLength(captionName, GetWindowText(wndICQChat, PChar(captionName), 256));
  if (className = ICQ_WIN_CLASS) then // Falls ICQ Fenster Klasse übereinstimmt
    if (captionName <> 'ICQ') then // aber nicht das Hauptfenster
    // Falls ein Displayname angegeben ist muss er übereinstimmen
      if (DisplayName = '') or (DisplayName = captionName) then
      begin
        wnd := FindWindowEx2(wndICQChat, ICQ_WIN_CLASS, 2);
        if wnd <> 0 then
        begin
     // Internet Explorer_Server vom Chatfenster suchen
          Result := FindWindowEx(wnd, 0, 'Internet Explorer_Server', nil);
        end;
      end;
end;

type
  PEnumInfo = ^TEnumInfo;
  TEnumInfo = record wndICQChat: HWND; DisplayName: string; end;

function EnumWindowsProc(wnd: HWND; var EnumInfo: TEnumInfo): Bool; stdcall; export;
var
  ClassName: array[0..255] of char;
  wndICQChat: HWND;
begin
  Result := True;
  wndICQChat := GetICQChatHandle(wnd, EnumInfo.DisplayName); // ICQ Chat Handle ermitteln
  if wndICQChat <> 0 then
  begin
    // gefunden. EnumWindows abbrechen
    Result := False;
    EnumInfo.wndICQChat := wndICQChat;
  end;
end;


// IHTMLDocument2 vom ICQ Chat Control ermitteln

function GetICQChatDoc(DisplayName: string): IHTMLDocument2;
var
  wndICQChat: HWND;
  htmlDoc: IHTMLDocument2;
  EnumInfo: TEnumInfo;
begin
  EnumInfo.DisplayName := DisplayName;
  // alle Fenster durchlaufen
  EnumWindows(@EnumWindowsProc, Integer(@EnumInfo));
  wndICQChat := EnumInfo.wndICQChat;
  if wndICQChat <> 0 then
  begin
  // htmlDoc vom Handle ermitteln
    htmlDoc := GetHTMLDocumentFromHWnd(wndICQChat);
    // body.innerText zurückgeben
    if htmlDoc <> nil then
      Result := htmlDoc;
  end;
end;

// den Text vom ICQ Chat Control ermitteln

function GetICQChatText(DisplayName: string): string;
var
  ICQhtmlDoc: IHTMLDocument2;
begin
  ICQhtmlDoc := GetICQChatDoc(DisplayName);
  if ICQhtmlDoc <> nil then
    Result := ICQhtmlDoc.body.innerText;
end;

// Test: Text vom ICQ Chat Control ermitteln

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetICQChatText('')); // hier kannst du ICQ Anzeigenamen übergeben
end;
Thomas
  Mit Zitat antworten Zitat