Welcome

首页 / 软件开发 / .NET编程技术 / Windows 7开发:Shell 库 - 本机(动手实验)(下)

Windows 7开发:Shell 库 - 本机(动手实验)(下)2011-05-06 MSDN 2.编译并测试:

a.打开一个命令行窗口,并且将目录(cd)切换到SLUtil.exe所在的位置。

b.打开紧邻命令行窗口的库Shell文件夹,你将会看到你使用SLUtil工具所做的变 化.

c.在命令行窗口中,测试SLUtil命令。

d.试着为你的MyLib库使用一个与Pictures库相同的图标。

e.试着将你的库在文件管理面板中锁定和解锁。

f.改变库的默认存储位置,将这个位置移到文件系统中另外的位置,并且测试默认的存 储位置是否解决了这个变化。

任务 6 –添加FolderType 命令

每个库都有一个文件夹模板类型。这个类型定义了Shell库窗口的样子和感觉,例如,图 片和音乐就有不同的模板。IShellLibrary SetFolderType()方法则使用GUID的方式来表示 文件夹的类型id。GetFolderType()方法则能返回当前文件夹的id。默认情况,用户创建的 库,都是一般文件夹类型。

为了让用户使用文本类型而不是使用GUID,我们需要使用一个帮助类。 CfolderTypeIdNameConverter这个类知道如何将Pictures, Music, Documents, Videos, 和 Generic转换成相应的FOLDERTYPEID_*,同样,反之亦然。同时,这个方法还知道取名称的 一部分,比如vid 就是 Videos。这些都将使设置库文件夹的类型变得简单:SLUtil FolderType MyLib doc

1.向项目中添加FolderTypeIdNameConverter.h 和 FolderTypeIdNameConverter.cpp文 件。你可以从Starter文件夹中找到它们。

2.添加 #include "FolderTypeIdNameConverter.h"

3.添加下面的代码片段,来实现FolderType命令:

C++ (SLUtil.cpp)

//Set or get the library"s folder template
void FolderType(const CCommand &command, const vector<PCWSTR> &arguments)
{
IShellLibrary *shellLibrary = NULL;
HRESULT hr = S_OK;
FOLDERTYPEID folderTypeId;

CFolderTypeIdNameConverter converter;

//Show current folder type
if (arguments.size() == 1)
{
//Open library with read permissions
IShellLibrary *shellLibrary =
OpenLibrary(L"FolderType", arguments[0]);

hr = shellLibrary->GetFolderType(&folderTypeId);
if (FAILED (hr))
{
wcerr <<
L"FolderType: Can"t get the library"s folder template."
<< endl;
exit(5);
}
wcout << L"Library " << arguments[0] << L": Folder template is: " << converter.GetFolderTypeIdName(folderTypeId) << endl;
}
else //Set the current folder type
{
//Open library with read/write permissions
IShellLibrary *shellLibrary =
OpenLibrary(L"FolderType", arguments[0], false);

hr = converter.GetFolderTypeIdFromName(arguments[1],
&folderTypeId);
if (FAILED (hr))
{
wcerr << L"FolderType: Invalida folder template name." <<
endl;
exit(6);
}

hr = shellLibrary->SetFolderType(folderTypeId);
if (FAILED (hr))
{
wcerr <<
L"FolderType: Can"t set the library"s folder template,"
<< endl;
exit(7);
}
//Commit the library changes
shellLibrary ->Commit();
}
if (shellLibrary != NULL)
shellLibrary ->Release();
}
COMMAND(L"FolderType",
L"SLUtil FolderType LibraryName [Documents|Pictures|Music|Videos|Generic]",
L"Set or get the library"s folder template", L"SLUtil MyLib Documents", 1,
FolderType);