許多ActiveX控件,比如:ActiveMovie, IE工具條中的Stop按鈕對於他們來說有特殊的意義。當Stop按鈕下按時這些控件可以停止播放背景音樂或動畫。這篇文章將告訴你在ActiveX控件中添加IOleCommandTarget接口來捕捉Stop 按鈕的動作。
詳細內容
為了支持IOleCommandTarget接口你必須手工在COleControl派生類的.h 和.cpp文件中添加如下代碼。然後,你可以在IOleCommandTarget::Exec()函數中捕獲OLECMDID_STOP命令id (IE工具條中Stop 按鈕的id)。具體代碼如下:
// COleControl派生類的.h文件:
class CMyOleControl : public COleControl
{
...
// Interface Maps
protected:
// Add the following to support the IOleCommandTarget interface.
// NOTE: Nested class name is called CmdTargetObj
DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(CmdTargetObj, IOleCommandTarget)
STDMETHOD(QueryStatus)(const GUID*, ULONG, OLECMD[], OLECMDTEXT*);
STDMETHOD(Exec)(const GUID*, DWORD, DWORD, VARIANTARG*,
VARIANTARG*);
END_INTERFACE_PART(CmdTargetObj)
};
//COleControl派生類的.cpp文件:
BEGIN_INTERFACE_MAP(CMyOleControl, COleControl)
INTERFACE_PART(CMyOleControl, IID_IOleCommandTarget, CmdTargetObj)
END_INTERFACE_MAP()
ULONG FAR EXPORT CMyOleControl::XCmdTargetObj::AddRef()
{
METHOD_PROLOGUE(CMyOleControl, CmdTargetObj)
return pThis->ExternalAddRef();
}
ULONG FAR EXPORT CMyOleControl::XCmdTargetObj::Release()
{
METHOD_PROLOGUE(CMyOleControl, CmdTargetObj)
return pThis->ExternalRelease();
}
HRESULT FAR EXPORT CMyOleControl::XCmdTargetObj::QueryInterface(
REFIID iid, void FAR* FAR* ppvObj)
{
METHOD_PROLOGUE(CMyOleControl, CmdTargetObj)
return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
}
STDMETHODIMP CMyOleControl::XCmdTargetObj::QueryStatus(
const GUID* pguidCmdGroup, ULONG cCmds, OLECMD rgCmds[],
OLECMDTEXT* pcmdtext)
{
METHOD_PROLOGUE(CMyOleControl, CmdTargetObj)
//... add YOUR own code here.
return S_OK;
}
STDMETHODIMP CMyOleControl::XCmdTargetObj::Exec(
const GUID* pguidCmdGroup, DWORD nCmdID, DWORD nCmdExecOpt,
VARIANTARG* pvarargIn, VARIANTARG* pvarargOut)
{
METHOD_PROLOGUE(CMyOleControl, CmdTargetObj)
if (nCmdID == OLECMDID_STOP)
{
// ... STOP button is clicked, add YOUR own code here.
// We just display a message box.
::MessageBox(NULL, "STOP","CMyOleControl", MB_OK);
}
return S_OK;
}