许多ActiveX控件,比如:ActiveMovie, IE工具条中的Stop按钮对于他们来说有特殊的意义。当Stop按钮下按时这些控件可以停止播放背景音乐或动画。这篇文章将告诉你在ActiveX控件中添加IOleCommandTarget接口来捕捉Stop 按钮的动作。
详细内容
为了支持IOleCommandTarget接口你必须手工在COleControl派生类的.h 和.cpp文件中添加如下代码。然后,你可以在IOleCommandTarget::Exec()函数中捕获OLECMDID_STOP命令id (IE工具条中Stop 按钮的id)。具体代码如下:
// COleControl派生类的.***件:
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;
} |