如何让其它程序的窗口,在我自己程序中的Panel控件中显示.? ( 积分: 200 )

  • 主题发起人 主题发起人 laohe
  • 开始时间 开始时间
L

laohe

Unregistered / Unconfirmed
GUEST, unregistred user!
例如,我想让一个游戏(1.exe)的主窗口,显示到我软件的Panel控件中显示.

//---------下面次要,主要的是上面的问题.谢谢大侠先帮我看看
如果可能的话,我也想可以控制他(至少也可以获取他里面的某点颜色)
 
例如,我想让一个游戏(1.exe)的主窗口,显示到我软件的Panel控件中显示.

//---------下面次要,主要的是上面的问题.谢谢大侠先帮我看看
如果可能的话,我也想可以控制他(至少也可以获取他里面的某点颜色)
 
你看一下这个吧,它是将其它程序在自己的MDI窗体里打开的,你看看有没有借荐的吧
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
unit Unit1;

interface

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

type
PProcessWindow = ^TProcessWindow;
TProcessWindow = record
TargetProcessID: Cardinal;
FoundWindow: hWnd;
end;
TForm1 = class(TForm)
Button1: TButton;
OpenD: TOpenDialog;
Panel1: TPanel;
Splitter1: TSplitter;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
hWin: HWND = 0;
implementation

{$R *.dfm}

function EnumWindowsProc(Wnd: hWnd; ProcWndInfo: PProcessWindow): BOOL; stdcall;
var
WndProcessID: Cardinal;
begin
GetWindowThreadProcessId(Wnd, @WndProcessID);
if WndProcessID = ProcWndInfo^.TargetProcessID then begin
ProcWndInfo^.FoundWindow := Wnd;
Result := False; // This tells EnumWindows to stop enumerating since we've already found a window.
end else Result := True; // Keep searching
end;

function GetProcessWindow(TargetProcessID: Cardinal): hWnd;
var
ProcWndInfo: TProcessWindow;
begin
ProcWndInfo.TargetProcessID := TargetProcessID;
ProcWndInfo.FoundWindow := 0;
EnumWindows(@EnumWindowsProc, Integer(@ProcWndInfo));
Result := ProcWndInfo.FoundWindow;
end;

function RunAppInPanel(const AppName: string; PanelHandle: HWND): boolean;
var
si: STARTUPINFO;
pi: TProcessInformation;
begin
FillChar(si, SizeOf(si), 0);
si.cb := SizeOf(si);
si.wShowWindow := SW_SHOW;
result := CreateProcess(nil, PChar(AppName), nil,
nil, true, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi);

if not result then exit;

WaitForInputIdle(pi.hProcess, 10000); // let process start!

hWin := GetProcessWindow(pi.dwProcessID);
if hWin > 0 then begin
Windows.SetParent(hWin, PanelHandle);
SetWindowPos(hWin, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
result := true;
end;

// we don't need the handles so we close them
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenD.Execute then begin
if hWin > 0 then PostMessage(hWin, WM_CLOSE, 0, 0); // close any app currently opened
if not RunAppInPanel(OpenD.FileName, Panel1.Handle) then ShowMessage('App not found');
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if hWin > 0 then
PostMessage(hWin, WM_CLOSE, 0, 0);
end;

end.
 
使用Frame类型.
 
5411,你说清楚点好吧

有源码最好
 
多人接受答案了。
 
后退
顶部