delphi 调用 EXE 并在Panle中运行
求助delphi调用外部EXE文件并在Panle中运行谢谢回复,,,,,,能给个实例吗?gentleman66@qq.com...
求助 delphi 调用外部EXE文件 并在Panle中运行谢谢回复,,,,,, 能给个实例吗?gentleman66@qq.com
展开
2个回答
展开全部
界面上就4个常用控件,全部的代码在此,自己照着代码加控件
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.
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.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询