原文地址http://blog.csdn.net/tonylk/archive/2005/04/21/357701.aspx
后台完美调用外部程序
-----------------------------------------------------
主代码unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FDesktop:HDESK; { Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
const
DesktopName = 'MYDESK';implementationuses Unit3;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var
StartInfo:TStartupInfo;
FProceInfo :TProcessInformation;
begin
FillChar(StartInfo, sizeof(StartInfo), 0);
StartInfo.cb:=sizeof(StartInfo);
StartInfo.lpDesktop:=PChar(DesktopName); //指定Desktop的名称即可
StartInfo.wShowWindow:=SW_SHOW;
StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartInfo.hStdError:=0;
StartInfo.hStdInput:=0;
StartInfo.hStdOutput:=0;
if not CreateProcess('D:\Downloads\quickpar\QuickPar\QuickPar.exe', nil,nil,nil,true,0,nil,'D:\Downloads\quickpar\QuickPar',StartInfo,FProceInfo) then begin
MessageBox(Application.Handle,PChar(inttostr(GetLastError)),PChar(Application.Title),MB_ICONWARNING);
exit;
end;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
FDesktop:=CreateDesktop(DesktopName,nil,nil,0,GENERIC_ALL,nil);//创建新的虚拟桌面
if FDesktop=0 then
ShowMessage( IntToStr(GetLastError));
end;procedure TForm2.Button3Click(Sender: TObject);
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:=FindWindowThread.WindowHandle;
finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;
end;
新线程代码
unit Unit3;interfaceuses
Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};type TFindWindowThread = class(TThread)
private
FDesktop:THandle;
FWindowHandle:THandle;
protected
procedure Execute; override;
public
constructor Create(ACreateSuspended:Boolean;const ADesktop:THandle);reintroduce;
property WindowHandle:THandle read FWindowHandle;
end;implementationuses Unit2;
procedure TFindWindowThread.Execute;
var
I:Integer;begin
NameThreadForDebugging('TThread');
//make the current thread find window on the new desktop!
if not SetThreadDesktop(FDesktop) then begin
exit;
end;
for I:=0 to 60 do begin //wait 30 seconds for open the main window
FWindowHandle:=FindWindow(nil,PChar('WindowCaption'));
if FWindowHandle<>0 then begin
break;
end;
Sleep(500);
end;end;
constructor TFindWindowThread.Create(ACreateSuspended:Boolean;const ADesktop:THandle);
begin
inherited Create(ACreateSuspended);
FDesktop:=ADesktop;
end;
end.现在
procedure TForm2.Button3Click(Sender: TObject);
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:=FindWindowThread.WindowHandle;
finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;
button 那里编译不过,谁帮我看看代码
我现在就是想在虚拟桌面控制 'D:\Downloads\quickpar\QuickPar\QuickPar.exe'
这个软件 我共享了
http://u.115.com/file/t741585673#目的 在QuickPar.exe,添加一个文件,然后按下创建。
后台完美调用外部程序
-----------------------------------------------------
主代码unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FDesktop:HDESK; { Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
const
DesktopName = 'MYDESK';implementationuses Unit3;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var
StartInfo:TStartupInfo;
FProceInfo :TProcessInformation;
begin
FillChar(StartInfo, sizeof(StartInfo), 0);
StartInfo.cb:=sizeof(StartInfo);
StartInfo.lpDesktop:=PChar(DesktopName); //指定Desktop的名称即可
StartInfo.wShowWindow:=SW_SHOW;
StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartInfo.hStdError:=0;
StartInfo.hStdInput:=0;
StartInfo.hStdOutput:=0;
if not CreateProcess('D:\Downloads\quickpar\QuickPar\QuickPar.exe', nil,nil,nil,true,0,nil,'D:\Downloads\quickpar\QuickPar',StartInfo,FProceInfo) then begin
MessageBox(Application.Handle,PChar(inttostr(GetLastError)),PChar(Application.Title),MB_ICONWARNING);
exit;
end;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
FDesktop:=CreateDesktop(DesktopName,nil,nil,0,GENERIC_ALL,nil);//创建新的虚拟桌面
if FDesktop=0 then
ShowMessage( IntToStr(GetLastError));
end;procedure TForm2.Button3Click(Sender: TObject);
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:=FindWindowThread.WindowHandle;
finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;
end;
新线程代码
unit Unit3;interfaceuses
Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};type TFindWindowThread = class(TThread)
private
FDesktop:THandle;
FWindowHandle:THandle;
protected
procedure Execute; override;
public
constructor Create(ACreateSuspended:Boolean;const ADesktop:THandle);reintroduce;
property WindowHandle:THandle read FWindowHandle;
end;implementationuses Unit2;
procedure TFindWindowThread.Execute;
var
I:Integer;begin
NameThreadForDebugging('TThread');
//make the current thread find window on the new desktop!
if not SetThreadDesktop(FDesktop) then begin
exit;
end;
for I:=0 to 60 do begin //wait 30 seconds for open the main window
FWindowHandle:=FindWindow(nil,PChar('WindowCaption'));
if FWindowHandle<>0 then begin
break;
end;
Sleep(500);
end;end;
constructor TFindWindowThread.Create(ACreateSuspended:Boolean;const ADesktop:THandle);
begin
inherited Create(ACreateSuspended);
FDesktop:=ADesktop;
end;
end.现在
procedure TForm2.Button3Click(Sender: TObject);
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:=FindWindowThread.WindowHandle;
finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;
button 那里编译不过,谁帮我看看代码
我现在就是想在虚拟桌面控制 'D:\Downloads\quickpar\QuickPar\QuickPar.exe'
这个软件 我共享了
http://u.115.com/file/t741585673#目的 在QuickPar.exe,添加一个文件,然后按下创建。
解决方案 »
- 在Form上放一个PageControl 后,Form的 OnKeyDown 失效
- TComponent构造函数
- 远程调用com对象时显示rpc服务器不可用,急!
- 大家看看这个dll啊
- 300解!
- “木鱼”的贴……谢谢大家!!!
- Delphi调用子窗体,关闭子窗体后,再次打开子窗体出现内存地址错误!万分感激!
- 各位老大 你们是如何解决学习语言方面的问题的,如何让自已的知识面丰富的?
- 请教大家!last 和 findlast 的区别? 在线等待,马上给分!
- 救命!谁有Delphi5 Update Pack1的Key?
- RO保存文件到Oracle的Blob字段问题(来人接分)~
- 如何在 mxoutlookbarpro 的button上添加图片!!
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:=FindWindowThread.WindowHandle;
finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;
就这里编译不过
FindWindowThread,
FMainWindowHandle
看来你不会delphi吧
我是搞硬件,这个是我第一次用delphi
FindWindowThread:TThread;
FMainWindowHandle:THandle;
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:= FindWindowThread.WindowHandle;
finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;错误
Undeclared identifier: 'WindowHandle'
这个怎么解决
FindWindowThread:TThread;
FMainWindowHandle:THandle;
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:= FindWindowThread.Handle;//原来(FindWindowThread.WindowHandle;) finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;
这样会编译能通过。
调试FMainWindowHandle有反馈值但是怎么确定这个是我调用的程序的窗口Handle呢
unit Unit3;interfaceuses
Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};type TFindWindowThread = class(TThread)
private
FDesktop:THandle;
FWindowHandle:THandle;
protected
procedure Execute; override;
public
constructor Create(ACreateSuspended:Boolean;const ADesktop:THandle);reintroduce;
property WindowHandle:THandle read FWindowHandle;
end;implementationuses Unit2;
procedure TFindWindowThread.Execute;
var
I:Integer;begin
NameThreadForDebugging('TThread');
//make the current thread find window on the new desktop!
if not SetThreadDesktop(FDesktop) then begin
exit;
end;
for I:=0 to 60 do begin //wait 30 seconds for open the main window
FWindowHandle:=FindWindow(nil,PChar('WindowCaption'));
if FWindowHandle<>0 then begin
break;
end;
Sleep(500);
end;end;
constructor TFindWindowThread.Create(ACreateSuspended:Boolean;const ADesktop:THandle);
begin
inherited Create(ACreateSuspended);
FDesktop:=ADesktop;
end;
end.
主程序:unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FDesktop:HDESK; { Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
const
DesktopName = 'MYDESK';implementationuses Unit3;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var
StartInfo:TStartupInfo;
FProceInfo :TProcessInformation;
begin
FillChar(StartInfo, sizeof(StartInfo), 0);
StartInfo.cb:=sizeof(StartInfo);
StartInfo.lpDesktop:=PChar(DesktopName); //指定Desktop的名称即可
StartInfo.wShowWindow:=SW_SHOW;
StartInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartInfo.hStdError:=0;
StartInfo.hStdInput:=0;
StartInfo.hStdOutput:=0;
if not CreateProcess('D:\Downloads\quickpar\QuickPar\QuickPar.exe', nil,nil,nil,true,0,nil,'D:\Downloads\quickpar\QuickPar',StartInfo,FProceInfo) then begin
MessageBox(Application.Handle,PChar(inttostr(GetLastError)),PChar(Application.Title),MB_ICONWARNING);
exit;
end;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
FDesktop:=CreateDesktop(DesktopName,nil,nil,0,GENERIC_ALL,nil);//创建新的虚拟桌面
if FDesktop=0 then
ShowMessage( IntToStr(GetLastError));
end;procedure TForm2.Button3Click(Sender: TObject);
var
FindWindowThread:TThread;
FMainWindowHandle:THandle;
begin
FindWindowThread:=TFindWindowThread.Create(false,FDesktop);
try
FindWindowThread.WaitFor;
FMainWindowHandle:=FindWindowThread.Handle;
finally
FindWindowThread.Free;
end;
if FMainWindowHandle=0 then begin
MessageBox(Application.Handle,'Error when init voice (6).',PChar(Application.Title),MB_ICONWARNING);
exit;
end;
end;
这样调试是过了的
#8 zkroy36我在unit2里 uses Unit3 了的;