找回密码
 注册
搜索
热搜: 超星 读书 找书
查看: 392|回复: 0

[【推荐】] Windows 7编程新特性Shell Library接口介绍

[复制链接]
发表于 2009-10-28 18:32:41 | 显示全部楼层 |阅读模式
目前看来很多Windows 7编程新特性都是围绕.NET平台进行的,毕竟都是微软一家的东西。

下文所用到的示例代码来源于微软一站式开发技术框架解决方案。你可以通过http://cfx.codeplex.com/Release/ProjectReleases.aspx下载到Windows7ShellLibrary相关的sample。其中包含C++、C#、VB.NET对ShellLibrary操作的示例代码:CppWin7ShellLibrary,C#Win7ShellLibrary,VBWin7ShellLibrary。

为了帮助用户更加有效地对硬盘上的文件进行管理,Windows7中引入了新的文件管理方式:库(Library)。库自然演化自以往操作系统中MyDocuments文件夹这个概念。有了库,我们就可以将多个相关的文件夹组织到同一个库下,从而更快更便捷地管理和搜索数据。

创建Windows Shell Library

Windows 7提供了SHCreateLibrary API用来创建一个Shell Library:

C++ CreateShellLibrary
/**//*!
* Create a new shell library under the user's Libraries folder. If a library  
* with the same name already exists, the new one overrides the existing one.
*  
* \\param pwszLibraryName
* The name of the shell library to be created.  
*/
BOOL CreateShellLibrary(LPWSTR pwszLibraryName)
{
  /**//////////////////////////////////////////////////////////////////////////
  // Create the shell library COM object.
  //  

  IShellLibrary* pShellLib = NULL;
  HRESULT hr = SHCreateLibrary(IID_PPV_ARGS(&pShellLib));
  if (FAILED(hr))
  {
    _tprintf(_T(\"SHCreateLibrary failed to create the shell library \"

)\\ _T( COMobjectw/err0x%08lx\\n ),hr); return FALSE; } /**/ //////////////////////////////////////////////////////////////////////// //Savethenewlibraryundertheuser'sLibrariesfolder. // IShellItem*pSa

  ) \\
      _T(\"COM object w/err 0x%08lx\\n\"), hr);
    return FALSE;
  }


  /**/////////////////////////////////////////////////////////////////////////
  // Save the new library under the user's Libraries folder.
  //  

  IShellItem* pSavedTo = NULL;
  hr = pShellLib->SaveInKnownFolder(FOLDERID_UsersLibraries,  
    pwszLibraryName, LSF_OVERRIDEEXISTING, &pSavedTo);
  if (FAILED(hr))
  {
    _tprintf(_T(\"IShellLibrary::SaveInKnownFolder failed to save the \") \\
      _T(\"library w/err 0x%08lx\\n\"), hr);
    return FALSE;
  }


  /**//////////////////////////////////////////////////////////////////////////
  // Clean up.
  //  

  if (pShellLib != NULL)
    pShellLib->Release();

if (pSavedTo!=NULL) pSavedTo-Release(); return TRUE; } /**/ ///////////////////////////////////////////////////////////////////// //Createashelllibrary. // using (ShellLibrarylibrary= new ShellLibrar

  

  if (pSavedTo != NULL)
    pSavedTo->Release();

  return TRUE;
}
  

/**//////////////////////////////////////////////////////////////////////
// Create a shell library.
//  

using (ShellLibrary library = new ShellLibrary(libraryName, true))
{
}


管理Windows Shell Library

你可以通过调用SHShowManageLibraryUI API显示出Windows标准的ShellLibrary管理对话框。值得注意的是,在调用SHShowManageLibraryUI前请确保shelllibrary没有被以可写方式打开。否则在SHShowManageLibraryUI中对shelllibrary的修改将无法被保存。

C++ ShowManageLibraryUI

C++ ShowManageLibraryUI

/**//*!

* Shows the library management dialog box of thespec ifiedlibrary, which

* enables users to manage the library folders and defaultsavelocation.

*

* \\param pwszLibraryName

* The name of the shell library

*/

BOOL ShowManageLibraryUI( LPWSTR pwszLibraryName)

{

// Get the shell item that represents the library.

IShellItem2* pShellItem=GetShellLibraryItem(pwszLibraryName);

HRESULT hr = SHShowManageLibraryUI(pShellItem, NULL,

LCppWin7ShellLibrary, LManage Library foldersandsettings,

LMD_ALLOWUNINDEXABLENETWORKLOCATIONS);

// Clean up

if (pShellItem != NULL)

pShellItem->Release();

return SUCCEEDED(hr);

}

C# ShowManageLibraryUI

// ShowManageLibraryUI requires that the library isnotcurrently

// opened with write permission.

ShellLibrary.ShowManageLibraryUI(libraryName, IntPtr.Zero,

CSWin7ShellLibrary, Manage Library folders and settings,true);

向Shell Library中添加文件夹

SHAddFolderPathToLibrary可用来向指定的Shell Library中添加文件夹。

C++ AddFolderToShellLibrary

/**//*!

* Add a folder to an existing shell library.

*

* \\param pShellLib

* The IShellLibrary interface of the shell library

*

* \\param pwszFolderPath

* The path of the folder to be added into the shell library

*

* \\param bSaveLocation

* if bSaveLocation is true, set thefolder as the save location ofthe shell

* library

*/

BOOL AddFolderToShellLibrary(IShellLibrary* pShellLib,

LPWSTR pwszFolderPath, BOOL bSaveLocation)

{

HRESULT hr =SHAddFolderPathToLibrary(pShellLib,pwszFolderPath);

if (FAILED(hr))

{

_tprintf(_T(SHAddFolderPathToLibrary failed to add a folder)\\

_T(to the shell library w/err 0x%08lx\\n), hr);

return jiacubiaoj ijiacubiaoj ifalse;

}

// Save the folder as the save location of the shell library

if (bSaveLocation)

{

// Create shell item from folder path

IShellItem2* pShellItemSaveFolder = NULL;

hr = SHCreateItemFromParsingName(pwszFolderPath, 0,

IID_PPV_ARGS(&pShellItemSaveFolder));

if (FAILED(hr))

{

_tprintf(_T(SHCreateItemFromParsingName failed w/err ) \\

_T(0x%08lx\\n), hr);

return jiacubiaoj ijiacubiaoj ifalse;

}

// Set the folder as the save location

pShellLib->SetDefaultSaveFolder(DSFT_DETECT,pShellItemSaveFolder);

if (pShellItemSaveFolder != NULL)

pShellItemSaveFolder->Release();

if (FAILED(hr))

{

_tprintf(_T(IShellLibrary::SetDefaultSaveFolder failed ) \\

_T(w/err 0x%08lx\\n), hr);

return jiacubiaoj ijiacubiaoj ifalse;

}

}

// Commit the change of the shell library

pShellLib->Commit();

return true;

}

C# AddFolderToShellLibrary

using (ShellLibrary library =ShellLibrary.Load(libraryName,jiacubiaoj ijiacubiaoj ifalse))

{

/**//////////////////////////////////////////////////////////////////

// Add a folder to the shell library.

//

// Add the folder to the shell library

library.Add(folderPath);

library.DefaultSaveFolder = folderPath;

}

枚举Shell Library中的文件夹

IShellLibrary::GetFolders可用来得到Shell Library中的文件夹。

删除一个Shell Library

Windows 7编程新特性Shell Library接口介绍就到这里吧。

http://os.51cto.com/art/200910/158624.htm

本文介绍Windows 7新增特性:库相关的编程接口和示例。Windows7中引入了新的文件管理方式:库(Library)。库自然演化自以往操作系统中MyDocuments文件夹这个概念。有了库,我们就可以将多个相关的文件夹组织到同一个库下,从而更快更便捷地管理和搜索数据。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|网上读书园地

GMT+8, 2024-6-9 22:55 , Processed in 0.360748 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表