ZT:这其实是个很老的话题在这里写。实在有点没意义。不过还是写写。因为ShellExecute总还是在经常用到。而且平率比较高。但我不打算重点讲shell的参数。因为我打算做个面向对象封装。避开不必要参数方面下次调用。和其它界面API的参数差不多句柄。要实行的操作。程序名。后面两个不重要的参数。然后是消息。ShellExecute主要实施的操作是。打开。查找。打印。因此我打算做三个函数分别封装其具体。由于要打开不同的应用程序重点是句柄和程序名。所以两个属性。就是它们了。然后我打算加入一个其他功能就是关闭应用程序。其实就是对要关闭的应用程序发送WM_CLOSE消息。OK我们就来封装。//这里自定义一个异常以方便我们使用unit myShell;interfaceuses Windows, SysUtils, ShellAPI;//shellexecute就定义在ShellApi单元中 Type TShellException = class (Exception) end;Type TShellExecute = class private FExeName : string ; FHandle : HWND; procedure SetExeName(Value : string); procedure SetHandle (Value : HWND); public constructor Create(h : HWND;PrgName : string ); property ExeName : string read FExeName write SetExeName; property Handle : HWND read FHandle write SetHandle; procedure Execute ; procedure FindExe; procedure PrintExe; procedure ClosePrg; End;implementationConstructor TShellExecute.Create(h : HWND;PrgName : string ); begin if (h>0) and (PrgName<>'') then begin self.ExeName:=PrgName; self.Handle :=h; end else raise TShellException.Create('Handle or PrgName is Null'); end;//执行网页,Email,普通程序 procedure TShellExecute.Execute ; begin ShellExecute(self.FHandle,'open',PChar(self.FExeName),nil,nil, SW_SHOW); end;//查找指定文件 procedure TShellExecute.FindExe; begin ShellExecute(self.FHandle,'find',PChar(self.FExeName),nil,nil, SW_SHOW); end;//打印文件 procedure TShellExecute.PrintExe; begin ShellExecute(self.FHandle,'print',PChar(self.FExeName),nil,nil, SW_HIDE); end;//关闭程序 procedure TShellExecute.ClosePrg; begin self.FHandle:=FindWindow(nil,PChar(self.FExeName)); if FHandle<>0 then SendMessage(self.FHandle,WM_Close,0,0) Else raise TShellException.Create('The Programm is null') end;procedure TShellExecute.SetExeName(Value : string); begin if value <> '' then self.FExeName:=Value else raise TShellException.Create('The Name should not Null'); end;procedure TShellExecute.SetHandle (Value : HWND); begin if Value > 0 then self.FHandle:=value else raise TShellException.Create('The Handle should not Null'); end;end.上面可以看到FileName我是用String定义的。然后用Pchar转意。这样就方便了程序内部调用的通用信。
Windows, SysUtils, ShellAPI;//shellexecute就定义在ShellApi单元中
Type
TShellException = class (Exception)
end;Type
TShellExecute = class
private
FExeName : string ;
FHandle : HWND;
procedure SetExeName(Value : string);
procedure SetHandle (Value : HWND);
public
constructor Create(h : HWND;PrgName : string );
property ExeName : string read FExeName write SetExeName;
property Handle : HWND read FHandle write SetHandle;
procedure Execute ;
procedure FindExe;
procedure PrintExe;
procedure ClosePrg;
End;implementationConstructor TShellExecute.Create(h : HWND;PrgName : string );
begin
if (h>0) and (PrgName<>'') then
begin
self.ExeName:=PrgName;
self.Handle :=h;
end
else
raise TShellException.Create('Handle or PrgName is Null');
end;//执行网页,Email,普通程序
procedure TShellExecute.Execute ;
begin
ShellExecute(self.FHandle,'open',PChar(self.FExeName),nil,nil, SW_SHOW);
end;//查找指定文件
procedure TShellExecute.FindExe;
begin
ShellExecute(self.FHandle,'find',PChar(self.FExeName),nil,nil, SW_SHOW);
end;//打印文件
procedure TShellExecute.PrintExe;
begin
ShellExecute(self.FHandle,'print',PChar(self.FExeName),nil,nil, SW_HIDE);
end;//关闭程序
procedure TShellExecute.ClosePrg;
begin
self.FHandle:=FindWindow(nil,PChar(self.FExeName));
if FHandle<>0 then
SendMessage(self.FHandle,WM_Close,0,0)
Else
raise TShellException.Create('The Programm is null')
end;procedure TShellExecute.SetExeName(Value : string);
begin
if value <> '' then
self.FExeName:=Value
else
raise TShellException.Create('The Name should not Null');
end;procedure TShellExecute.SetHandle (Value : HWND);
begin
if Value > 0 then
self.FHandle:=value
else
raise TShellException.Create('The Handle should not Null');
end;end.上面可以看到FileName我是用String定义的。然后用Pchar转意。这样就方便了程序内部调用的通用信。
我们常用的就是ShellExecute(...........)去打开一个文件或程序
uses
ShellAPI;//在程序主体部分加上ShellAPIprocedure TForm1.N11Click(Sender: TObject);
begin
shellexecute(handle,nil,'http://www.zgzcw.com',nil,nil,sw_normal); //执行打开主网站
end;
对于windows,dos命令可以理解就是一种shell命令,批处理程序就是shell程序。
shell在Linux里是有类似于DOS字符界面的含义。
Linux的内核与外壳有较为明显的界限,Linux的Shell是一种字符界面,而且这种Shell可以支持选择、循环等程序结构,可由来编制Shell脚本程序,类似于DOS的批处理程序。大家不要怀疑上面的话,前天我们学校才考过“操作系统原理”,这些基本上是书上的原话!