利用Delphi編寫Windows外殼擴(kuò)展
對(duì)于操作系統(tǒng)原理比較了解的朋友都會(huì)知道,一個(gè)完備的操作系統(tǒng)都會(huì)提供了一個(gè)外殼(Shell),以方便普通的用戶
使用操作系統(tǒng)提供的各種功能。Windows(在這里指的是Windows 95Windows NT4.0以上版本的操作系統(tǒng))的外殼不但提供
了方便美觀的GUI圖形界面,而且還提供了強(qiáng)大的外殼擴(kuò)展功能,大家可能在很多軟件中看到這些外殼擴(kuò)展了。例如在你的
系統(tǒng)中安裝了Winzip的話,當(dāng)你在Windows Explore中鼠標(biāo)右鍵點(diǎn)擊文件夾或者文件后,在彈出菜單中就會(huì)出現(xiàn)Winzip的壓
縮菜單。又或者Bullet FTP中在Windows資源管理器中出現(xiàn)的FTP站點(diǎn)文件夾。
Windows支持七種類型的外殼擴(kuò)展(稱為Handler),它們相應(yīng)的作用簡(jiǎn)述如下:
(1)Context menu handlers:向特定類型的文件對(duì)象增添上下文相關(guān)菜單;
(2)Drag-and-drop handlers用來(lái)支持當(dāng)用戶對(duì)某種類型的文件對(duì)象進(jìn)行拖放操作時(shí)的OLE數(shù)據(jù)傳輸;
(3)Icon handlers用來(lái)向某個(gè)文件對(duì)象提供一個(gè)特有的圖標(biāo),也可以給某一類文件對(duì)象指定圖標(biāo);
(4)PRoperty sheet handlers給文件對(duì)象增添屬性頁(yè)(就是右鍵點(diǎn)擊文件對(duì)象或文件夾對(duì)象后,在彈出菜單中選屬性
項(xiàng)后出現(xiàn)的對(duì)話框),屬性頁(yè)可以為同一類文件對(duì)象所共有,也可以給一個(gè)文件對(duì)象指定特有的屬性頁(yè);
(5)Copy-hook handlers在文件夾對(duì)象或者打印機(jī)對(duì)象被拷貝、移動(dòng)、刪除和重命名時(shí),就會(huì)被系統(tǒng)調(diào)用,通過(guò)為Windows
增加Copy-hook handlers,可以允許或者禁止其中的某些操作;
(6)Drop target handlers在一個(gè)對(duì)象被拖放到另一個(gè)對(duì)象上時(shí),就會(huì)被系統(tǒng)被調(diào)用;
(7)Data object handlers在文件被拖放、拷貝或者粘貼時(shí),就會(huì)被系統(tǒng)被調(diào)用。
Windows的所有外殼擴(kuò)展都是基于COM(Component Object Model) 組件模型的,外殼是通過(guò)接口(Interface)來(lái)訪問(wèn)對(duì)象的。
外殼擴(kuò)展被設(shè)計(jì)成32位的進(jìn)程中服務(wù)器程序,并且都是以動(dòng)態(tài)鏈接庫(kù)的形式為操作系統(tǒng)提供服務(wù)的。因此,如果要對(duì)Windows
的用戶界面進(jìn)行擴(kuò)充的話,則具備寫COM對(duì)象的一些知識(shí)是十分必要的。 由于篇幅所限,在這里就不介紹COM,讀者可以參考
微軟的MSDN庫(kù)或者相關(guān)的幫助文檔,一個(gè)接口可以看做是一個(gè)特殊的類,它包含一組函數(shù)合過(guò)程可以用來(lái)操作一個(gè)對(duì)象。
寫好外殼擴(kuò)展程序后,必須將它們注冊(cè)才能生效。所有的外殼擴(kuò)展都必須在Windows注冊(cè)表的HKEY_CLASSES_ROOTCLSID鍵
之下進(jìn)行注冊(cè)。在該鍵下面可以找到許多名字像{0000002F-0000-0000-C000-000000000046}的鍵,這類鍵就是全局唯一類標(biāo)識(shí)
符(Guid)。每一個(gè)外殼擴(kuò)展都必須有一個(gè)全局唯一類標(biāo)識(shí)符,Windows正是通過(guò)此唯一類標(biāo)識(shí)符來(lái)找到外殼擴(kuò)展處理程序的。
在類標(biāo)識(shí)符之下的InProcServer32子鍵下記錄著外殼擴(kuò)展動(dòng)態(tài)鏈接庫(kù)在系統(tǒng)中的位置。與某種文件類型關(guān)聯(lián)的外殼擴(kuò)展注冊(cè)在
相應(yīng)類型的shellex主鍵下。如果所處的Windows操作系統(tǒng)為Windows NT,則外殼擴(kuò)展還必須在注冊(cè)表中的
HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionShellExtensionsApproved主鍵下登記。
編譯完外殼擴(kuò)展的DLL程序后就可以用Windows本身提供的regsvr32.exe來(lái)注冊(cè)該DLL服務(wù)器程序了。如果使用Delphi,也可
以在Run菜單中選擇Register ActiveX Server來(lái)注冊(cè)。
下面首先介紹一個(gè)比較常用的外殼擴(kuò)展應(yīng)用:上下文相關(guān)菜單,在Windows中,用鼠標(biāo)右鍵單擊文件或者文件夾時(shí)彈出的那
個(gè)菜單便稱為上下文相關(guān)菜單。要?jiǎng)討B(tài)地在上下文相關(guān)菜單中增添菜單項(xiàng),可以通過(guò)寫Context Menu Handler來(lái)實(shí)現(xiàn)。比如大家
所熟悉的WinZip和UltraEdit等軟件都是通過(guò)編寫Context Menu Handler來(lái)動(dòng)態(tài)地向菜單中增添菜單項(xiàng)的。如果系統(tǒng)中安裝了
WinZip,那么當(dāng)用右鍵單擊一個(gè)名為Windows的文件(夾)時(shí),其上下文相關(guān)菜單就會(huì)有一個(gè)名為Add to Windows.zip的菜單項(xiàng)。
本文要實(shí)現(xiàn)的Context Menu Handler與WinZip提供的上下文菜單相似。它將在任意類型的文件對(duì)象的上下文相關(guān)菜單中添加一個(gè)
文件操作菜單項(xiàng),當(dāng)點(diǎn)擊該項(xiàng)后,接口程序就會(huì)彈出一個(gè)文件操作窗口,執(zhí)行文件拷貝、移動(dòng)等操作。
編寫Context Menu Handler必須實(shí)現(xiàn)IShellExtInit、IContextMenu和TComObjectFactory三個(gè)接口。IShellExtInit實(shí)現(xiàn)
接口的初始化,IContextMenu接口對(duì)象實(shí)現(xiàn)上下文相關(guān)菜單,IComObjectFactory接口實(shí)現(xiàn)對(duì)象的創(chuàng)建。
下面來(lái)介紹具體的程序?qū)崿F(xiàn)。首先在Delphi中點(diǎn)擊菜單的 File|New 項(xiàng),在New Item窗口中選擇DLL建立一個(gè)DLL工程文件。
然后點(diǎn)擊菜單的 File|New 項(xiàng),在New Item窗口中選擇Unit建立一個(gè)Unit文件,點(diǎn)擊點(diǎn)擊菜單的 File|New 項(xiàng),在New Item窗口
中選擇Form建立一個(gè)新的窗口。將將工程文件保存為Contextmenu.dpr ,將Unit1保存為Contextmenuhandle.pas,將Form保存為
OpWindow.pas。
Contextmenu.dpr的程序清單如下:
library contextmenu;
uses
ComServ,
contextmenuhandle in 'contextmenuhandle.pas',
opwindow in 'opwindow.pas' {Form2};
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
{$R *.TLB}
{$R *.RES}
begin
end.
Contextmenuhandle的程序清單如下:
unit ContextMenuHandle;
interface
uses Windows,ActiveX,ComObj,ShlObj,Classes;
type
TContextMenu = class(TComObject,IShellExtInit,IContextMenu)
private
FFileName: array[0..MAX_PATH] of Char;
protected
function IShellExtInit.Initialize = SEIInitialize; // Avoid compiler warning
function SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
hKeyProgID: HKEY): HResult; stdcall;
function QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst, idCmdLast,
uFlags: UINT): HResult; stdcall;
function InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult; stdcall;
function GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
pszName: LPSTR; cchMax: UINT): HResult; stdcall;
end;
const
Class_ContextMenu: TGUID = '{19741013-C829-11D1-8233-0020AF3E97A0}';
{全局唯一標(biāo)識(shí)符(GUID)是一個(gè)16字節(jié)(128為)的值,它唯一地標(biāo)識(shí)一個(gè)接口(interface)}
var
FileList:TStringList;
implementation
uses ComServ, SysUtils, ShellApi, Registry,UnitForm;
function TContextMenu.SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;
hKeyProgID: HKEY): HResult;
var
StgMedium: TStgMedium;
FormatEtc: TFormatEtc;
FileNumber,i:Integer;
begin
file://如果lpdobj等于Nil,則本調(diào)用失敗
if (lpdobj = nil) then begin
Result := E_INVALIDARG;
Exit;
end;
file://首先初始化并清空FileList以添加文件
FileList:=TStringList.Create;
FileList.Clear;
file://初始化剪貼版格式文件
with FormatEtc do begin
cfFormat := CF_HDROP;
ptd := nil;
dwaspect := DVASPECT_CONTENT;
lindex := -1;
tymed := TYMED_HGLOBAL;
end;
Result := lpdobj.GetData(FormatEtc, StgMedium);
if Failed(Result) then Exit;
file://首先查詢用戶選中的文件的個(gè)數(shù)
FileNumber := DragQueryFile(StgMedium.hGlobal,$FFFFFFFF,nil,0);
file://循環(huán)讀取,將所有用戶選中的文件保存到FileList中
for i:=0 to FileNumber-1 do begin
DragQueryFile(StgMedium.hGlobal, i, FFileName, SizeOf(FFileName));
FileList.Add(FFileName);
Result := NOERROR;
end;
ReleaseStgMedium(StgMedium);
end;
function TContextMenu.QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst,
idCmdLast, uFlags: UINT): HResult;
begin
Result := 0;
if ((uFlags and $0000000F) = CMF_NORMAL) or
((uFlags and CMF_EXPLORE) <> 0) then begin
// 往Context Menu中加入一個(gè)菜單項(xiàng) ,菜單項(xiàng)的標(biāo)題為察看位圖文件
InsertMenu(Menu, indexMenu, MF_STRING or MF_BYPOSITION, idCmdFirst,
PChar('文件操作'));
// 返回增加菜單項(xiàng)的個(gè)數(shù)
Result := 1;
end;
end;
function TContextMenu.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;
var
frmOP:TForm1;
begin
// 首先確定該過(guò)程是被系統(tǒng)而不是被一個(gè)程序所調(diào)用
if (HiWord(Integer(lpici.lpVerb)) <> 0) then
begin
Result := E_FAIL;
Exit;
end;
// 確定傳遞的參數(shù)的有效性
if (LoWord(lpici.lpVerb) <> 0) then begin
Result := E_INVALIDARG;
Exit;
end;
file://建立文件操作窗口
frmOP:=TForm1.Create(nil);
file://將所有的文件列表添加到文件操作窗口的列表中
frmOP.ListBox1.Items := FileList;
Result := NOERROR;
end;
function TContextMenu.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;
pszName: LPSTR; cchMax: UINT): HRESULT;
begin
if (idCmd = 0) then begin
if (uType = GCS_HELPTEXT) then
{返回該菜單項(xiàng)的幫助信息,此幫助信息將在用戶把鼠標(biāo)
移動(dòng)到該菜單項(xiàng)時(shí)出現(xiàn)在狀態(tài)條上。}
StrCopy(pszName, PChar('點(diǎn)擊該菜單項(xiàng)將執(zhí)行文件操作'));
Result := NOERROR;
end
else
Result := E_INVALIDARG;
end;
type
TContextMenuFactory = class(TComObjectFactory)
public
procedure UpdateRegistry(Register: Boolean); override;
end;
procedure TContextMenuFactory.UpdateRegistry(Register: Boolean);
var
ClassID: string;
begin
if Register then begin
inherited UpdateRegistry(Register);
ClassID := GUIDToString(Class_ContextMenu);
file://當(dāng)注冊(cè)擴(kuò)展庫(kù)文件時(shí),添加庫(kù)到注冊(cè)表中
CreateRegKey('*shellex', ', ');
CreateRegKey('*shellexContextMenuHandlers', ', ');
CreateRegKey('*shellexContextMenuHandlersFileOpreation', ', ClassID);
file://如果操作系統(tǒng)為Windows NT的話
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
OpenKey('SOFTWAREMicrosoftWindowsCurrentVersionShell Extensions', True);
OpenKey('Approved', True);
WriteString(ClassID, 'Context Menu Shell Extension');
finally
Free;
end;
end
else begin
DeleteRegKey('*shellexContextMenuHandlersFileOpreation');
inherited UpdateRegistry(Register);
end;
end;
initialization
TContextMenuFactory.Create(ComServer, TContextMenu, Class_ContextMenu,
', 'Context Menu Shell Extension', ciMultiInstance,tmApartment);
end.
在OpWindow窗口中加入一個(gè)TListBox控件和兩個(gè)TButton控件,OpWindows.pas的程序清單如下:
unit opwindow;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls,shlobj,shellapi,ActiveX;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
FileList:TStringList;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
FileList:=TStringList.Create;
Button1.Caption :='復(fù)制文件';
Button2.Caption :='移動(dòng)文件';
Self.Show;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FileList.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
sPath:string;
fsTemp:SHFILEOPSTRUCT;
i:integer;
begin
sPath:=InputBox('文件操作','輸入復(fù)制路徑','c:windows');
if sPath<>'then begin
fsTemp.Wnd := Self.Handle;
file://設(shè)置文件操作類型
fsTemp.wFunc :=FO_COPY;
file://允許執(zhí)行撤消操作
fsTemp.fFlags :=FOF_ALLOWUNDO;
for i:=0 to ListBox1.Items.Count-1 do begin
file://源文件全路徑名
fsTemp.pFrom := PChar(ListBox1.Items.Strings[i]);
file://要復(fù)制到的路徑
fsTemp.pTo := PChar(sPath);
fsTemp.lpszProgressTitle:='拷貝文件';
if SHFileOperation(fsTemp)<>0 then
ShowMessage('文件復(fù)制失敗');
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
sPath:string;
fsTemp:SHFILEOPSTRUCT;
i:integer;
begin
sPath:=InputBox('文件操作','輸入移動(dòng)路徑','c:windows');
if sPath<>'then begin
fsTemp.Wnd := Self.Handle;
fsTemp.wFunc :=FO_MOVE;
fsTemp.fFlags :=FOF_ALLOWUNDO;
for i:=0 to ListBox1.Items.Count-1 do begin
fsTemp.pFrom := PChar(ListBox1.Items.Strings[i]);
fsTemp.pTo := PChar(sPath);
fsTemp.lpszProgressTitle:='移動(dòng)文件';
if SHFileOperation(fsTemp)<>0 then
ShowMessage('文件復(fù)制失敗');
end;
end;
end;
end.
點(diǎn)擊菜單的 Project | Build ContextMenu 項(xiàng),Delphi就會(huì)建立Contextmenu.dll文件,這個(gè)就是上下文相關(guān)菜單程序了。
使用,Regsvr32.exe 注冊(cè)程序,然后在Windows的Explore 中在任意的一個(gè)或者幾個(gè)文件中點(diǎn)擊鼠標(biāo)右鍵,在上下文菜單中就會(huì)
多一個(gè)文件操作的菜單項(xiàng),點(diǎn)擊該項(xiàng),在彈出窗口的列表中會(huì)列出你所選擇的所有文件的文件名,你可以選擇拷貝文件按鈕或者
移動(dòng)文件按鈕執(zhí)行文件操作。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注