3. 关闭已开启的外部应用程序 ⑴ 通过调用两个API函数,可以实现该功能。这两个函数分别为: ① FindWindow函数 该函数用于查找与指定的类名和窗口名相匹配的高层窗口,如果查找成功,返回非0值,否则返回0。 ② SendMessage函数 此函数向一个或多个窗口发送指定的消息。在此通过发送WM_CLOSE消息来关闭指定的外部应用程序。 ⑵ 通过编写标题为“关闭已开启的外部应用程序”组件的OnClick事件,来关闭已开启的外部应用程序。代码如下:procedure TForm1.Button2Click(Sender: TObject);varhWndClose: HWnd; file://存储指定的外部应用程序窗口句柄str: String; file://存储指定的外部应用程序的窗口名beginstr := InputBox('提示','请输入应用程序名:',''); file://获取要关闭的应用程序窗口名if str <> '' then beginfile://根据窗口名查找要关闭的窗口句柄hWndClose := FindWindow(nil, PChar(str));if hWndClose <> 0 then file://如果查找成功,则发送消息,关闭指定的窗口SendMessage(hWndClose,WM_CLOSE,0,0);else file://否则,给出提示信息ShowMessage('没找到指定的应用程序,所以无法关闭!');end;
應該分開判斷吧, 沒有一個統一的Office 可以!!uses ComObj, ActiveX; function IsObjectActive(ClassName: string): Boolean; var ClassID: TCLSID; Unknown: IUnknown; begin try ClassID := ProgIDToClassID(ClassName); Result := GetActiveObject(ClassID, nil, Unknown) = S_OK; except // raise; Result := False; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if IsObjectActive('Word.Application') then ShowMessage('Word is running !'); if IsObjectActive('Excel.Application') then ShowMessage('Excel is running !'); if IsObjectActive('Outlook.Application') then ShowMessage('Outlook is running !'); if IsObjectActive('Access.Application') then ShowMessage('Access is running !'); if IsObjectActive('Powerpoint.Application') then ShowMessage('Powerpoint is running !'); end;
ComObj, ActiveX; function IsObjectActive(ClassName: string): Boolean;
var
ClassID: TCLSID;
Unknown: IUnknown;
begin
try
ClassID := ProgIDToClassID(ClassName);
Result := GetActiveObject(ClassID, nil, Unknown) = S_OK;
except
// raise;
Result := False;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
begin
if IsObjectActive('Word.Application') then ShowMessage('Word is running !');
if IsObjectActive('Excel.Application') then ShowMessage('Excel is running !');
if IsObjectActive('Outlook.Application') then ShowMessage('Outlook is running !');
if IsObjectActive('Access.Application') then ShowMessage('Access is running !');
if IsObjectActive('Powerpoint.Application') then ShowMessage('Powerpoint is running !');
end;
可是问题的关键是如何保存正在运行的工作。我不知道该怎么做呀!:(