MFC的OLE对象操作文档示例.docx
《MFC的OLE对象操作文档示例.docx》由会员分享,可在线阅读,更多相关《MFC的OLE对象操作文档示例.docx(86页珍藏版)》请在冰豆网上搜索。
MFC的OLE对象操作文档示例
///code/frame.cpp
//frame.cpp:
implementationoftheCMainFrameclass
//
//ThisisapartoftheMicrosoftFoundationClassesC++library.
//Copyright(C)1992-1998MicrosoftCorporation
//Allrightsreserved.
//
//Thissourcecodeisonlyintendedasasupplementtothe
//MicrosoftFoundationClassesReferenceandrelated
//electronicdocumentationprovidedwiththelibrary.
//Seethesesourcesfordetailedinformationregardingthe
//MicrosoftFoundationClassesproduct.
#include"stdafx.h"
#include"oclient.h"
#include"frame.h"
#ifdef_DEBUG
#undefTHIS_FILE
staticcharBASED_CODETHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame,CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame,CMDIFrameWnd)
//_{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_WINDOW_TILE_HORZ,OnWindowTileHorz)
ON_WM_PALETTECHANGED()
ON_WM_QUERYNEWPALETTE()
//_}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//arraysofIDsusedtoinitializecontrolbars
//toolbarbuttons-IDsarecommandbuttons
staticUINTBASED_CODEbuttons[]=
{
//sameorderasinthebitmap'toolbar.bmp'
ID_FILE_NEW,
ID_FILE_OPEN,
ID_FILE_SAVE,
ID_SEPARATOR,
ID_EDIT_CUT,
ID_EDIT_COPY,
ID_EDIT_PASTE,
ID_SEPARATOR,
ID_FILE_PRINT,
ID_APP_ABOUT,
ID_CONTEXT_HELP,
};
staticUINTBASED_CODEindicators[]=
{
ID_SEPARATOR,//statuslineindicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
//CMainFrameconstruction/destruction
CMainFrame:
:
CMainFrame()
{
}
intCMainFrame:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)
{
if(CMDIFrameWnd:
:
OnCreate(lpCreateStruct)==-1)
return-1;
if(!
m_wndToolBar.Create(this)||
!
m_wndToolBar.LoadBitmap(IDR_MAINFRAME)||
!
m_wndToolBar.SetButtons(buttons,
sizeof(buttons)/sizeof(UINT)))
{
TRACE0("Failedtocreatetoolbar\n");
return-1;//failtocreate
}
if(!
m_wndStatusBar.Create(this)||
!
m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failedtocreatestatusbar\n");
return-1;//failtocreate
}
EnableDocking(CBRS_ALIGN_ANY);
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
CDC*pDC=GetDC();
m_palette.CreateHalftonePalette(pDC);
ReleaseDC(pDC);
return0;
}
CMainFrame:
:
~CMainFrame()
{
}
/////////////////////////////////////////////////////////////////////////////
//CMainFramediagnostics
#ifdef_DEBUG
voidCMainFrame:
:
AssertValid()const
{
CMDIFrameWnd:
:
AssertValid();
}
voidCMainFrame:
:
Dump(CDumpContext&dc)const
{
CMDIFrameWnd:
:
Dump(dc);
}
#endif//_DEBUG
/////////////////////////////////////////////////////////////////////////////
//CMainFramecommands
voidCMainFrame:
:
OnWindowTileHorz()
{
if(GetKeyState(VK_SHIFT)<0)
OnMDIWindowCmd(ID_WINDOW_TILE_VERT);
else
OnMDIWindowCmd(ID_WINDOW_TILE_HORZ);
}
/////////////////////////////////////////////////////////////////////////////
//CMainFramemessagehandlers
voidCMainFrame:
:
OnPaletteChanged(CWnd*pFocusWnd)
{
CMDIFrameWnd:
:
OnPaletteChanged(pFocusWnd);
if(pFocusWnd!
=this)
SelectPalette(TRUE);
}
BOOLCMainFrame:
:
OnQueryNewPalette()
{
if(CMDIFrameWnd:
:
OnQueryNewPalette())
returnTRUE;
returnSelectPalette(FALSE);
}
BOOLCMainFrame:
:
SelectPalette(BOOLbBackground)
{
if(m_palette.m_hObject==NULL)
returnFALSE;
CDC*pDC=GetDC();
CPalette*pOldPal=pDC->SelectPalette(&m_palette,bBackground);
UINTnPalChg=pDC->RealizePalette();
pDC->SelectPalette(pOldPal,TRUE);//background
ReleaseDC(pDC);
if(nPalChg>0)
InvalidateRect(NULL,TRUE);
returnTRUE;
}
///code/frame.h
//frame.h:
interfaceoftheCMainFrameclass
//
//ThisisapartoftheMicrosoftFoundationClassesC++library.
//Copyright(C)1992-1998MicrosoftCorporation
//Allrightsreserved.
//
//Thissourcecodeisonlyintendedasasupplementtothe
//MicrosoftFoundationClassesReferenceandrelated
//electronicdocumentationprovidedwiththelibrary.
//Seethesesourcesfordetailedinformationregardingthe
//MicrosoftFoundationClassesproduct.
classCMainFrame:
publicCMDIFrameWnd
{
DECLARE_DYNAMIC(CMainFrame)
public:
CMainFrame();
//Attributes
public:
CPalettem_palette;
//Operations
public:
BOOLSelectPalette(BOOLbBackground);
//Implementation
public:
virtual~CMainFrame();
#ifdef_DEBUG
virtualvoidAssertValid()const;
virtualvoidDump(CDumpContext&dc)const;
#endif
//Controlbarembeddedmembers
protected:
CStatusBarm_wndStatusBar;
CToolBarm_wndToolBar;
//Generatedmessagemapfunctions
protected:
//_{{AFX_MSG(CMainFrame)
afx_msgintOnCreate(LPCREATESTRUCTlpCreateStruct);
afx_msgvoidOnWindowTileHorz();
afx_msgvoidOnPaletteChanged(CWnd*pFocusWnd);
afx_msgBOOLOnQueryNewPalette();
//_}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
///code/maindoc.cpp
//maindoc.cpp:
implementationoftheCMainDocclass
//
//ThisisapartoftheMicrosoftFoundationClassesC++library.
//Copyright(C)1992-1998MicrosoftCorporation
//Allrightsreserved.
//
//Thissourcecodeisonlyintendedasasupplementtothe
//MicrosoftFoundationClassesReferenceandrelated
//electronicdocumentationprovidedwiththelibrary.
//Seethesesourcesfordetailedinformationregardingthe
//MicrosoftFoundationClassesproduct.
#include"stdafx.h"
#include"oclient.h"
#include"maindoc.h"
#include"rectitem.h"
#ifdef_DEBUG
#undefTHIS_FILE
staticcharBASED_CODETHIS_FILE[]=__FILE__;
#endif
//privateclipboardformat
CLIPFORMATCMainDoc:
:
m_cfPrivate=NULL;
/////////////////////////////////////////////////////////////////////////////
//CMainDoc
IMPLEMENT_DYNCREATE(CMainDoc,COleLinkingDoc)
BEGIN_MESSAGE_MAP(CMainDoc,COleLinkingDoc)
//_{{AFX_MSG_MAP(CMainDoc)
ON_COMMAND(ID_EDIT_CLEAR_ALL,OnEditClearAll)
ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL,OnUpdateEditClearAll)
//_}}AFX_MSG_MAP
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE,OnUpdatePasteMenu)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK,OnUpdatePasteLinkMenu)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS,OnUpdateEditLinksMenu)
ON_COMMAND(ID_OLE_EDIT_LINKS,OnEditLinks)
ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST,OnUpdateObjectVerbMenu)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CHANGE_ICON,OnUpdateEditChangeIcon)
ON_COMMAND(ID_OLE_EDIT_CHANGE_ICON,OnEditChangeIcon)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMainDocconstruction/destruction
CMainDoc:
:
CMainDoc():
m_sizeDoc(800,1050)//documentsizeis8x10.5
{
EnableCompoundFile();
m_bNeedUpdate=TRUE;
if(m_cfPrivate==NULL)
m_cfPrivate=(CLIPFORMAT)
:
:
RegisterClipboardFormat(_T("MFCOClientSample"));
}
CMainDoc:
:
~CMainDoc()
{
}
CSize&CMainDoc:
:
GetDocumentSize()
{
returnm_sizeDoc;
}
voidCMainDoc:
:
OnShowViews(BOOLbVisible)
{
COleLinkingDoc:
:
OnShowViews(bVisible);
if(bVisible&&m_bNeedUpdate)
{
//updateembeddedlinksinthisdocumentbeforeshowingthewindow
COleUpdateDialogdlg(this);
dlg.DoModal();
m_bNeedUpdate=FALSE;
}
}
/////////////////////////////////////////////////////////////////////////////
//CMainDocitemmanagement
CRectItem*CMainDoc:
:
CreateItem()
{
returnnewCRectItem(this);//does'AddItem'automatically
}
//safedeletethatnotifiesviews
voidCMainDoc:
:
DeleteItem(CRectItem*pItem)
{
ASSERT(pItem->GetDocument()==this);
SetModifiedFlag();
UpdateAllViews(NULL,1,pItem);//pItemwillbedeleted
pItem->Delete();//doesa'RemoveItem'&'deletethis'automatically
}
/////////////////////////////////////////////////////////////////////////////
//CMainDocserialization
voidCMainDoc:
:
Serialize(CArchive&ar)
{
//NOTE:
Neweasiertouseserializationmodel--evenforOLEobjects!
WORDwMagic=0x0DAF;
if(ar.IsStoring())
{
if(HasBlankItems()&&
AfxMessageBox(IDP_SAVEINCOMPLETE,MB_YESNO|MB_ICONQUESTION)!
=IDYES)
{
TRACE0("Abortingsave--incompleteitemsfound!
\n");
AfxThrowArchiveException(CArchiveException:
:
generic);
}
ar<}
else
{
WORDw;
ar>>w;
if(w!
=wMagic)
{
TRACE0("invalidmagicnumberatstartoffile\n");
AfxThrowArchiveException(CArchiveException:
:
generic);
}
}
//serializetherestofthedocument(OLEitems)
COleLinkingDoc:
:
Serialize(ar);
}
/////////////////////////////////////////////////////////////////////////////
//CMainDoccommands
voidCMainDoc:
:
OnEditClearAll()
{
//deleteallitemsinthedocument(alsoremovessub-storages)
POSITIONpos=GetStartPosition();
while(pos!
=NULL)
{
CRectItem*pItem=(CRectItem*)GetNextItem(pos);
ASSERT_KINDOF(CRectItem,pItem);
pItem->Delete();
}
//everythingisgonenow!
SetModifiedFlag();
UpdateAllViews(NULL);
}
voidCMainDoc:
:
OnUpdateEditClearAll(CCmdUI*pCmdUI)
{
//EnableClearAllifthereisanythingtoclear
pCmdUI->Enable(GetStartPosition()!
=NULL);
}
voidCMainDoc:
:
AdjustItemPosition(CRectItem*pItem)
{
POSITIONpos=GetStartPosition();
while(pos!
=NULL)
{
CRectItem*pRectItem=(CRectItem*)GetNextItem(pos);
ASSERT_KINDOF(CRectItem,pItem);
if(pRectItem!
=pItem&&pRectItem->GetRect()==pItem->GetRect())
{
pItem->m_ptPos.x+=10;
pItem->m_ptPos.y-=10;
pos=GetStartPosition();
}
}
}
/////////////////////////////////////////////////////////////////////////////
//CMainDocdiagnostics
#ifdef_DEBUG
voidCMainDoc:
:
AssertValid()const
{
COleLinkingDoc:
:
AssertValid();
}
voidCMainDoc:
:
Dump(CDum