文章編號: 309309 - 上次校閱: 2005年8月23日 - 版次: 4.3
如何使用 Visual C++.NET 2002年或 Visual C++.NET 2003年和 MFC 處理 PowerPoint 2000 事件或 PowerPoint 2002 事件
系統提示 本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
附註 Microsoft Visual C++.NET (2002),支援 Managed 程式碼模型所提供的 Microsoft.NET Framework 和不受管理的原生 Microsoft Windows 程式碼模型。本文資訊僅適用於不受管理的 Visual C++ 程式碼。
本文將告訴您,如何藉由使用 Microsoft Visual C++.NET 2002年或 Visual C++.NET 2003年和 MFC 處理 Microsoft PowerPoint 2000 事件或 PowerPoint 2002 的事件。
PowerPoint 事件PowerPoint 會引發事件以回應使用者動作或透過自動化呼叫某些方法的回應。在 PowerPoint 物件模型中的 [
應用程式 ] 物件就會引發這些事件在其輸出介面
EApplication 上。
若要檢視這個介面和它的方法,您可以使用 OLE/COM 物件檢視器,如下所示:
在 Visual Studio.NET 中的 [工具 ] 功能表上選取 [OLE/COM 物件檢視器 ]。 展開節點的 型別程式庫 ,並選取 [在清單內的 [Microsoft PowerPoint 物件程式庫 。 在 [物件 ,功能表上選取 [檢視 ],以在 ITypeLib 檢視器中開啟文件庫]。 展開 coclass 應用程式 的節點,然後選取 [EApplication 。 請注意
EApplication 衍生自
IDispatch ,而不是分配介面通常是用作為來源介面。如果來源介面一個分配介面您可以判斷它的方法的分派識別項 (DISPID) 藉由使用 OLE/COM 物件檢視器。不過,因為
EApplication 不是一個分配介面,您無法判斷 PowerPoint 事件 DISPID 藉由檢查型別程式庫。
下表列出 DISPID PowerPoint 2000 及 PowerPoint 2002 物件模型公開 (Expose) 的事件:
您可以藉由呼叫
IConnectionPointContainer::FindConnectionPoint 來尋找您想要的事件] 介面的連接點,然後再
IConnectionPoint::Advise 與該事件您實作的
IUnknown 介面為非一個 PowerPoint 事件接收 C + + 應用程式中。
建立 C + + 應用程式處理 PowerPoint 事件在 Visual C++.NET 使用 Microsoft 基礎類別 (MFC) 應用程式精靈,以建立以新對話方塊方塊為基礎的應用程式。命名您的專案 PPTEventsDemo,並接受預設設定。連同相對應的 Ppteventsdemodlg.cpp 和 Ppteventsdemodlg.h 檔案的預設會建立 [] 對話方塊。 將三個按鈕加入至您對話方塊並再分別命名的按鈕,啟動 PowerPoint 、 建立連接點,並註冊接收器 ,然後 取消註冊接收器] 和 [正常待辦事項 」 。新增清單方塊] 對話方塊。發生時,清單方塊就會顯示事件的名稱。 在 [專案總管] 視窗在 [類別檢視中, PPTEventsDemo ] 上按一下滑鼠右鍵、 指向 [新增 ],然後按一下 [加入類別 ]。在 [加入類別 ] 對話方塊選取 TypeLibrary 從 MFC 類別 ,然後按一下 [開啟舊檔 ]。這會從 TypeLib 精靈開始加入類別。 從 可用 TypeLibraries 下拉式清單中選取 [Microsoft PowerPoint 10.0 物件程式庫 (針對 PowerPoint 2002) 或 Microsoft PowerPoint 9.0 物件程式庫 (針對 PowerPoint 2000)]。[介面 ] 清單方塊會顯示所有的型別程式庫會公開的介面。選取 [_Application ],然後按一下 > ] 按鈕。接受預設值,然後按一下 [完成] 。這會產生衍生自 COleDispatchDriver CApplication 包裝函式類別。實作和這個類別的定義隨即出現在 Capplication.h 檔案。 Visual 的 Studio.NET 中按一下 [若要顯示 PPTEventsDemo ] 對話方塊的 [檢視 ] 功能表上的 [資源檢視 ]。連按兩下以顯示該按鈕的 Click 事件已經插入空的事件處理常式的 Ppteventsdemodlg.cpp [程式碼檢視] 視窗 啟動 PowerPoint 。將下列程式碼加入至 啟動 PowerPoint 按鈕處理常式:
if(!pptapp.CreateDispatch("Powerpoint.Application"))
{
AfxMessageBox("Could not create Powerpoint object.");
return;
}
pptapp.put_Visible((long) 1);
建立連接點及註冊接收器 按鈕處理常式中加入下列程式碼:
///*********************** Start of code to get connection point **************
// Declare the events that you want to catch.
//
// Look for the coclass for Application in the Msppt9.olb typelib,
// then look for the word "source." The EApplication interface
// is the next search target. When you find it, you will see the
// following GUID for the event interface.
// 914934C2-5A91-11CF-8700-00AA0060263B
static const GUID IID_IEApplication =
{0x914934C2,0x5A91,0x11CF, {0x87,0x00,0x00,0xAA,0x00,0x60,0x26,0x3b}};
// Steps for setting up events.
// 1. Get the IConnectionPointContainer interface of the server.
// 2. Call IConnectionPointContainer::FindConnectionPoint()
// to find the event that you want to catch.
// 3. Call IConnectionPoint::Advise() with the IUnknown
// interface of your implementation of the events.
HRESULT hr;
// Get the (PPT) IConnectionPointContainer interface of the server.
IConnectionPointContainer *pConnPtContainer;
hr = pptapp.m_lpDispatch->QueryInterface(
IID_IConnectionPointContainer,
(void **)&pConnPtContainer
);
if(FAILED(hr)) AfxMessageBox("Couldn't get IConnectionPointContainer interface.");
ASSERT(!FAILED(hr));
// Find a connection point for the events that you are interested in.
hr = pConnPtContainer->FindConnectionPoint(
IID_IEApplication,
&m_pConnectionPoint
);
if(FAILED(hr)) AfxMessageBox("Couldn't find connection point via event GUID.");
ASSERT(!FAILED(hr));
//Instantiate the sink object.
m_sink = new CMyPPTEventsHandler();
//Update the list box when you obtain the events in the event handler.
m_sink->m_pListBox = m_listBox;
// Get the IUnknown interface of your event implementation.
LPUNKNOWN pUnk = NULL;
pUnk = m_sink->GetInterface(&IID_IUnknown);
ASSERT(pUnk);
// Setup advisory connection.
hr = m_pConnectionPoint->Advise(pUnk, &m_sink->cookie);
ASSERT(!FAILED(hr));
// Release the IConnectionPointContainer interface.
pConnPtContainer->Release();
// *********************** End of code to get connection point ******************
將下列程式碼加入至 取消註冊接收器] 及 [正常待辦向上 按鈕的處理常式:
//Use the cookie to unregister the sink.
m_pConnectionPoint->Unadvise(m_sink->cookie);
m_pConnectionPoint->Release();
//Detach the application object from the server.
pptapp.DetachDispatch();
加入下列程式碼中 CPPTEventsDemoDlg 類別建構函式的底部:
m_pConnectionPoint = NULL;
pptapp = NULL;
您從 CPPTEventsDemoDlg::OnInitDialog 傳回之前新增下行的程式碼:
//Get the MFC class pointer for the list box on the dialog box.
m_listBox = (CListBox*) GetDlgItem(IDC_LIST1);
請務必將下列的 # include 陳述式放在 Ppteventsdemodlg.cpp 檔案開頭:
#include "stdafx.h"
#include "CApplication.h"
#include "MyPPTEventsHandler.h"
#include "PPTEventsDemo.h"
#include "PPTEventsDemoDlg.h"
向前宣告 Ppteventsdemodlg.h 檔案中的下列類別:
class CMyPPTEventsHandler;
class CApplication;
將下列宣告為私用成員加入至 CPPTEventsDemoDlg :
IConnectionPoint* m_pConnectionPoint;
CApplication pptapp;
CMyPPTEventsHandler* m_sink;
CListBox* m_listBox;
在專案總管] 中的 [類別檢視 PPTEventsDemo ] 上按一下滑鼠右鍵、 指向 [新增 ],然後按一下 [加入類別 ]。在 AddClass ] 對話方塊選取 範本 ,] 下方的 MFC 類別 ,然後按一下 [開啟舊檔 ]。類別] 名稱的型別 CMyPPTEventsHandler 選取 CCmdTarget 基底類別,然後選取 [自動化 ]。其他欄位接受預設值。按一下 [完成 ]。這會建立一個新的 CMyPPTEventsHandler MFC 類別衍生自 CCmdTarget 。這個類別會定義在 Myppteventshandler.h 檔案,且 Myppteventshandler.cpp 中實作。這是包含在 PowerPoint 事件的回應中呼叫的方法的 事件處理常式 類別。 這會建立新的在 Myppteventshandler.h 檔案,向前宣告下列類別: 這會建立新的在 Myppteventshandler.h 檔案,將下列成員加入至公用 CMyPPTEventsHandler 類別中宣告:
DWORD cookie;
CListBox* m_pListBox;
這會建立新的新增下列的方法,以保護宣告 CMyPPTEventsHandler 類別中:
void WindowSelectionChange(LPDISPATCH Pres);
void WindowBeforeRightClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel);
void WindowBeforeDoubleClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel);
void PresentationClose ( LPDISPATCH Pres);
void PresentationSave( LPDISPATCH Pres);
void PresentationOpen( LPDISPATCH Pres);
void NewPresentation( LPDISPATCH Pres);
void PresentationNewSlide( LPDISPATCH Pres);
void WindowActivate( LPDISPATCH Pres,LPDISPATCH Wn);
void WindowDeactivate(LPDISPATCH Pres, LPDISPATCH Wn);
void SlideShowBegin(LPDISPATCH Wn);
void SlideShowNextBuild( LPDISPATCH Wn);
void SlideShowNextSlide( LPDISPATCH Wn);
void SlideShowEnd( LPDISPATCH Pres);
void PresentationPrint(LPDISPATCH Pres);
void SlideSelectionChanged(LPDISPATCH SldRange);
void ColorSchemeChanged(LPDISPATCH SldRange);
void PresentationBeforeSave(LPDISPATCH Pres, VARIANT_BOOL * Cancel);
void SlideShowNextClick(LPDISPATCH Wn, LPDISPATCH nEffect);
void PresentationPrint(LPDISPATCH Pres);
這些是 PowerPoint 事件處理常式。注意: 下列 4 個事件處理常式無法在 PowerPoint 2000 使用而不會呼叫如果用戶端有 PowerPoint 2000:
void SlideSelectionChanged(LPDISPATCH SldRange);
void ColorSchemeChanged(LPDISPATCH SldRange);
void PresentationBeforeSave(LPDISPATCH Pres, VARIANT_BOOL * Cancel);
void SlideShowNextClick(LPDISPATCH Wn, LPDISPATCH nEffect);
這會建立新的取代整個 Myppteventshandler.cpp 的內容與下列:
// MyPPTEventsHandler.cpp : implementation file.
//
#include "stdafx.h"
#include "PPTEventsDemo.h"
#include "MyPPTEventsHandler.h"
// CMyPPTEventsHandler.
IMPLEMENT_DYNAMIC(CMyPPTEventsHandler, CCmdTarget)
CMyPPTEventsHandler::CMyPPTEventsHandler()
{
EnableAutomation();
}
CMyPPTEventsHandler::~CMyPPTEventsHandler()
{
}
void CMyPPTEventsHandler::OnFinalRelease()
{
// When the last reference for an Automation object is released,
// OnFinalRelease is called. The base class automatically
// deletes the object. Add additional cleanup required for your
// object before you call the base class.
CCmdTarget::OnFinalRelease();
}
BEGIN_MESSAGE_MAP(CMyPPTEventsHandler, CCmdTarget)
END_MESSAGE_MAP()
BEGIN_DISPATCH_MAP(CMyPPTEventsHandler, CCmdTarget)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"WindowSelectionChange",2001,WindowSelectionChange,VT_EMPTY, VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler , "WindowBeforeRightClick", 2002 ,WindowBeforeRightClick, VT_EMPTY , VTS_DISPATCH VTS_BOOL)
DISP_FUNCTION_ID(CMyPPTEventsHandler , "WindowBeforeDoubleClick", 2003 ,WindowBeforeDoubleClick, VT_EMPTY, VTS_DISPATCH VTS_BOOL)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"PresentationClose",2004,PresentationClose,VT_EMPTY, VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"PresentationSave",2005,PresentationSave,VT_EMPTY, VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"PresentationOpen",2006,PresentationOpen,VT_EMPTY, VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"NewPresentation",2007,NewPresentation,VT_EMPTY, VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"PresentationNewSlide",2008,PresentationNewSlide,VT_EMPTY, VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"WindowActivate",2009,WindowActivate,VT_EMPTY, VTS_DISPATCH VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"WindowDeactivate",2010,WindowDeactivate,VT_EMPTY, VTS_DISPATCH VTS_DISPATCH)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"SlideShowBegin",2011,SlideShowBegin,VT_EMPTY, VTS_DISPATCH )
DISP_FUNCTION_ID(CMyPPTEventsHandler,"SlideShowNextBuild",2012,SlideShowNextBuild,VT_EMPTY, VTS_DISPATCH )
DISP_FUNCTION_ID(CMyPPTEventsHandler,"SlideShowNextSlide",2013,SlideShowNextSlide,VT_EMPTY, VTS_DISPATCH )
DISP_FUNCTION_ID(CMyPPTEventsHandler,"SlideShowEnd",2014,SlideShowEnd,VT_EMPTY, VTS_DISPATCH )
DISP_FUNCTION_ID(CMyPPTEventsHandler,"PresentationPrint",2015,PresentationPrint,VT_EMPTY, VTS_DISPATCH )
DISP_FUNCTION_ID(CMyPPTEventsHandler,"SlideSelectionChanged",2016,SlideSelectionChanged,VT_EMPTY, VTS_DISPATCH )
DISP_FUNCTION_ID(CMyPPTEventsHandler,"ColorSchemeChanged",2017,ColorSchemeChanged,VT_EMPTY, VTS_DISPATCH )
DISP_FUNCTION_ID(CMyPPTEventsHandler,"PresentationBeforeSave",2018,PresentationBeforeSave,VT_EMPTY, VTS_DISPATCH VTS_BOOL)
DISP_FUNCTION_ID(CMyPPTEventsHandler,"SlideShowNextClick",2019,SlideShowNextClick,VT_EMPTY, VTS_DISPATCH VTS_DISPATCH )
END_DISPATCH_MAP()
//The GUUID is different from the one originally generated by Class Wizard. This GUUID is the same as the one for the EApplication outgoing event interface.
static const IID IID_IMyPPTEventsHandler =
{0x914934C2,0x5A91,0x11CF, {0x87,0x00,0x00,0xAA,0x00,0x60,0x26,0x3b}};
BEGIN_INTERFACE_MAP(CMyPPTEventsHandler, CCmdTarget)
INTERFACE_PART(CMyPPTEventsHandler, IID_IMyPPTEventsHandler, Dispatch)
END_INTERFACE_MAP()
// CMyPPTEventsHandler message handlers.
void CMyPPTEventsHandler::WindowSelectionChange(LPDISPATCH Pres)
{
m_pListBox->AddString("WindowSelectionChange");
return ;
}
void CMyPPTEventsHandler::WindowBeforeRightClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel)
{
m_pListBox->AddString("WindowBeforeRightClick");
return ;
}
void CMyPPTEventsHandler::WindowBeforeDoubleClick(LPDISPATCH Pres, VARIANT_BOOL* Cancel)
{
m_pListBox->AddString("WindowBeforeDoubleClick");
return;
}
void CMyPPTEventsHandler::PresentationClose ( LPDISPATCH Pres)
{
m_pListBox->AddString("PresentationClose");
return ;
}
void CMyPPTEventsHandler::PresentationSave( LPDISPATCH Pres)
{
m_pListBox->AddString("PresentationSave");
return ;
}
void CMyPPTEventsHandler::PresentationOpen( LPDISPATCH Pres)
{
m_pListBox->AddString("PresentationOpen");
return ;
}
void CMyPPTEventsHandler::NewPresentation( LPDISPATCH Pres)
{
m_pListBox->AddString("NewPresentation");
return ;
}
void CMyPPTEventsHandler::PresentationNewSlide( LPDISPATCH Pres)
{
m_pListBox->AddString("PresentationNewSlide");
return ;
}
void CMyPPTEventsHandler::WindowActivate( LPDISPATCH Pres,LPDISPATCH Wn)
{
m_pListBox->AddString("WindowActivate");
return ;
}
void CMyPPTEventsHandler::WindowDeactivate(LPDISPATCH Pres, LPDISPATCH Wn)
{
m_pListBox->AddString("WindowDeactivate");
return ;
}
void CMyPPTEventsHandler::SlideShowBegin(LPDISPATCH Wn)
{
m_pListBox->AddString("SlideShowBegin");
return;
}
void CMyPPTEventsHandler::SlideShowNextBuild( LPDISPATCH Wn)
{
m_pListBox->AddString("SlideShowNextBuild");
return ;
}
void CMyPPTEventsHandler::SlideShowNextSlide( LPDISPATCH Wn)
{
m_pListBox->AddString("SlideShowNextSlide");
return ;
}
void CMyPPTEventsHandler::SlideShowEnd( LPDISPATCH Pres)
{
m_pListBox->AddString("SlideShowEnd");
return;
}
void CMyPPTEventsHandler::PresentationPrint(LPDISPATCH Pres)
{
m_pListBox->AddString("PresentationPrint");
return;
}
//The following events are not available for PowerPoint 2000.
void CMyPPTEventsHandler::SlideSelectionChanged(LPDISPATCH SldRange)
{
m_pListBox->AddString("SlideSelectionChanged");
return ;
}
void CMyPPTEventsHandler::ColorSchemeChanged(LPDISPATCH SldRange)
{
m_pListBox->AddString("ColorSchemeChanged");
return ;
}
void CMyPPTEventsHandler::PresentationBeforeSave(LPDISPATCH Pres, VARIANT_BOOL * Cancel)
{
m_pListBox->AddString("PresentationBeforeSave");
return;
}
void CMyPPTEventsHandler::SlideShowNextClick(LPDISPATCH Wn, LPDISPATCH nEffect)
{
m_pListBox->AddString("SlideShowNextClick");
return ;
}
注意 IID_IMyPPTEventsHandler 靜態常數的值已變更從類別精靈原本產生的值。值變更為下列:
static const IID IID_IMyPPTEventsHandler =
{0x914934C2,0x5A91,0x11CF, {0x87,0x00,0x00,0xAA,0x00,0x60,0x26,0x3b}};
這是 EApplication 傳出事件介面的 PowerPoint 應用程式 類別的 GUID。同時而且,請注意這個類別的分派對應將事件的 DISPID 對應至事件處理常式方法。 這會建立一個新的在 Ppteventsdemo.cpp 檔案,新增下面的的 # include 陳述式
#include "CApplication.h"
下列之前:
#include "PPTEventsDemoDlg.h"
這會建立新的加入下列幾行程式碼 InitInstance 方法 Ppteventsdemoapp.cpp 檔案的頂端:
if(!AfxOleInit())
{
AfxMessageBox("Unable to initialize COM");
return FALSE;
} 測試應用程式按下 F5 以建置並執行程式。對話方塊隨即出現。 按一下 [啟動 PowerPoint ]。PowerPoint 會啟動,並會成為可見。 按一下 [建立連接點及註冊接收器 來設定事件接收器。 在 PowerPoint 中建立新的簡報。WindowActivate 、 NewPresentation 、 PresentationNewSlide 、 SlideSelectionChange 及 WindowSelectionChange 事件引發。 啟動投影片放映,並透過結束播放。SlideShowBegin 、 SlideShowNextSlide 和 SlideShowEnd 事件觸發。 儲存簡報。PresentationBeforeSave 和 PresentationSave 事件觸發。 關閉簡報。[PresentationClose ] 引發,並由 PowerPoint 2002 所觸發,而且由程式所處理的事件出現在清單方塊。注意: 這些是 PowerPoint 2002 所引發的事件。您可能無法看到這些事件在 PowerPoint 2000 部分。 按一下 [取消註冊接收器和清除待辦 Up 中斷連線事件接收器。 關閉對話方塊。 如需詳細資訊按一下面的文件編號,檢視 「 Microsoft 知識庫 」 中 「 文件:
254009 ?
(http://support.microsoft.com/kb/254009/EN-US/
)
資訊: PowerPoint 2000 事件示範可供下載
308336 ?
(http://support.microsoft.com/kb/308336/EN-US/
)
HOWTO: 使用自動化來建立並顯示使用 Visual C++.NET 和 MFC 的 PowerPoint 簡報
如上 Office 自動化的詳細資訊,請參閱下列 Microsoft Office 程式開發支援網站:
這篇文章中的資訊適用於: Microsoft Visual C++ .NET 2002 Standard Edition Microsoft PowerPoint 2000 Standard Edition Microsoft PowerPoint 2002 Standard Edition Microsoft Visual C++ .NET 2003 Standard Edition Microsoft Foundation Class Library 4.2 kbmt _ik11561 kbautomation kbgrpdso kbhowtomaster KB309309 KbMtzh
機器翻譯 重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:
309309 ?
(http://support.microsoft.com/kb/309309/en-us/
)
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
請說明您希望我們對本篇文章進行什麼改善及需要改善的原因
謝謝您!您的意見將協助我們改進支援內容。如需更多協助選項,請造訪
說明及支援首頁 。