|
|
|
VC6.0实现快捷方式中的查找功能(2) |
|
|
作者:未知 来源:未知 加入时间:2006-1-23 人气:119 |
ghShell32 = LoadLibrary(_T("Shell32.DLL"));
if (ghShell32 == NULL)
return FALSE;
gpfSHOpenFolderAndSelectItems =
(HRESULT (WINAPI*)(LPCITEMIDLIST*, UINT, LPCITEMIDLIST*, DWORD)) GetProcAddress(ghShell32, "SHOpenFolderAndSelectItems");
if (gpfSHOpenFolderAndSelectItems != NULL)
{
/// 可以获得SHOpenFolderAndSelectItems()函数的API地址
if (SUCCEEDED(gpfSHOpenFolderAndSelectItems((LPCITEMIDLIST*)pidlFolder,0,(LPCITEMIDLIST*)NULL,0)))
{
///直接调用系统的功能
FreeLibrary(ghShell32);
return TRUE;
}
FreeLibrary(ghShell32);
return FALSE;
}
FreeLibrary(ghShell32);
/// 当操作系统不支持SHOpenFolderAndSelectItems()函数的API时的处理,
/// 自已动手写一个与系统功能相同的代码
pidl = pidlFolder;
pIdlFile = pidl;
/// 找出目标文件中文件名的偏移量
while (cb = pIdlFile->
mkid.cb)
{
pidl2 = pIdlFile;
pIdlFile = (ITEMIDLIST*)((BYTE*)pIdlFile + cb);
}
cb = pidl2->
mkid.cb;
pidl2->
mkid.cb = 0;
/// 打开目标文件所在的文件夹
if (SUCCEEDED(GetShellFolderViewDual(pidl, &pIShellFolderViewDual)))
{
pidl2->
mkid.cb = cb;
// 0 Deselect the item.
// 1 Select the item.
// 3 Put the item in edit mode.
// 4 Deselect all but the specified item.
// 8 Ensure the item is displayed in the view.
// 0x10 Give the item the focus.
COleVariant bszFile(pidl2);
if(pIShellFolderViewDual != NULL)
{
/// 选中相应的选项
pIShellFolderViewDual->
SelectItem(bszFile, 0x1d);
pIShellFolderViewDual->
Release();
}
return TRUE;
}
return FALSE;
}
void FindTarget(CString str)
{
HRESULT hres;
IShellLink *psl;
ITEMIDLIST *pidl;
IPersistFile *ppf;
CoInitialize(NULL);
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
// 设置目标文件
psl->
SetPath(str);
/// 获得目标文件的ITEMIDLIST
psl->
GetIDList(&pidl);
// Get a pointer to the IPersistFile interface.
hres = psl->
QueryInterface(IID_IPersistFile, (void**)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
#ifdef _UNICODE
wcscpy(wsz, str);
#else
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, str, -1, wsz, MAX_PATH);
#endif
// Load the shortcut.
hres = ppf->
Load(wsz, STGM_READ);
if (SUCCEEDED(hres))
{
/// 获得快捷方式的ITEMIDLIST
psl->
GetIDList(&pidl);
}m
ppf->
Release();
}
/// 打开文件夹并选中项目
XZSHOpenFolderAndSelectItems(pidl);
psl->
Release();
}
CoUninitialize();
}
在VC6下编译后的代码,通过98,2k,XP的测试。
|
|
相关文章:VC6.0实现快捷方式中的查找功能(1)
相关软件:
|
|
|