07-20-2007 11:40 AM
07-24-2007 03:53 AM
Okay, as finally I managed myself to find the answer I publish it in case it finally interests someone.
1) Yes it is possible to create NI object ActiveX programmatically without using visual resource editor. However the most appropriate way of doing that is not directly using underlying COM Api but OLE support within MFC and in particular CWnd class. Actually CNiGraph class is rather integrated is such way. Therefore here are the very few magic code lines:
int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd ::OnCreate(lpCreateStruct) == -1)
return -1;
CLSID clsid = CNiGraph::GetClsid();
if (!m_NiGraph.CreateControl(clsid, NULL, WS_VISIBLE, CRect(0,0,300,300), this, 1))
return -1;
return 0;
}
where m_NiGrah is a CNiGraph member of CChildView
NOTE: online reference documentation is incorrect as CNiGraph::GetClsid( ) and CNiGraph::CreateControl( ) are documented as protected method while they are public.
2) Yes the CLSID is the coclass CWGraph from OLE/COM Object Viewer. However the most appropriate way to get it is using static function CNiGraph::GetClsid(); As described in 1)
NOTE: online reference documentation is incorrect as CNiGraph::GetClsid( ) and CNiGraph::CreateControl( ) are documented as protected method while they are public.
3) Lets say that underlying interface is _DCWGraph implementing the COM standard IDispatch interface. Actually it is not required to have this information as CNiGraph wrapper do the job (see 1) code sample, QueryInterface( ) is not required)
4) See 1)
5) Event are used quite like usual MFC controls notifications excepted it uses underlying COM EventSink mecanisms. But once again all necessary standard wrappers and macros to ease implementation are available. The equivalent of the usual MESSAGE_MAP is the EVENTSINK_MAP. The most appropriate way to get the related needed information (like event dispatch Ids (equivalent of notification Ids), handler function prototype and parameter type descriprions) is to create a dummy dialog based project and to insert the control in the dialog box resources. Then using Visual studio facility to add message map handlers (through class wizard for example), the skeleton code with all necessary information to handle the event is automatically produced in h and cpp file of the dialog box. Finally just copy past it in the real project parent class window to get same behaviour.
That's it