Einzelnen Beitrag anzeigen

torud

Registriert seit: 26. Jul 2002
Ort: Sachsen
1.198 Beiträge
 
Delphi XE5 Professional
 
#1

Handle von Sub-Fenster anderer Applikation implementieren

  Alt 17. Apr 2010, 13:47
Hallo Wissende,

ich möchte eine andere Application starten und mir das Handle eines der Unter-Forms in den Canvas eines TIMage oder eine TPaintbox zeichnen lassen. Ich weiss, dass das Kollegen mit C# schon gemacht haben.

Wie ich die andere Applikation starte und das Handle des Fenster bekomme, habe ich mir hier im Forum schon zusammen gesucht.

Aber wenn ich dann das erhaltene Handle zuweise bleibt das Image oder die Paintbox leer. Was mache ich da falsch?

Hier mal mein Code:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  PFindWindowStruct = ^TFindWindowStruct;
  TFindWindowStruct = record
    Caption: string;
    ClassName: String;
    WindowHandle: THandle;
end;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Image1: TImage;
    Button1: TButton;
    PaintBox1: TPaintBox;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//sammeln aller infos und schreiben dieser in eine listbox - nur so, zum testen
function EnumWinProc(Wnd: THandle; LParam: LongInt): Boolean; stdcall;
var
 WinCaption : string;
 Len: integer;
begin
 Result := True;
 Len := GetWindowTextLength(Wnd);
 SetLength(WinCaption, Len);
 GetWindowText(Wnd, PChar(WinCaption), Len+1);
 if Trim(WinCaption) <> 'then
   Form1.Listbox1.Items.Add(Format('%.6x : %s', [Wnd, WinCaption]));
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  EnumWindows(@EnumWinProc, 0);
end;

function EnumWindowsProc(hWindow: hWnd; lParam: LongInt): boolean; stdcall;
var lpBuffer: PChar;
    WindowCaptionFound: boolean;
    ClassNameFound: boolean;
begin
  GetMem(lpBuffer, 255);
  result:=true;
  WindowCaptionFound:=false;
  ClassNameFound:=false;
  try
    if GetWindowText(hWindow, lpBuffer,255)>0 then
      if Pos(PFindWindowStruct(lParam).Caption, StrPas(lpBuffer))>0
      then WindowCaptionFound:=true;
    if PFindWindowStruct(lParam).ClassName='then
      ClassNameFound:=true
      else if GetClassName(hWindow, lpBuffer, 255)>0 then
        if Pos(PFindWindowStruct(lParam).ClassName, StrPas(lpBuffer))>0
        then ClassNameFound:=true;
    if (WindowCaptionFound and ClassNameFound) then begin
      PFindWindowStruct(lParam).WindowHandle:=hWindow;
      result:=false;
    end;
  finally
    FreeMem(lpBuffer, sizeof(lpBuffer^));
  end;
end;

function FindAWindow(WinCaption: string; WinClassName: string): THandle;
var WindowInfo: TFindWindowStruct;
begin
  with WindowInfo do begin
    caption := WinCaption;
    className := WinClassName;
    WindowHandle := 0;
    EnumWindows(@EnumWindowsProc, LongInt(@WindowInfo));
    result := WindowHandle;
  end;
end;

//hier wird das fenster gesucht und das handle zugewiesen
procedure TForm1.Button1Click(Sender: TObject);
var TheWindowHandle: THandle;
begin
  TheWindowHandle:=FindAWindow('Renderer', '');
  if TheWindowHandle=0 then
    ShowMessage('Window not found!')
  else
    PaintBox1.Canvas.Handle := (TheWindowHandle);
    PaintBox1.Repaint;

    //ShowWindow(TheWindowHandle, SW_SHOWMINNOACTIVE);

    //hier wird das fenster in den vordergrund geholt
    //allerdings samt des hauptfenstrers der anderen applikation
    //BringWindowToTop(TheWindowHandle); end;

end.
Es ist so, dass ich keine Fehlermeldung erhalte, das Handle wird also korrekt ermittelt.
Ich mache Scheinbar nur noch einen Fehler bei der Übergabe des Handles an die Paintbox, respektive das Image.
Wäre das überhaupt der richtige Weg, oder sollte ich es besser anders machen?
Danke
Tom
  Mit Zitat antworten Zitat