📄 oledlgs1.cpp
字号:
////////////////////////////////////////////////////////////////////////////
// InsertObject dialog wrapper
COleInsertDialog::COleInsertDialog(DWORD dwFlags, CWnd* pParentWnd)
: COleDialog(pParentWnd)
{
memset(&m_io, 0, sizeof(m_io)); // initialize structure to 0/NULL
// fill in common part
m_io.cbStruct = sizeof(m_io);
m_io.dwFlags = dwFlags;
if (!afxData.bWin4 && AfxHelpEnabled())
m_io.dwFlags |= IOF_SHOWHELP;
if (_AfxOlePropertiesEnabled())
m_io.dwFlags |= IOF_HIDECHANGEICON;
m_io.lpfnHook = AfxOleHookProc;
m_nIDHelp = AFX_IDD_INSERTOBJECT;
// specific to this dialog
m_io.lpszFile = m_szFileName;
m_io.cchFile = _countof(m_szFileName);
m_szFileName[0] = '\0';
}
COleInsertDialog::~COleInsertDialog()
{
_AfxDeleteMetafilePict(m_io.hMetaPict);
}
void COleInsertDialog::AddClassIDToList(LPCLSID& lpList,
int& nListCount, int& nBufferLen, LPCLSID lpNewID)
{
// if the list doesn't exist, create it
if (lpList == NULL)
{
nListCount = 0;
nBufferLen = 16;
lpList = new CLSID[nBufferLen];
}
// if the list isn't big enough grow it by twice
if (nListCount == nBufferLen)
{
LPCLSID lpOldList = lpList;
nBufferLen <<= 2;
lpList = new CLSID[nBufferLen];
memcpy(lpList, lpOldList, sizeof(CLSID) * nListCount);
delete [] lpOldList;
}
lpList[nListCount] = *lpNewID;
nListCount++;
return;
}
int COleInsertDialog::DoModal(DWORD dwFlags)
{
LPCLSID lpNewExcludeList = NULL;
int nNewExcludeCount = 0;
int nNewExcludeLen = 0;
// support for controls is built-in
// support doc objects by filtering what the dialog shows
if (dwFlags == ControlsOnly)
m_io.dwFlags |= IOF_SELECTCREATECONTROL | IOF_SHOWINSERTCONTROL;
else if (dwFlags == DocObjectsOnly)
{
m_io.dwFlags |= IOF_DISABLEDISPLAYASICON | IOF_DISABLELINK;
HKEY hkClassesRoot;
HKEY hkCLSID;
HKEY hkItem;
HKEY hkDocObject;
DWORD dwIndex = 0;
TCHAR szName[MAX_PATH+1];
if (RegOpenKeyEx(HKEY_CLASSES_ROOT, NULL, 0, KEY_READ,
&hkClassesRoot) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hkClassesRoot, _T("CLSID"), 0,
KEY_ENUMERATE_SUB_KEYS, &hkCLSID) == ERROR_SUCCESS)
{
while(RegEnumKey(hkCLSID, dwIndex++,
szName, sizeof(szName)) == ERROR_SUCCESS)
{
if (RegOpenKeyEx(hkCLSID, szName, 0,
KEY_READ, &hkItem) == ERROR_SUCCESS)
{
if ((RegOpenKeyEx(hkItem, _T("Insertable"), 0,
KEY_READ, &hkDocObject) == ERROR_SUCCESS) ||
(RegOpenKeyEx(hkItem, _T("Ole1Class"),0,
KEY_READ, &hkDocObject) == ERROR_SUCCESS))
{
RegCloseKey(hkDocObject);
if ((RegOpenKeyEx(hkItem, _T("DocObject"), 0,
KEY_READ, &hkDocObject) != ERROR_SUCCESS ) )
{
USES_CONVERSION;
CLSID clsid;
CLSIDFromString(T2OLE(szName), &clsid);
AddClassIDToList(lpNewExcludeList,
nNewExcludeCount, nNewExcludeLen, &clsid);
RegCloseKey(hkItem);
}
RegCloseKey(hkDocObject);
}
}
}
RegCloseKey(hkCLSID);
}
}
RegCloseKey(hkClassesRoot);
}
UINT cOldClsidExclude = m_io.cClsidExclude;
LPCLSID lpOldClsidExclude = m_io.lpClsidExclude;
if (lpNewExcludeList != NULL)
{
m_io.lpClsidExclude = lpNewExcludeList;
m_io.cClsidExclude = nNewExcludeCount;
}
QueryAgain:
int nDisposition = DoModal();
if (nDisposition == IDOK && GetSelectionType() == insertFromFile &&
dwFlags == DocObjectsOnly)
{
USES_CONVERSION;
// Double-check that the requested file really is serviced by a
// DocObject server.
CLSID clsidFile;
BOOL bIsDocObject = FALSE;
if (GetClassFile(T2COLE(m_io.lpszFile), &clsidFile) == S_OK)
{
CString strKey;
CString strCLSID = AfxStringFromCLSID(clsidFile);
strKey.Format(_T("CLSID\\%s\\DocObject"), strCLSID);
HKEY hKey;
if (ERROR_SUCCESS ==
RegOpenKeyEx(HKEY_CLASSES_ROOT, strKey, 0, KEY_QUERY_VALUE, &hKey))
{
RegCloseKey(hKey);
bIsDocObject = TRUE;
}
}
if (!bIsDocObject)
{
nDisposition =
AfxMessageBox(AFX_IDS_NOT_DOCOBJECT, MB_RETRYCANCEL | MB_ICONHAND);
if (nDisposition == IDRETRY)
goto QueryAgain;
else
nDisposition = IDCANCEL;
}
}
delete [] lpNewExcludeList;
m_io.cClsidExclude = cOldClsidExclude;
m_io.lpClsidExclude = lpOldClsidExclude;
return nDisposition;
}
int COleInsertDialog::DoModal()
{
ASSERT_VALID(this);
ASSERT(m_io.lpfnHook != NULL); // can still be a user hook
m_io.hWndOwner = PreModal();
int iResult = MapResult(::OleUIInsertObject(&m_io));
PostModal();
return iResult;
}
UINT COleInsertDialog::GetSelectionType() const
{
ASSERT_VALID(this);
if (m_io.dwFlags & IOF_SELECTCREATEFROMFILE)
{
if (m_io.dwFlags & IOF_CHECKLINK)
return linkToFile;
else
return insertFromFile;
}
ASSERT(m_io.dwFlags & IOF_SELECTCREATENEW);
return createNewItem;
}
// allocate an item first, then call this fuction to create it
BOOL COleInsertDialog::CreateItem(COleClientItem* pNewItem)
{
ASSERT_VALID(pNewItem);
// switch on selection type
UINT selType = GetSelectionType();
BOOL bResult;
switch (selType)
{
case linkToFile:
// link to file selected
ASSERT(m_szFileName[0] != 0);
bResult = pNewItem->CreateLinkFromFile(m_szFileName);
break;
case insertFromFile:
// insert file selected
ASSERT(m_szFileName[0] != 0);
bResult = pNewItem->CreateFromFile(m_szFileName);
break;
default:
// otherwise must be create new
ASSERT(selType == createNewItem);
bResult = pNewItem->CreateNewItem(m_io.clsid);
break;
}
// deal with Display As Iconic option
if (bResult && GetDrawAspect() == DVASPECT_ICON)
{
// setup iconic cache (it will draw iconic by default as well)
if (!pNewItem->SetIconicMetafile(m_io.hMetaPict))
{
TRACE0("Warning: failed to set iconic aspect in CreateItem.\n");
return TRUE;
}
// since picture was set OK, draw as iconic as well...
pNewItem->SetDrawAspect(DVASPECT_ICON);
}
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
// COleInsertDialog diagnostics
#ifdef _DEBUG
void COleInsertDialog::Dump(CDumpContext& dc) const
{
COleDialog::Dump(dc);
dc << "m_szFileName = " << m_szFileName;
dc << "\nm_io.cbStruct = " << m_io.cbStruct;
dc << "\nm_io.dwFlags = " << (LPVOID)m_io.dwFlags;
dc << "\nm_io.hWndOwner = " << (UINT)m_io.hWndOwner;
dc << "\nm_io.lpszCaption = " << m_io.lpszCaption;
dc << "\nm_io.lCustData = " << (LPVOID)m_io.lCustData;
dc << "\nm_io.hInstance = " << (UINT)m_io.hInstance;
dc << "\nm_io.lpszTemplate = " << (LPVOID)m_io.lpszTemplate;
dc << "\nm_io.hResource = " << (UINT)m_io.hResource;
if (m_io.lpfnHook == AfxOleHookProc)
dc << "\nhook function set to standard MFC hook function";
else
dc << "\nhook function set to non-standard hook function";
dc << "\nm_io.hMetaPict = " << (UINT)m_io.hMetaPict;
dc << "\n";
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COleConvertDialog
COleConvertDialog::COleConvertDialog(COleClientItem* pItem, DWORD dwFlags,
CLSID* pClassID, CWnd* pParentWnd) : COleDialog(pParentWnd)
{
if (pItem != NULL)
ASSERT_VALID(pItem);
ASSERT(pClassID == NULL || AfxIsValidAddress(pClassID, sizeof(CLSID), FALSE));
memset(&m_cv, 0, sizeof(m_cv)); // initialize structure to 0/NULL
if (pClassID != NULL)
m_cv.clsid = *pClassID;
// fill in common part
m_cv.cbStruct = sizeof(m_cv);
m_cv.dwFlags = dwFlags;
if (!afxData.bWin4 && AfxHelpEnabled())
m_cv.dwFlags |= CF_SHOWHELPBUTTON;
m_cv.lpfnHook = AfxOleHookProc;
m_nIDHelp = AFX_IDD_CONVERT;
// specific to this dialog
m_cv.fIsLinkedObject = pItem->GetType() == OT_LINK;
m_cv.dvAspect = pItem->GetDrawAspect();
if (pClassID == NULL && !m_cv.fIsLinkedObject)
{
// for embeddings, attempt to get class ID from the storage
if (ReadClassStg(pItem->m_lpStorage, &m_cv.clsid) == S_OK)
pClassID = &m_cv.clsid;
// attempt to get user type from storage
CLIPFORMAT cf = 0;
LPOLESTR lpOleStr = NULL;
ReadFmtUserTypeStg(pItem->m_lpStorage, &cf, &lpOleStr);
m_cv.lpszUserType = TASKSTRINGOLE2T(lpOleStr);
m_cv.wFormat = (WORD)cf;
}
// get class id if neded
if (pClassID == NULL)
{
// no class ID in the storage, use class ID of the object
pItem->GetClassID(&m_cv.clsid);
}
// get user type if needed
if (m_cv.lpszUserType == NULL)
{
// no user type in storge, get user type from class ID
LPTSTR lpszUserType = NULL;
LPOLESTR lpOleStr = NULL;
if (OleRegGetUserType(m_cv.clsid, USERCLASSTYPE_FULL,
&lpOleStr) == S_OK)
{
lpszUserType = TASKSTRINGOLE2T(lpOleStr);
}
else
{
lpszUserType = (LPTSTR)CoTaskMemAlloc(256 * sizeof(TCHAR));
if (lpszUserType != NULL)
{
lpszUserType[0] = '?';
lpszUserType[1] = 0;
VERIFY(AfxLoadString(AFX_IDS_UNKNOWNTYPE, lpszUserType) != 0);
}
}
m_cv.lpszUserType = lpszUserType;
}
m_cv.hMetaPict = pItem->GetIconicMetafile();
}
COleConvertDialog::~COleConvertDialog()
{
_AfxDeleteMetafilePict(m_cv.hMetaPict);
}
int COleConvertDialog::DoModal()
{
ASSERT_VALID(this);
ASSERT(m_cv.lpfnHook != NULL); // can still be a user hook
m_cv.hWndOwner = PreModal();
int iResult = MapResult(::OleUIConvert(&m_cv));
PostModal();
return iResult;
}
BOOL COleConvertDialog::DoConvert(COleClientItem* pItem)
{
ASSERT_VALID(pItem);
CWaitCursor wait;
UINT selType = GetSelectionType();
BOOL bResult = TRUE;
if (m_cv.clsidNew != CLSID_NULL)
{
switch (selType)
{
case convertItem:
bResult = pItem->ConvertTo(m_cv.clsidNew);
break;
case activateAs:
bResult = pItem->ActivateAs(m_cv.lpszUserType, m_cv.clsid,
m_cv.clsidNew);
break;
default:
ASSERT(selType == noConversion);
break;
}
}
if (!bResult)
{
// if unable to convert the object show message box
AfxMessageBox(AFX_IDP_FAILED_TO_CONVERT);
return FALSE;
}
// change to iconic/content view if changed
if ((DVASPECT)m_cv.dvAspect != pItem->GetDrawAspect())
{
pItem->OnChange(OLE_CHANGED_ASPECT, (DWORD)m_cv.dvAspect);
pItem->SetDrawAspect((DVASPECT)m_cv.dvAspect);
}
// change the actual icon as well
if (m_cv.fObjectsIconChanged)
{
pItem->SetIconicMetafile(m_cv.hMetaPict);
if (pItem->GetDrawAspect() == DVASPECT_ICON)
pItem->OnChange(OLE_CHANGED, (DWORD)DVASPECT_ICON);
}
return TRUE;
}
UINT COleConvertDialog::GetSelectionType() const
{
ASSERT_VALID(this);
if (m_cv.clsid != m_cv.clsidNew)
{
if (m_cv.dwFlags & CF_SELECTCONVERTTO)
return convertItem;
else if (m_cv.dwFlags & CF_SELECTACTIVATEAS)
return activateAs;
}
return noConversion;
}
/////////////////////////////////////////////////////////////////////////////
// COleConvertDialog diagnostics
#ifdef _DEBUG
void COleConvertDialog::Dump(CDumpContext& dc) const
{
COleDialog::Dump(dc);
dc << "m_cv.cbStruct = " << m_cv.cbStruct;
dc << "\nm_cv.dwFlags = " << (LPVOID)m_cv.dwFlags;
dc << "\nm_cv.hWndOwner = " << (UINT)m_cv.hWndOwner;
dc << "\nm_cv.lpszCaption = " << m_cv.lpszCaption;
dc << "\nm_cv.lCustData = " << (LPVOID)m_cv.lCustData;
dc << "\nm_cv.hInstance = " << (UINT)m_cv.hInstance;
dc << "\nm_cv.lpszTemplate = " << (LPVOID)m_cv.lpszTemplate;
dc << "\nm_cv.hResource = " << (UINT)m_cv.hResource;
if (m_cv.lpfnHook == AfxOleHookProc)
dc << "\nhook function set to standard MFC hook function";
else
dc << "\nhook function set to non-standard hook function";
dc << "\nm_cv.dvAspect = " << (UINT)m_cv.dvAspect;
dc << "\nm_cv.wFormat = " << (UINT)m_cv.wFormat;
dc << "\nm_cv.fIsLinkedObject = " << m_cv.fIsLinkedObject;
dc << "\nm_cv.hMetaPict = " << (UINT)m_cv.hMetaPict;
dc << "\nm_cv.lpszUserType = " << m_cv.lpszUserType;
dc << "\nm_cv.fObjectsIconChanged = " << m_cv.fObjectsIconChanged;
dc << "\n";
}
#endif
/////////////////////////////////////////////////////////////////////////////
// COleChangeIconDialog
COleChangeIconDialog::COleChangeIconDialog(COleClientItem* pItem,
DWORD dwFlags, CWnd* pParentWnd) : COleDialog(pParentWnd)
{
if (pItem != NULL)
ASSERT_VALID(pItem);
memset(&m_ci, 0, sizeof(m_ci)); // initialize structure to 0/NULL
// fill in common part
m_ci.cbStruct = sizeof(m_ci);
m_ci.dwFlags = dwFlags;
if (!afxData.bWin4 && AfxHelpEnabled())
m_ci.dwFlags |= CIF_SHOWHELP;
m_ci.lpfnHook = AfxOleHookProc;
m_nIDHelp = AFX_IDD_CHANGEICON;
// specific to this dialog
if (pItem != NULL)
{
pItem->GetClassID(&m_ci.clsid);
m_ci.hMetaPict = pItem->GetIconicMetafile();
}
}
COleChangeIconDialog::~COleChangeIconDialog()
{
_AfxDeleteMetafilePict(m_ci.hMetaPict);
}
int COleChangeIconDialog::DoModal()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -