代碼運行效果圖如下:
介紹
有時我們並不需要提供完整的腳本支持,就像本文所要介紹的ActiveX腳本宿主(script hosting,可能稱為腳本引擎更好理解),本文提供的ScriptControlMacro程序展示了使用Microsoft腳本控件簡單地實現腳本支持功能,代碼中還包括了以下一些技術:
用文件映射實現只允許一個應用實例.
如何用MFC存儲讀取二進制注冊表數據
如何保存恢復應用程序窗口尺寸,位置
如何在CEditView派生類中使用不同地字體
如何捕捉OLE異常並顯示錯誤信息
等...
開始前的准備
這篇文章假定你已經對COM、ActiveX控件、OLE自動化有所熟悉,並知道如何在MFC裡使用他們。因為這些技術包含了相當多的內容,因此你應該先學習一下這些內容再來看本文。
基本步驟:
1、建立一個提供ActiveX控件支持的新的MFC工程
2、使用ClassWizard從腳本控件類型庫中建立一個dispatch類
ClassWizard將為腳本控件接口生成頭文件與執行文件。
// Machine generated IDispatch wrapper class(es) created with
// ClassWizard
////////////////////////////////////////////////////////////
// IScriptControl wrapper class
class IScriptControl : public COleDispatchDriver
{
// Operations
public:
void SetLanguage(LPCTSTR lpszNewValue);
void SetSitehWnd(HWND hWnd);
LPDISPATCH GetError();
void AddObject(LPCTSTR Name, LPDISPATCH Object,
BOOL AddMembers);
void AddCode(LPCTSTR Code);
};
// Machine generated IDispatch wrapper class(es) created
// with ClassWizard
#include "stdafx.h"
#include "MSScriptControl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////
// IScriptControl operations
void IScriptControl::SetLanguage(LPCTSTR lpszNewValue)
{
static BYTE parms[] =
VTS_BSTR;
InvokeHelper(0x5dc, DISPATCH_PROPERTYPUT, VT_EMPTY,
NULL, parms,
lpszNewValue);
}
void IScriptControl::SetSitehWnd(HWND hWnd)
{
static BYTE parms[] =
VTS_I4;
InvokeHelper(0x5de, DISPATCH_PROPERTYPUT, VT_EMPTY,
NULL, parms,
hWnd);
}
LPDISPATCH IScriptControl::GetError()
{
LPDISPATCH result;
InvokeHelper(0x5e3, DISPATCH_PROPERTYGET, VT_DISPATCH,
(void*)&result, NULL);
return result;
}
void IScriptControl::AddObject(LPCTSTR Name,
LPDISPATCH Object,
BOOL AddMembers)
{
static BYTE parms[] =
VTS_BSTR VTS_DISPATCH VTS_BOOL;
InvokeHelper(0x9c4, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
Name, Object, AddMembers);
}
void IScriptControl::AddCode(LPCTSTR Code)
{
static BYTE parms[] =
VTS_BSTR;
InvokeHelper(0x7d0, DISPATCH_METHOD, VT_EMPTY, NULL, parms,
Code);
}
3、刪除類中不需要的IDispatch封裝的方法
4、如果你想自己添加函數,可以用ClassWizard添加一個自動化類。
ClasWizard將為IDispatch接口產生如下頭文件與執行文件:
// ScriptControlMacroDispatch.h : interface of the
// CScriptControlMacroDispatch class
//
////////////////////////////////////////////////////////////
#if !defined(AFX_SCRIPTCONTROLMACRODISPATCH_H__FB55B5AF_
00E5_47F5_B176_214B2C7BF19A__INCLUDED_)
#define AFX_SCRIPTCONTROLMACRODISPATCH_H__FB55B5AF_00E5_
47F5_B176_214B2C7BF19A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
////////////////////////////////////////////////////////////
// CScriptControlMacroDispatch command target
class CScriptControlMacroDispatch : public CCmdTarget
{
DECLARE_DYNCREATE(CScriptControlMacroDispatch)
CScriptControlMacroDispatch();
// protected constructor used by dynamic creation
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CScriptControlMacroDispatch)
//}}AFX_VIRTUAL
// Implementation
protected:
//friend class CScriptControlMacroView;
// Generated message map functions
//{{AFX_MSG(CScriptControlMacroDispatch)
// NOTE - the ClassWizard will add and remove member
// functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
// Generated OLE dispatch map functions
//{{AFX_DISPATCH(CScriptControlMacroDispatch)
afx_msg void Test1();
afx_msg void Test2();
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
};
// Note: we add support for IID_IScriptControlMacroDispatch
// to support typesafe binding from VBA. This IID must match
// the GUID that is attached to the dispinterface in the
// .ODL file.
// {69AA5686-41AF-4CD9-AEAE-9DB88130E7C1}
static const IID IID_IScriptControlMacroDispatch =
{0x69AA5686, 0x41AF, 0x4CD9, {0xAE, 0xAE, 0x9D, 0xB8,
0x81, 0x30, 0xE7, 0xC1}};
////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations
// immediately before the previous line.
#endif // !defined(AFX_SCRIPTCONTROLMACRODISPATCH_H__
// FB55B5AF_00E5_47F5_B176_214B2C7BF19A__INCLUDED_)
// ScriptControlMacroDispatch.cpp : implementation of the
// CScriptControlMacroDispatch class
//
#include "stdafx.h"
#include "ScriptControlMacroDispatch.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////
// CScriptControlMacroDispatch
IMPLEMENT_DYNCREATE(CScriptControlMacroDispatch, CCmdTarget)
CScriptControlMacroDispatch::CScriptControlMacroDispatch()
{
EnableAutomation();
}
BEGIN_MESSAGE_MAP(CScriptControlMacroDispatch, CCmdTarget)
//{{AFX_MSG_MAP(CScriptControlMacroDispatch)
// NOTE - the ClassWizard will add and remove mapping
// macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BEGIN_DISPATCH_MAP(CScriptControlMacroDispatch, CCmdTarget)
//{{AFX_DISPATCH_MAP(CScriptControlMacroDispatch)
DISP_FUNCTION(CScriptControlMacroDispatch,
"Test1", Test1, VT_EMPTY, VTS_NONE)
DISP_FUNCTION(CScriptControlMacroDispatch,
"Test2", Test2, VT_EMPTY, VTS_NONE)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
BEGIN_INTERFACE_MAP(CScriptControlMacroDispatch, CCmdTarget)
INTERFACE_PART(CScriptControlMacroDispatch,
IID_IScriptControlMacroDispatch, Dispatch)
END_INTERFACE_MAP()
////////////////////////////////////////////////////////////
// CScriptControlMacroDispatch message handlers
void CScriptControlMacroDispatch::Test1()
{
// TODO: Add your dispatch handler code here
AfxMessageBox(CString(_T("\"")) + GetDispatchMap()->
lpEntries->lpszName + _T("\" method call of
the \"") + RUNTIME_CLASS(
CScriptControlMacroDispatch)->m_lpszClassName
+ _T("\" class"), MB_ICONASTERISK);
}
void CScriptControlMacroDispatch::Test2()
{
// TODO: Add your dispatch handler code here
AfxMessageBox(CString(_T("\"")) + GetDispatchMap()->
lpEntries[1].lpszName + _T("\" method
call of the \"") + RUNTIME_CLASS(
CScriptControlMacroDispatch)->m_lpszClassName
+ _T("\" class"), MB_ICONASTERISK);
}
5、這樣在產生的代碼中已經實現了一些自定義的東西
a. 不必要的聲名和代碼已經刪除
b. 全局的應用程序對象已經聲名:extern CScriptControlMacroApp theApp
c. MFC隱含函數聲名已經添加:CString AFXAPI AfxStringFromCLSID( REFCLSID ).
d. 為了使用AfxStringFromCLSID,IID_IScriptControlMacroDispatch定義已經移到了頭文件中
6、為了在所有MFC程序中方便地提供Unicode支持,在AFX.H頭文件中作了如下定制:
////////////////////////////////////////////////////////////
// Win32 libraries
// Start of customization
#ifdef _UNICODE
#pragma comment(linker, "/entry:wWinMainCRTStartup")
#endif
// End of customization
7、為了在所有MFC工程中使用 _WIN32_WINDOWS=0x500 這個預定義,在AFXV_W32.H頭文件中作了如下定制:
#ifndef ALL_WARNINGS
#pragma warning(disable: 4201)
// winnt.h uses nameless structs
#endif
// Start of customization
#ifndef _WIN32_WINDOWS
// End of customization
#define _WIN32_WINDOWS 0x0500
// Start of customization
#endif
// End of customization
本文配套源碼