我知道
CRecentFileList
构造函数可以让您指定 lpszSection
作为键。
Add
方法的最新文档,它接受文件路径并指出:
virtual void Add(LPCTSTR lpszPathName);
virtual void Add(
LPCTSTR lpszPathName,
LPCTSTR lpszAppID);
void Add(
IShellItem* pItem,
LPCTSTR lpszAppID);
void Add(
IShellLink* pLink,
LPCTSTR lpszAppID);
void Add(
PIDLIST_ABSOLUTE pidl,
LPCTSTR lpszAppID);
如您所见,它有第二个参数
lpszAppID
,文档说:
指定应用程序的应用程序用户模型 ID。
这个
lpszAppID
参数的用途是什么,因为它似乎根本没有在MFC代码中使用?例如API 允许我们使用 []
运算符获取字符串,但是 lpszAppID
有什么意义呢?
顺便说一句,为什么我们必须用
CRecentFileList
创建这些 new
对象?所有示例代码似乎都是这样做的。
代码很旧。因此为了兼容性,仍然使用指针。但这只是内部实现。用户看不到它。
最近的文件也会通过
SHAddToRecentDocs
添加到全局最近的文件列表中,并且此功能可能会使用给定的 id 来定位您的程序来打开该文件。请参阅那里的文档。
此应用程序 ID 还用于 Windows Toast,从中也可以打开文件或链接到应用程序中的操作。
查看MFC的源代码我们可以看到:
void CRecentFileList::Add(LPCTSTR lpszPathName, LPCTSTR lpszAppID)
{
CWinApp* pApp = AfxGetApp();
if (pApp == NULL || !pApp->IsWindows7())
{
Add(lpszPathName);
return;
}
CString strAppID = lpszAppID == NULL ? _T("") : lpszAppID;
ASSERT(AfxIsValidString(lpszPathName));
Add(lpszPathName);
HRESULT hr = S_OK;
CComPtr<IShellItem> psi = NULL;
#ifdef UNICODE
hr = _AfxSHCreateItemFromParsingName(lpszPathName, NULL, IID_IShellItem, reinterpret_cast<void**>(&psi));
#else
{
USES_CONVERSION;
#pragma warning(suppress: 6255) // There is no workaround for below conversion. Keep the behavior as-is.
LPOLESTR lpWPath = A2W(lpszPathName);
hr = _AfxSHCreateItemFromParsingName(lpWPath, NULL, IID_IShellItem, (LPVOID*)&psi);
}
#endif
ENSURE(SUCCEEDED(hr));
Add(psi, strAppID);
}