本文將告訴您,如何以 Microsoft Foundation Classes (MFC) 取得 Office
應用程式的視窗控制代碼,同時從 Visual C++ 自動化該應用程式。
大多數 Microsoft Office
應用程式的物件模型並不會公開用來擷取應用程式視窗控制代碼的屬性。如果要找出您想自動化之 Office 應用程式的視窗控制代碼,您可以搭配使用
FindWindow API
函數與應用程式最上層視窗的類別名稱。如果應用程式能夠同時執行多個執行個體,您可能必須將這點考慮進去,這樣才能擷取正確的視窗控制代碼。下列章節說明的技術,可以讓您用來擷取單一和多個執行個體應用程式的視窗控制代碼。
注意 Microsoft Access 物件模型會公開
_Application 物件的
hWndAccessApp 函數,以找出應用程式的視窗控制代碼。此外,Microsoft Excel 2002 是第一個為
Application 物件引進
hWnd 屬性的版本。對於 Microsoft Excel 2002、Microsoft Access 97 和更新版本,由於這些
Office 應用程式提供了透過各自物件模型來擷取應用程式視窗控制代碼的方法,因此不需要用到本文所述的 FindWindow 方法。
尋找單一執行個體之應用程式的視窗控制代碼
下列步驟將告訴您,如何搭配使用
FindWindow 函數與 Visual C++ Automation 用戶端,以找出只能有單一執行個體之跨處理序 (Out-Of-Process)
自動化伺服器的視窗控制代碼。將 Microsoft PowerPoint 當做自動化伺服器使用時,您可能會用到這項技術。
逐步範例- 建立新的對話方塊架構 MFC EXE 專案。
- 新增按鈕至您的對話方塊,並為該按鈕新增 BN_CLICKED 處理常式。
- 開啟 [ClassWizard] (CTRL+W),按一下 [自動化] 索引標籤,再按一下 [加入類別],然後選取 [從型別程式庫]。
- 移至您安裝 Office (例如 C:\Program Files\Microsoft Office\Office)
的目錄,並選擇 Msppt9.olb。
- 選取 [ClassWizard] 找到的所有類別,並按一下 [確定] 返回專案。[ClassWizard] 已經從 PowerPoint 型別程式庫產生一些自動化包裝函式類別,並建立了 Msppt9.h 和 Msppt9.cpp 檔案。
- 將下列程式碼新增至您的按鈕處理常式:
// Start PowerPoint
_Application app;
COleException e;
if(!app.CreateDispatch("PowerPoint.Application", &e)) {
CString str;
str.Format("CreateDispatch() failed w/err 0x%08lx", e.m_sc),
AfxMessageBox(str, MB_SETFOREGROUND);
return;
}
// Get Presentations collection and add a new presentation.
Presentations presSet(app.GetPresentations());
_Presentation pres(presSet.Add(TRUE));
// Get Slides collection and add a new slide.
Slides slideSet(pres.GetSlides());
_Slide slide1(slideSet.Add(1, 2));
// Add text to slide, by navigating the slide as follows:
// slide1.shapes(#).TextFrame.TextRange.Text
{
Shapes shapes(slide1.GetShapes());
Shape shape(shapes.Item(COleVariant((long)1)));
TextFrame textFrame(shape.GetTextFrame());
TextRange textRange(textFrame.GetTextRange());
textRange.SetText("Window Handle of PowerPoint Application");
}
// Get the Window Handle
HWND hWndPpt = ::FindWindow ("PP9FrameClass", NULL);
::ShowWindow (hWndPpt, SW_SHOWNORMAL);
// You can make the application visible using:
// app.SetVisible(TRUE);
// Here ShowWindow API is used to demonstrate the use of
// the Window Handle got from FindWindow<BR/>
::MessageBox(NULL, "Now displaying PPT using the ShowWindow API", "", MB_SETFOREGROUND);
{
Shapes shapes(slide1.GetShapes());
Shape shape(shapes.Item(COleVariant((long)2)));
TextFrame textFrame(shape.GetTextFrame());
TextRange textRange(textFrame.GetTextRange());
textRange.SetText("hWndPpt contains the Window Handle of the PowerPoint application. You can "
"use this Window Handle in various Win 32 APIs, such as SetForeGroundWindow, "
"which require a Window Handle parameter to be supplied.\r\n"
"This example uses the Window Handle in the ShowWindow API.\r\n\r\n"
"All done. PowerPoint will close in 15 seconds.");
}
// Prepare and run a slide show.
{
SlideShowSettings sss(pres.GetSlideShowSettings());
sss.SetShowType(3); // Kiosk.
sss.SetLoopUntilStopped(TRUE);
sss.SetRangeType(1); // Show all.
sss.SetAdvanceMode(2); // Use slide timings.
SlideShowWindow ssw(sss.Run()); // Run show.
}
// Sleep so user can watch slide show.
::Sleep(15000);
// Tell PowerPoint to quit.
app.Quit();
- 在實作按鈕處理常式之前,請先加入下列行:
#include "msppt9.h"
// Ole initialization class.
class OleInitClass {
public:
OleInitClass() {
OleInitialize(NULL);
}
~OleInitClass() {
OleUninitialize();
}
};
// This global class calls OleInitialize() at
// application startup, and calls OleUninitialize()
// at application exit.
OleInitClass g_OleInitClass;
- 編譯並執行程式。按一下按鈕並請注意,PowerPoint 會開啟單一投影片簡報,並在 15
秒後關閉該簡報。
尋找能夠允許多個執行個體之應用程式的視窗控制代碼
有些應用程式 (例如 Microsoft Excel 或 Microsoft Word)
能夠同時執行多個執行個體。如果要擷取您想自動化之應用程式執行個體的控制代碼,您可以先使用「自動化」將應用程式的標題變更為唯一值,再使用
FindWindow 來擷取應用程式的視窗控制代碼。下列步驟將 Microsoft Excel
當做自動化伺服器使用,藉以說明這項技術。
逐步範例- 建立新的對話方塊架構 MFC EXE 專案。
- 新增按鈕至您的對話方塊,並為該按鈕新增 BN_CLICKED 處理常式。
- 開啟 [ClassWizard] (CTRL+W),按一下 [自動化] 索引標籤,再按一下 [加入類別],然後選取 [從型別程式庫]。
- 移至您安裝 Office (例如 C:\Program Files\Microsoft Office\Office)
的目錄,並選擇 Excel9.olb。
- 選取 [ClassWizard] 找到的所有類別,並按一下 [確定] 返回專案。[ClassWizard] 已經從 Excel 型別程式庫產生一些自動化包裝函式類別,並建立了 excel9.h 和 excel9.cpp 檔案。
- 將下列程式碼新增至您的按鈕處理常式:
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
// Start Excel and get Application object.
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't start Excel and get Application object.");
return;
}
//Get a new workbook.
books = app.GetWorkbooks();
book = books.Add (covOptional);
app.SetCaption ("New Caption Supplied by Program");
// Get the Window Handle
HWND hWndXL = ::FindWindow ("XLMAIN", app.GetCaption ());
// Set the original caption back
app.SetCaption ("");
::ShowWindow (hWndXL, SW_SHOWNORMAL);
// You can make the application visible using:
// app.SetVisible(TRUE);
// Here ShowWindow API is used to demonstrate the use of
// the Window Handle got from FindWindow
AfxMessageBox ("hWndXL contains the Window Handle of the Excel application. You can use\n"
"this Window Handle in various Win 32 APIs, such as SetForeGroundWindow,\n"
"which require a Window Handle parameter to be supplied.\n\n"
"This example uses the Window Handle in the ShowWindow API.\n\n"
"All done. Press OK to close Excel.", MB_SETFOREGROUND);
// Tell Excel to quit.
app.Quit();
- 在實作按鈕處理常式之前,請先加入下列行:
#include "excel9.h"
// Ole initialization class.
class OleInitClass {
public:
OleInitClass() {
OleInitialize(NULL);
}
~OleInitClass() {
OleUninitialize();
}
};
// This global class calls OleInitialize() at
// application startup, and calls OleUninitialize()
// at application exit.
OleInitClass g_OleInitClass;
- 編譯並執行程式。按一下按鈕並請注意,Excel 會啟動且出現一個訊息方塊,指出取得的視窗控制代碼在
ShowWindow API 中被當做範例使用。按一下 [確定] 關閉訊息方塊,然後結束 Excel。
如需詳細資訊,請按一下下面的文件編號,檢視「Microsoft
知識庫」中的文件:
178749?
(http://support.microsoft.com/kb/178749/
)
How to create an automation project using MFC and a type library
183009?
(http://support.microsoft.com/kb/183009/
)
How To Enumerate Windows Using the WIN32 API
175030?
(http://support.microsoft.com/kb/175030/
)
How To Enumerate Applications Using Win32 APIs
179706?
(http://support.microsoft.com/kb/179706/
)
How to use MFC to automate Excel and create and format a new workbook