使用VC++獲得Microsoft Word97應用事件
發表于:2007-07-14來源:作者:點擊數:
標簽:
概要 這文章證明怎樣使用VC++.獲得微軟word97應用事件。但是,在這文章中觀念和代碼是并非對Microsoft Word特有。 他們是適用于整個套微軟office應用, 以及任何其它類似的應用程序。 更多信息 下列的步給怎樣建立MFC應用,它可以捕獲Microsoft Word 97 Appli
概要
這文章證明怎樣使用VC++.獲得微軟word97應用事件。但是,在這文章中觀念和代碼是并非對Microsoft Word特有。 他們是適用于整個套微軟office應用, 以及任何其它類似的應用程序。
更多信息
下列的步給怎樣建立MFC應用,它可以捕獲Microsoft Word 97 Application事件Startup(), DocumentChange()和Quit():
使用MFC AppWizard創造一個新對話框。給出項目的名字WordEvents, 接受缺省的設置.給你的對話框添加兩按鍵,并分別命名為"Start and Setup"和"Quit and Clean Up"。
添加下列的代碼到 "Start and Setup"按鍵的句柄:
// 檢查看是否你已經啟動
服務器.
if(m_app.m_lpDispatch != NULL) {
AfxMessageBox("Server a
lready started.");
return;
}
char buf[256]; // General purpose buffer.
// 打開自動化服務器.
COleException e;
if(!m_app.CreateDispatch("Word.Application.8", &e)) {
sprintf(buf, "Error on CreateDispatch(): %ld (%08lx)",e.m_sc, e.m_sc);
AfxMessageBox(buf, MB_SETFOREGROUND);
return;
}
// 通過自動化使服務器變得可見.
// I.e.: Application.Visible = TRUE
DISPID dispID;
unsigned short *ucPtr;
BYTE *parmStr;
ucPtr = L"visible";
m_app.m_lpDispatch->GetIDsOfNames(IID_NULL, &ucPtr, 1, LOCALE_USER_DEFAULT, &dispID);
parmStr = (BYTE *)( VTS_VARI
ANT );
m_app.InvokeHelper(dispID, DISPATCH_METHOD | DISPATCH_PROPERTYPUT, VT_EMPTY,NULL, parmStr, &COleVari
ant((short)TRUE));
// 事件獲得.
// {000209F7-0000-0000-C000-000000000046}
static const GUID IID_IWord8AppEvents = {0x000209f7,0x000,0x0000,{0xc0,0x00,0x0,0x00,0x00,0x00,0x00,0x46 } };
// 建立事件步驟
// 1. 得到服務器的IConnectionPointContainer接口.
// 2. 調用IConnectionPointContainerFindConnectionPoint()尋找到希望獲得的接口
// 3. 調用IConnectionPoint::Advise()
HRESULT hr;
// 得到服務器的IConnectionPointContainer接口.
IConnectionPointContainer *pConnPtContainer;
hr = m_app.m_lpDispatch->QueryInterface(IID_IConnectionPointContainer,(void **)&pConnPtContainer);
ASSERT(!FAILED(hr));
// 為使你感興趣的事件找到聯系點.
hr = pConnPtContainer->FindConnectionPoint(IID_IWord8AppEvents,&m_pConnectionPoint);
ASSERT(!FAILED(hr));
// 得到你的事件實現的IUnknown界面.
LPUNKNOWN pUnk = m_myEventSink.GetInterface(&IID_IUnknown);
ASSERT(pUnk);
// 建立advisory聯系!
hr = m_pConnectionPoint->Advise(pUnk, &m_adviseCookie);
ASSERT(!FAILED(hr));
// 釋放IConnectionPointContainer
pConnPtContainer->Release();
下面是"Quit and Clean Up"按鈕的處理句柄代碼:
// 如果你已啟動服務器.
if(m_app.m_lpDispatch == NULL) {
AfxMessageBox("You haven't started the server yet.");
return;
}
m_pConnectionPoint->Unadvise(m_adviseCookie);
// 告訴服務器放棄.
// Application.Quit()
DISPID dispID; // Temporary DISPID
unsigned short *ucPtr; // Temporary name holder
ucPtr = L"quit";
m_app.m_lpDispatch->GetIDsOfNames(
IID_NULL, &ucPtr, 1, LOCALE_USER_DEFAULT, &dispID
);
m_app.InvokeHelper(dispID, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);
// 釋放應用對象.
m_app.ReleaseDispatch();
打開MFC ClassWizard (CTRL+W),加入一個源自
CCmdTarget的新類MyEventSink。按下面的順序加入新方法:
void Startup()
void Quit()
void DocumentChange()
在MyEventSink.cpp中,我們會看到下面的代碼:
void MyEventSink::Startup() { AfxMessageBox("MyEventSink::Startup() called."); }
void MyEventSink::Quit() { AfxMessageBox("MyEventSink::Quit() called.");}
void MyEventSink::DocumentChange(){ AfxMessageBox("MyEventSink::DocumentChange() called.");}
打開你的MyEventSink.cpp文找到IID_IMyEventSink聲明. ClassWizard為你的界面產生一新的隨機GUID, 但是因為你執行一已有GUID的特有接口,所以需要進行如下修改:
static const GUID IID_IMyEventSink = {0x000209f7,0x000,0x0000,{0xc0,0x00,0x0,0x00,0x00,0x00,0x00,0x46}};
給你的在WordEventsDlg.h中WordEventsDlg類添加下列的公共成員變量:
COleDispatchDriver m_app;
IConnectionPoint *m_pConnectionPoint;
DWORD m_adviseCookie;
MyEventSink m_myEventSink;
WordEventsDlg.h中加入下面聲明:
#include "MyEventSink.h"
打開文件MyEventSink.h和找出聲明的destructor;它將如下出現:
// Implementation
protected:
virtual ~MyEventSink();
在"Protected"之上寫出聲明:
virtual ~MyEventSink();
// Implementation
protected:
// virtual ~MyEventSink(); // 或者這行可以刪除.
最后,確保OLE/COM庫有機會預置.正好在你的"start and setup"按鍵的前面添加下列的代碼.這個創造全局類。初始化函數會初始化OLE/COM庫。
// Ole初始化類
class OleInitClass {
public:
OleInitClass() { OleInitialize(NULL);}
~OleInitClass() { OleUninitialize();}
};
OleInitClass g_OleInitClass;
運行下面的程序你會發現當你新建文檔的時候會有一個菜單彈出。在創建事件獲得函數時請按順序進行,因為word中的這三個事件函數分別對應DISPIDs 1, 2和3,如果用戶按順序創建,則不需要修改。如果象excel中一樣,事件不是按順序的,則需要人工添加dispid號。
原文轉自:http://www.kjueaiud.com