var Hand: THandle; begin Hand := Finwindow('要关闭的程序的窗口标题',nil); Sendmessage(hand, wm_close,nil,nil); end;
function MyExecute(const Command: string; bWaitExecute: Boolean; bShowWindow: Boolean; PI: PProcessInformation): Boolean; //在自己的进程中运行别的程序,调用举例:MyExecute('C:\WINNT\system32\net.exe send huo aa',true,true,nil); function TMyClass.MyExecute(const Command: string; bWaitExecute, bShowWindow: Boolean; PI: PProcessInformation): Boolean; var StartupInfo : TStartupInfo; ProcessInformation: TProcessInformation; begin FillChar(StartupInfo, SizeOf(TStartupInfo), 0); with StartupInfo do begin cb := SizeOf(TStartupInfo); dwFlags := STARTF_USESHOWWINDOW; if bShowWindow then wShowWindow := SW_NORMAL else wShowWindow := SW_HIDE; end; Result := CreateProcess(nil, PChar(Command), nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInformation); if not Result then Exit; if bWaitExecute then WaitForSingleObject(ProcessInformation.hProcess, INFINITE); if Assigned(PI) then Move(ProcessInformation, PI^, SizeOf(ProcessInformation)); end; 你用这种方法调用的话,在你的程序中退出时,会自动退出他的
to huojiehai(海天子) 您的方法不行!
to ljmanage(过客) 您的办法有点问题,启动程序时,只看得到那个小程序,看不到主程序运行,如果退出小程序后,主程序才会出现。我的目的就是怎样在程序启动时启动我指定的程序,并且在主程序退出时再退出它!
不好意思,刚才反了 procedure TForm1.Button1Click(Sender: TObject); var H:THandle; P:DWORD; begin H:=FindWindow(nil,'无标题 - 记事本'); if H<>0 then begin GetWindowThreadProcessId(H,@P); if P<>0 then TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF); end; end;
to huojiehai(海天子) 不好意思,出现了一个小现象:我的那个小程序在运行时是出现在右下角的系统栏的。用您的办法退出时,那个图标还在,可是鼠标移上去那个图标就马上消失了。我想您的办法是中断了那个小程序,有没有让它正常退出的办法?
To yundou()应该是退出了,按你说的意思,是你的小程序有点Bug 小程序中的Form1的OnDestroy要写如下语句 Shell_NotifyIcon(NIM_DELETE, @FIconData);//FIconData为你程序中用到的TNotifyIconData类型的变量
Hand: THandle;
begin
Hand := Finwindow('要关闭的程序的窗口标题',nil);
Sendmessage(hand, wm_close,nil,nil);
end;
bShowWindow: Boolean; PI: PProcessInformation): Boolean;
//在自己的进程中运行别的程序,调用举例:MyExecute('C:\WINNT\system32\net.exe send huo aa',true,true,nil);
function TMyClass.MyExecute(const Command: string; bWaitExecute,
bShowWindow: Boolean; PI: PProcessInformation): Boolean;
var
StartupInfo : TStartupInfo;
ProcessInformation: TProcessInformation;
begin
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
with StartupInfo do
begin
cb := SizeOf(TStartupInfo);
dwFlags := STARTF_USESHOWWINDOW;
if bShowWindow then
wShowWindow := SW_NORMAL
else
wShowWindow := SW_HIDE;
end;
Result := CreateProcess(nil, PChar(Command),
nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
StartupInfo, ProcessInformation);
if not Result then
Exit;
if bWaitExecute then
WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
if Assigned(PI) then
Move(ProcessInformation, PI^, SizeOf(ProcessInformation));
end;
你用这种方法调用的话,在你的程序中退出时,会自动退出他的
procedure TForm1.Button1Click(Sender: TObject);
var
H:THandle;
P:DWORD;
begin
H:=FindWindow(nil,'无标题 - 记事本');
if H<>0 then
begin
GetWindowThreadProcessId(H,@P);
if P<>0 then
TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);
end;
end;
小程序中的Form1的OnDestroy要写如下语句
Shell_NotifyIcon(NIM_DELETE, @FIconData);//FIconData为你程序中用到的TNotifyIconData类型的变量
SendMessage WM_CLose对于有Window的都OK
procedure TForm1.FormDestroy(Sender: TObject);
beginend;
begin
Shell_NotifyIcon(NIM_DELETE, @FIconData);//TNotifyIconData类型的变量
//Shell_NotifyIcon(NIM_ADD, @FIconData)就是这里的FIconData
end;