feilanglove6

闲静少言,不慕荣利。好交友,不求回报;好技术,每有会意,便写文章自娱。喜编程,善vc,但无好看之界面之引擎,好vc之人,无不知mfc,然界面丑陋之极点,偶遇duilib,甚是喜爱,技艺不精,希勿笑。   
        困扰已久,几经搜寻,终于在codeproject 上面搜到一篇好文,甚喜,几经参摹,现列举成文,希有同需而不得者共之。
原文帖:http://www.codeproject.com/Articles/9014/Understanding-COM-Event-Handling
备用帖:链接: http://pan.baidu.com/s/1geg2hxh 密码: gkr3
要知详细如何,且down源码细研之。down url:链接: http://pan.baidu.com/s/1jH2GFvo 密码: 4vha
       1.我加载的是串口的mscomm32.ocx 下载地址,百度一搜一大把
       2.下载下来先注册activex,运行-》regsvr32 activex文件名及路径。
       3.创建duilib项目。现分类复制粘帖
    duidemo.xml 

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="607,488" caption="0,0,0,90">
    <VerticalLayout width="400" height="400" bkimage="Form_WelcomeImage.png">
        <HorizontalLayout height="57">
            <HorizontalLayout width="57" />
            <HorizontalLayout />
            <HorizontalLayout width="66">
                <Button name="btnclose" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="close.png" pushedimage="file=&apos;close.png&apos; fade=&apos;51&apos;" />
            </HorizontalLayout>
        </HorizontalLayout>
        <HorizontalLayout>
            <HorizontalLayout bordersize="1" bordercolor="#000000FF">
                <ActiveX name="wmplayer" visible="false" width="50" height="50" clsid="{648A5600-2C6E-101B-82B6-000000000014}" delaycreate="false" />
                <RichEdit name="REditMsg" autovscroll="true" autohscroll="true" />
            </HorizontalLayout>
            <VerticalLayout>
                <VerticalLayout width="304" height="173">
                    <Button name="open" text="打开" float="true" pos="53,29,0,0" width="60" height="30" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="btn_NumImage.png" pushedimage="btn_NumImageClick.png" />
                    <Button name="close" text="关闭" float="true" pos="196,29,0,0" width="60" height="30" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="btn_NumImage.png" pushedimage="btn_NumImageClick.png" />
                    <Button name="start" text="开始扫描" float="true" pos="53,93,0,0" width="60" height="30" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="btn_NumImage.png" pushedimage="btn_NumImageClick.png" />
                    <Button name="stop" text="结束扫描" float="true" pos="196,93,0,0" width="60" height="30" textcolor="#FF000000" disabledtextcolor="#FFA7A6AA" align="center" normalimage="btn_NumImage.png" pushedimage="btn_NumImageClick.png" />
                </VerticalLayout>
                <HorizontalLayout />
            </VerticalLayout>
        </HorizontalLayout>
    </VerticalLayout>
</Window>
2. MainFrame.h
#import "C:\\Windows\\SysWOW64\\mscomm32.ocx" raw_interfaces_only, raw_native_types, no_namespace, named_guids 
class CMainFrame;
typedef TEventHandler<CMainFrame, IMSComm, DMSCommEvents> IEventFiringObjectEventHandler;
... 
HRESULT OnEventFiringObjectInvoke
(
IEventFiringObjectEventHandler* pEventHandler,
DISPID dispidMember, 
REFIID riid,
LCID lcid, 
WORD wFlags, 
DISPPARAMS* pdispparams, 
VARIANT* pvarResult,
EXCEPINFO* pexcepinfo, 
UINT* puArgErr
); 
 ...
CActiveXUI* m_Activex;
CRichEditUI* m_txtUI;
IMSComm* m_pComm;
DWORD m_Cookie;
IEventFiringObjectEventHandler* m_spIEventDMSEventHandler; 

3.MainFrame.cpp
m_Activex = static_cast<CActiveXUI*>(m_PaintManager.FindControl(_T("wmplayer")));
if(m_Activex)
{
m_Activex->GetControl(IID_IMSComm,(void**)&m_pComm);
//DMSCommEvents
if(m_pComm)
ConnectEvents();

...
m_spIEventDMSEventHandler = new IEventFiringObjectEventHandler(*this,m_pComm,&CMainFrame::OnEventFiringObjectInvoke);
...
 HRESULT CMainFrame::OnEventFiringObjectInvoke( IEventFiringObjectEventHandler* pEventHandler, DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult, EXCEPINFO* pexcepinfo, UINT* puArgErr )
{
VARIANT variant_inp;
COleSafeArray safearray_inp;
short psCommEvent = 0;
long varBufLen = 0;
byte varRxdata[256]; //设置BYTE数组
CDuiString varShowMsg(_T(""));
wstring m_strShow(_T(""));
 
if (dispidMember == 0x01)  // Event1 event.
{
m_pComm->get_CommEvent(&psCommEvent);
if(psCommEvent == 2)
{
Sleep(1000);
m_pComm->get_Input(&variant_inp);
safearray_inp = variant_inp;
varBufLen = safearray_inp.GetOneDimSize();
for(long i = 0;i < varBufLen;i ++)
{
safearray_inp.GetElement(&i,varRxdata+i);
}
for(int i = 0;i < varBufLen;i ++)
{
char var = *(char*)(varRxdata+i);
m_Revbuf[i] = var;
varShowMsg.Format(_T("%c"),var);
m_strShow += varShowMsg.GetData();
}
}
m_strShow += _T("\r\n");
//wchar_t* ptr = const_cast<wchar_t*>(m_strShow.data());
m_txtUI->AppendText(m_strShow.c_str());
}
 
return S_OK;
}
4, EventHandle.h
 #pragma once
namespace TEventHandlerNamespace
{
// Generic event handler template class (especially useful (but not limited to) for non-ATL clients).
template <class event_handler_class, typename device_interface, typename device_event_interface>
class TEventHandler : IDispatch
{
friend class class_event_handler;
 
typedef HRESULT (event_handler_class::*parent_on_invoke)
(
TEventHandler<event_handler_class, device_interface, device_event_interface>* pthis,
DISPID dispidMember, 
REFIID riid,
LCID lcid, 
WORD wFlags, 
DISPPARAMS* pdispparams, 
VARIANT* pvarResult,
EXCEPINFO* pexcepinfo, 
UINT* puArgErr
);
 
public :
TEventHandler
(
event_handler_class& parent,
device_interface* pdevice_interface,  // Non-ref counted.
parent_on_invoke parent_on_invoke_function
) :
m_cRef(1),
m_parent(parent),
m_parent_on_invoke(parent_on_invoke_function),
m_pIConnectionPoint(0),
m_dwEventCookie(0)
{
SetupConnectionPoint(pdevice_interface);
}
 
~TEventHandler()
{
// Call ShutdownConnectionPoint() here JUST IN CASE connection points are still 
// alive at this time. They should have been disconnected earlier.
ShutdownConnectionPoint();
}
 
STDMETHOD_(ULONG, AddRef)()
{
InterlockedIncrement(&m_cRef);
 
return m_cRef;  
}
 
STDMETHOD_(ULONG, Release)()
{
InterlockedDecrement(&m_cRef);
 
if (m_cRef == 0)
{
delete this;
return 0;
}
 
return m_cRef;
}
 
STDMETHOD(QueryInterface)(REFIID riid, void ** ppvObject)
{
if (riid == IID_IUnknown)
{
*ppvObject = (IUnknown*)this;
AddRef();
return S_OK;
}
 
if ((riid == IID_IDispatch) || (riid == __uuidof(device_event_interface)))
{
*ppvObject = (IDispatch*)this;
AddRef();
return S_OK;
}
 
return E_NOINTERFACE;
}
 
STDMETHOD(GetTypeInfoCount)(UINT* pctinfo)
{
return E_NOTIMPL;
}
 
STDMETHOD(GetTypeInfo)(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
{
return E_NOTIMPL;
}
 
STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames,
LCID lcid, DISPID* rgdispid)
{
return E_NOTIMPL;
}
 
STDMETHOD(Invoke)(DISPID dispidMember, REFIID riid,
LCID lcid, WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
EXCEPINFO* pexcepinfo, UINT* puArgErr)
{
return (m_parent.*m_parent_on_invoke)(this, dispidMember, riid, lcid, wFlags, pdispparams, pvarResult, pexcepinfo, puArgErr);
}
 
protected :
LONGm_cRef;
 
// Pertaining to the owner of this object.
event_handler_class&m_parent;  // Non-reference counted. This is to prevent circular references.
 
// Pertaining to connection points.
IConnectionPoint*m_pIConnectionPoint;  // Ref counted of course.
DWORDm_dwEventCookie;
parent_on_invokem_parent_on_invoke;
 
void SetupConnectionPoint(device_interface* pdevice_interface)
{
IConnectionPointContainer*pIConnectionPointContainerTemp = NULL;
IUnknown*pIUnknown = NULL;
 
// QI this object itself for its IUnknown pointer which will be used 
// later to connect to the Connection Point of the device_interface object.
this -> QueryInterface(IID_IUnknown, (void**)&pIUnknown);
 
if (pIUnknown)
{
// QI the pdevice_interface for its connection point.
pdevice_interface -> QueryInterface (IID_IConnectionPointContainer, (void**)&pIConnectionPointContainerTemp);
 
if (pIConnectionPointContainerTemp)
{
pIConnectionPointContainerTemp -> FindConnectionPoint(__uuidof(device_event_interface), &m_pIConnectionPoint);
pIConnectionPointContainerTemp -> Release();
pIConnectionPointContainerTemp = NULL;
}
 
if (m_pIConnectionPoint)
{
m_pIConnectionPoint -> Advise(pIUnknown, &m_dwEventCookie);
}
 
pIUnknown -> Release();
pIUnknown = NULL;
}
}
 
public :
 
void ShutdownConnectionPoint()
{
if (m_pIConnectionPoint)
{
m_pIConnectionPoint -> Unadvise(m_dwEventCookie);
m_dwEventCookie = 0;
m_pIConnectionPoint -> Release();
m_pIConnectionPoint = NULL;
}
}
};
 
};

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2021-10-29
  • 2021-09-07
猜你喜欢
  • 2022-01-15
  • 2022-12-23
  • 2021-05-18
  • 2021-06-23
  • 2021-09-17
  • 2021-11-04
  • 2021-05-21
相关资源
相似解决方案