丧尸围城3几个人联机:VC中如何获取桌面快捷方式图标并对其操作

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/01 03:51:39
比如说,我桌面上有个"千千静听"的快捷方式,我想写一个程序,通过程序打开千千静听,问题有二:
1.快捷方式在VC中是以什么来处理的,HICON吗?
2.如果获取了快捷方式,如何向其发送消息?
望高手能帮助解答,最好说出原理并给出主要代码.
附:程序不能实现我是不会给分的,如果实现了追加五十分.

快捷方式 属于 平台SDK 中 Windows Shell 的内容。用到了COM。相当麻烦。
我替你查了一下MSDN。

首先因为用到了COM,
你要在你程序启动的时候 调用 CoInitilize(NULL);,在程序退出时调用CoUninitialize();

然后是准备操作,找到你要的快捷方式的路径。用FindFirstFile 和 FindNextFile 找。如果是MFC,可以用CFileFind比较方便。当然,你也可以在代码中给出快捷方式的路径,但这样会使程序不通用。

需要的头文件:<objidl.h>
需要链接的IDL文件: <shobjidl.idl>
之后是查找快捷方式的目标。MSDN 里有这段代码。就用MS 的吧。是一个函数的代码。我给你粘在这个帖子的最后面。你可以把它拷进你代码里。

最后当然是运行.exe 文件了。 用CreateProcess 或 WinExec 都行。

那个函数的代码:

HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath, int iPathBufferSize)
{
HRESULT hres;
IShellLink* psl;
char szGotPath[MAX_PATH];
char szDescription[MAX_PATH];
WIN32_FIND_DATA wfd;

*lpszPath = 0; // assume failure

// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;

// Get a pointer to the IPersistFile interface.
hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);

if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];

// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH);

// TODO: Check return value from MultiByteWideChar to ensure
success.

// Load the shortcut.
hres = ppf->Load(wsz, STGM_READ);

if (SUCCEEDED(hres))
{
// Resolve the link.
hres = psl->Resolve(hwnd, 0);

if (SUCCEEDED(hres))
{
// Get the path to the link target.
hres = psl->GetPath(szGotPath,
MAX_PATH,
(WIN32_FIND_DATA*)&wfd,
SLGP_SHORTPATH);

if (SUCCEEDED(hres))
{
// Get the description of the target.
hres = psl->GetDescription(szDescription, MAX_PATH);

if (SUCCEEDED(hres))
{
hres = StringCbCopy(lpszPath, iPathBufferSize,
szGotPath);
if (SUCCEEDED(hres))
{
// Handle success
}
else
// application-defined function
HandleErr(hres);
}
}
}
}

// Release the pointer to the IPersistFile interface.
ppf->Release();
}

// Release the pointer to the IShellLink interface.
psl->Release();
}
return hres;
}