建立自己的Ole文档察看器控件

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

在工作中有时需要可以打开Ole文档的控件,微软提供了DsoFramer控件的源代码,但DsoFramer在使用的过程中遇到很多的Bug,于是自己尝试借用文档视图结构写一个自己的打开Ole文档的控件。

1。启动C++6.0,创建MFC ActiveX ControlWizard类型的项目,这里用TestCtrl作为项目名称,向导自动生成CTestCtrlCtrl类。

2。启动另一个C++6.0,创建单文档的应用程序,支持Ole容器和ActiveX,这里用SDI作为项目名称。

3。将SDI工程中的MainFrm.cpp,MainFrm.h,SDIView.cpp,SDIView.h,SDIDoc.cpp,SDIDoc.h,CntrItem.cpp,CntrItem.h加入TestCtrl项目中,并将这些文件的#include "SDI.h"除去。

4。在控件类CTestCtrlCtrl头文件中加入CMainFrame * m_pMainFrame;在CTestCtrlCtrl类的OnCreate虚函数中加如下代码创建主框架:

RECT rect;

this->GetClientRect(&rect);

m_pMainFrame = new CMainFrame;

m_pMainFrame->Create(NULL,"Frame",WS_CHILD|WS_VISIBLE,rect,(CWnd*)this);

这样创建的框架将被控件包含。如果加上WS_OVERLAPPEDWINDOW风格,将会看到有趣的现象。

在CMainFrame类中加入下列成员:

CCreateContext * m_pCctext;

CSDIDoc * m_pSDIDoc;

CSingleDocTemplate* m_pNewDocTemplate;

框架创建函数改动如下:

给文档类增加个public函数SetTemplate

void CSDIDoc::SetTemplate(CDocTemplate* p)

{

this->m_pDocTemplate = p;

// TODO: add one-time construction code here

}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

m_pSDIDoc = new CSDIDoc;

m_pSDIDoc->OnNewDocument();

m_pNewDocTemplate = new CSingleDocTemplate(

IDR_MENU1,

RUNTIME_CLASS(CSDIDoc),

RUNTIME_CLASS(CMainFrame), // main SDI frame window

RUNTIME_CLASS(CSDIView));

m_pCctext = new CCreateContext;

m_pCctext->m_pCurrentDoc = m_pSDIDoc;

m_pCctext->m_pCurrentFrame = this;

m_pCctext->m_pLastView = NULL;

m_pCctext->m_pNewDocTemplate = m_pNewDocTemplate;

m_pCctext->m_pNewViewClass = RUNTIME_CLASS(CSDIView);

lpCreateStruct->lpCreateParams = m_pCctext;

m_pNewDocTemplate->AddDocument(m_pSDIDoc);

m_pSDIDoc->SetTemplate(m_pNewDocTemplate);

if (CFrameWnd::OnCreate(lpCreateStruct) == -1)//改动的lpCreateStruct作为参数传入。

return -1;

POSITION pos = m_pSDIDoc->GetFirstViewPosition();//将创建的新视图设置为当前视图,以便框架使用GetActiveView函数。

while (pos != NULL)

{

CView* pView = m_pSDIDoc->GetNextView(pos);

ASSERT_VALID(pView);

this->SetActiveView(pView);

}

return 0;//这里删除了以前框架的代码,不删除也可。

}

重写 CSDICntrItem虚函数,什么都不做,将不加入Ole服务器程序给我们的菜单

void CSDICntrItem::OnInsertMenus(CMenu* pMenuShared,LPOLEMENUGROUPWIDTHS lpMenuWidths)

{

}

5.响应控件的WM_SIZE消息,加入如下代码:

m_pMainFrame->SetWindowPos(&CWnd::wndTop,0,0,cx,cy,SWP_NOMOVE);

这样,支持Ole容器的文档视图结构就嵌入到控件中来了。

若给控件加入Open(LPCTSTR path) 和Close() Com接口函数,主框架和视图也相应加入public函数Open(LPCTSTR path) 和Close()

控件中:

void CTestCtrlCtrl::Open(LPCTSTR Path)

{

// TODO: Add your dispatch handler code here

m_pMainFrame->AssertValid();

if(m_pMainFrame != NULL)

{

m_pMainFrame->Close();

m_pMainFrame->Open(Path);

}

}

void CTestCtrlCtrl::Close()

{

// TODO: Add your dispatch handler code here

m_pMainFrame->AssertValid();

if(m_pMainFrame != NULL)

m_pMainFrame->Close();

}

关闭控件时候通知框架:

void CTestCtrlCtrl::OnDestroy()

{

COleControl::OnDestroy();

m_pMainFrame->SendMessage(WM_CLOSE);

}

主框架中:

void CMainFrame::Open(LPCSTR Path)

{

CSDIView * pView = (CSDIView *)this->GetActiveView();

if(pView != NULL)

{

pView->AssertValid();

pView->Open(Path);

}

// TODO: Add your message handler code here

}

void CMainFrame::Close()

{

CSDIView * pView = (CSDIView *)this->GetActiveView();

if(pView != NULL)

{

pView->AssertValid();

pView->Close();

}

// TODO: Add your message handler code here

}

视图中:

void CSDIView::Close()

{

COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);

if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)

{

// pActiveItem->Deactivate();

pActiveItem->Delete();

pActiveItem->Release();

ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);

}

}

void CSDIView::Open(LPCSTR path)

{

//Change the cursor so that the user knows that something exciting is going

//on.

BeginWaitCursor();

CSDICntrItem* pItem = NULL;

TRY

{

//Get the document associated with this view, and be sure that it is

//valid.

CSDIDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

//Create a new item associated with this document, and be sure that

//it is valid.

pItem = new CSDICntrItem(pDoc);

ASSERT_VALID(pItem);

// Create the Excel embedded item.

if(!pItem->CreateFromFile(path))

AfxThrowMemoryException();

//Make sure the new CContainerItem is valid.

ASSERT_VALID(pItem);

this->AssertValid();

// Start the server to edit the item.

pItem->DoVerb(OLEIVERB_SHOW, this);

// As an arbitrary user interface design, this sets the

// selection to the last item inserted.

m_pSelection = pItem; // Set selection to last inserted. item

pDoc->UpdateAllViews(NULL);

}

}

这样,借助了MFC文档视图结构,一个Ole文档察看器大体完成了。

顾冬岩 2005 . 4 . 5

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航