VC++创建快捷方式

两个用到的com接口

1、IShellLink
IShellLink接口函数:
GetArgumentsGets the command-line arguments associated with a Shell link object.
GetDescriptionGets the description string for a Shell link object.
GetHotkeyGets the hot key for a Shell link object.
GetIconLocationGets the location (path and index) of the icon for a Shell link object.
GetIDListGets the list of item identifiers for a Shell link object.
GetPathGets the path and file name of a Shell link object.
GetShowCmdGets the show command for a Shell link object.(SW_SHOWNORMAL、SW_SHOWMAXIMIZED、SW_SHOWMINNOACTIVE)
GetWorkingDirectoryGets the name of the working directory for a Shell link object.
ResolveAttempts to find the target of a Shell link, even if it has been moved or renamed.
SetArgumentsSets the command-line arguments for a Shell link object.
SetDescriptionSets the description for a Shell link object. The description can be any application-defined string.
SetHotkeySets a hot key for a Shell link object.
SetIconLocationSets the location (path and index) of the icon for a Shell link object.
SetIDListSets the pointer to an item identifier list (PIDL) for a Shell link object.
SetPathSets the path and file name of a Shell link object.
SetRelativePathSets the relative path to the Shell link object.
SetShowCmdSets the show command for a Shell link object. The show command sets the initial show state of the window.
SetWorkingDirectorySets the name of the working directory for a Shell link object.
注:
HRESULT SetHotkey(WORD  wHotkey );
这个WORD值,低位上是 virtual key code ,高位上可以是ALT,CTRL等组合键。可以用MAKEWORD(low, high)来生成一个WORD,如Ctrl+F12可以用MAKEWORD(VK_F12, HOTKEYF_CONTROL)表示。

2、IPersistFile
IPersistFile接口函数:
IsDirtyChecks an object for changes since it was last saved to its current file.
LoadOpens the specified file and initializes an object from the file contents.
SaveSaves the object into the specified file.
SaveCompletedNotifies the object that it can revert from NoScribble mode to Normal mode.
GetCurFileGets the current name of the file associated with the object.

示例:
/*
	函数功能:对指定文件在指定的目录下创建其快捷方式
	函数参数:
	lpszFileName    指定文件,为NULL表示当前进程的EXE文件。(全路径)
	lpszLnkFileDir  指定目录,不能为NULL。
	lpszLnkFileName 快捷方式名称,为NULL表示EXE文件名。(仅文件名)
lpszWorkDir  工作目录
	wHotkey         为0表示不设置快捷键
	pszDescription  备注
	iShowCmd        运行方式,默认为常规窗口
lpszIconPath  图标路径
lpszParam  命令行参数
*/
BOOL CreateFileShortcut(LPCTSTR lpszFileName, LPCTSTR lpszLnkFileDir, LPCTSTR lpszLnkFileName,  LPCTSTR lpszWorkDir, WORD wHotkey, LPCTSTR lpszDescription, int iShowCmd,  LPCTSTR lpszIconPath, LPCTSTR lpszParam)
{
if (lpszLnkFileDir == NULL)
		return FALSE;
	HRESULT hr;
	IShellLink     *pLink;  //IShellLink对象指针
	IPersistFile   *ppf; //IPersisFil对象指针

	::CoInitialize(NULL);

	//创建IShellLink对象
	hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pLink);
	if (FAILED(hr))
		return FALSE;

	//从IShellLink对象中获取IPersistFile接口
	hr = pLink->QueryInterface(IID_IPersistFile, (void**)&ppf);
	if (FAILED(hr))
	{
		pLink->Release();
		return FALSE;
	}

	TCHAR szFile[MAX_PATH] = {0};
	GetModuleFileName(NULL, szFile, ARRAYSIZE(szFile));

	//目标
	if (lpszFileName == NULL)
	{		
		pLink->SetPath(szFile);
	}
	else
		pLink->SetPath(lpszFileName);

	//工作目录
	if (lpszWorkDir != NULL)
		pLink->SetPath(lpszWorkDir);

	//快捷键
	if (wHotkey != 0)
		pLink->SetHotkey(wHotkey);

	//备注
	if (lpszDescription != NULL)
		pLink->SetDescription(lpszDescription);

	//显示方式
	pLink->SetShowCmd(iShowCmd);

	/设置图标
	if(lpszIconPath && lpszIconPath[0])
		pLink->SetIconLocation(lpszIconPath, 0);

	//设置参数
	if(lpszParam)
		pLink->SetArguments(lpszParam);

	//快捷方式的路径 + 名称
	TCHAR szBuffer[MAX_PATH];
	if (lpszLnkFileName != NULL) //指定了快捷方式的名称
		_sntprintf_s(szBuffer, ARRAYSIZE(szBuffer), _TRUNCATE, _T("%s\\%s"), lpszLnkFileDir, lpszLnkFileName);
	else  
	{
		//没有指定名称,就从取指定文件的文件名作为快捷方式名称。
		TCHAR szPEFileName[MAX_PATH] = {0};
		if (lpszFileName != NULL)			
			_tcsncpy_s(szPEFileName, ARRAYSIZE(szPEFileName), lpszFileName, _TRUNCATE);
		else
			_tcsncpy_s(szPEFileName, ARRAYSIZE(szPEFileName), szFile, _TRUNCATE);

		TCHAR* szFileNameEnd = NULL;
		szFileNameEnd = PathFindFileName(szPEFileName);

		if (szFileNameEnd == NULL)
		{    
			ppf->Release();
			pLink->Release();
			return FALSE;
		}
		//注意后缀名要从.exe改为.lnk
		_sntprintf_s(szBuffer, ARRAYSIZE(szBuffer), _TRUNCATE,_T("%s\\%s"), lpszLnkFileDir, szFileNameEnd);
		int nLen = _tcslen(szBuffer);
		szBuffer[nLen - 3] = 'l';
		szBuffer[nLen - 2] = 'n';
		szBuffer[nLen - 1] = 'k';
	}
	//保存快捷方式到指定目录下
	hr = ppf->Save(szBuffer, TRUE);

	ppf->Release();
	pLink->Release();

	CoUninitialize();

	return SUCCEEDED(hr);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值