1 // MfcControlCtl.cpp : Implementation of the CMfcControlCtrl ActiveX Control class.
2 
3 #include "stdafx.h"
4 #include "MfcControl.h"
5 #include "MfcControlCtl.h"
6 #include "MfcControlPpg.h"
7 
8 
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
14 
15 
16 IMPLEMENT_DYNCREATE(CMfcControlCtrl, COleControl)
17 
18 
19 /////////////////////////////////////////////////////////////////////////////
20 // Message map
21 
22 BEGIN_MESSAGE_MAP(CMfcControlCtrl, COleControl)
23 	//{{AFX_MSG_MAP(CMfcControlCtrl)
24 	// NOTE - ClassWizard will add and remove message map entries
25 	//    DO NOT EDIT what you see in these blocks of generated code !
26 	//}}AFX_MSG_MAP
27 	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
28 END_MESSAGE_MAP()
29 
30 
31 /////////////////////////////////////////////////////////////////////////////
32 // Dispatch map
33 
34 BEGIN_DISPATCH_MAP(CMfcControlCtrl, COleControl)
35 	//{{AFX_DISPATCH_MAP(CMfcControlCtrl)
36 	DISP_FUNCTION(CMfcControlCtrl, "inShort", inShort, VT_I2, VTS_I2)
37 	DISP_FUNCTION(CMfcControlCtrl, "inLong", inLong, VT_I4, VTS_I4)
38 	DISP_FUNCTION(CMfcControlCtrl, "inString", inString, VT_BSTR, VTS_PBSTR)
39 	DISP_FUNCTION(CMfcControlCtrl, "inFloat", inFloat, VT_R4, VTS_R4)
40 	DISP_FUNCTION(CMfcControlCtrl, "inDouble", inDouble, VT_R8, VTS_R8)
41 	DISP_FUNCTION(CMfcControlCtrl, "inVariant", inVariant, VT_VARIANT, VTS_VARIANT)
42 	DISP_FUNCTION(CMfcControlCtrl, "inObject", inObject, VT_DISPATCH, VTS_DISPATCH)
43 	DISP_FUNCTION(CMfcControlCtrl, "outShort", outShort, VT_EMPTY, VTS_PI2)
44 	DISP_FUNCTION(CMfcControlCtrl, "outLong", outLong, VT_EMPTY, VTS_PI4)
45 	DISP_FUNCTION(CMfcControlCtrl, "outString", outString, VT_EMPTY, VTS_PBSTR)
46 	DISP_FUNCTION(CMfcControlCtrl, "outFloat", outFloat, VT_EMPTY, VTS_PR4)
47 	DISP_FUNCTION(CMfcControlCtrl, "outDouble", outDouble, VT_EMPTY, VTS_PR8)
48 	DISP_FUNCTION(CMfcControlCtrl, "outVariant", outVariant, VT_EMPTY, VTS_PVARIANT)
49 	DISP_FUNCTION(CMfcControlCtrl, "outObject", outObject, VT_EMPTY, VTS_PDISPATCH)
50 	//}}AFX_DISPATCH_MAP
51 END_DISPATCH_MAP()
52 
53 
54 /////////////////////////////////////////////////////////////////////////////
55 // Event map
56 
57 BEGIN_EVENT_MAP(CMfcControlCtrl, COleControl)
58 	//{{AFX_EVENT_MAP(CMfcControlCtrl)
59 	// NOTE - ClassWizard will add and remove event map entries
60 	//    DO NOT EDIT what you see in these blocks of generated code !
61 	//}}AFX_EVENT_MAP
62 END_EVENT_MAP()
63 
64 
65 /////////////////////////////////////////////////////////////////////////////
66 // Property pages
67 
68 // TODO: Add more property pages as needed.  Remember to increase the count!
69 BEGIN_PROPPAGEIDS(CMfcControlCtrl, 1)
70 	PROPPAGEID(CMfcControlPropPage::guid)
71 END_PROPPAGEIDS(CMfcControlCtrl)
72 
73 
74 /////////////////////////////////////////////////////////////////////////////
75 // Initialize class factory and guid
76 
77 IMPLEMENT_OLECREATE_EX(CMfcControlCtrl, "MFCCONTROL.MfcControlCtrl.1",
78 	0xac221fb6, 0xa0d8, 0x11d4, 0x83, 0x3b, 0, 0x50, 0x4, 0x52, 0x6a, 0xb4)
79 
80 
81 /////////////////////////////////////////////////////////////////////////////
82 // Type library ID and version
83 
84 IMPLEMENT_OLETYPELIB(CMfcControlCtrl, _tlid, _wVerMajor, _wVerMinor)
85 
86 
87 /////////////////////////////////////////////////////////////////////////////
88 // Interface IDs
89 
90 const IID BASED_CODE IID_DMfcControl =
91 		{ 0xac221fb4, 0xa0d8, 0x11d4, { 0x83, 0x3b, 0, 0x50, 0x4, 0x52, 0x6a, 0xb4 } };
92 const IID BASED_CODE IID_DMfcControlEvents =
93 		{ 0xac221fb5, 0xa0d8, 0x11d4, { 0x83, 0x3b, 0, 0x50, 0x4, 0x52, 0x6a, 0xb4 } };
94 
95 
96 /////////////////////////////////////////////////////////////////////////////
97 // Control type information
98 
99 static const DWORD BASED_CODE _dwMfcControlOleMisc =
100 	OLEMISC_ACTIVATEWHENVISIBLE |
101 	OLEMISC_SETCLIENTSITEFIRST |
102 	OLEMISC_INSIDEOUT |
103 	OLEMISC_CANTLINKINSIDE |
104 	OLEMISC_RECOMPOSEONRESIZE;
105 
106 IMPLEMENT_OLECTLTYPE(CMfcControlCtrl, IDS_MFCCONTROL, _dwMfcControlOleMisc)
107 
108 
109 /////////////////////////////////////////////////////////////////////////////
110 // CMfcControlCtrl::CMfcControlCtrlFactory::UpdateRegistry -
111 // Adds or removes system registry entries for CMfcControlCtrl
112 
113 BOOL CMfcControlCtrl::CMfcControlCtrlFactory::UpdateRegistry(BOOL bRegister)
114 {
115 	// TODO: Verify that your control follows apartment-model threading rules.
116 	// Refer to MFC TechNote 64 for more information.
117 	// If your control does not conform to the apartment-model rules, then
118 	// you must modify the code below, changing the 6th parameter from
119 	// afxRegApartmentThreading to 0.
120 
121 	if (bRegister)
122 		return AfxOleRegisterControlClass(
123 			AfxGetInstanceHandle(),
124 			m_clsid,
125 			m_lpszProgID,
126 			IDS_MFCCONTROL,
127 			IDB_MFCCONTROL,
128 			afxRegApartmentThreading,
129 			_dwMfcControlOleMisc,
130 			_tlid,
131 			_wVerMajor,
132 			_wVerMinor);
133 	else
134 		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
135 }
136 
137 
138 /////////////////////////////////////////////////////////////////////////////
139 // CMfcControlCtrl::CMfcControlCtrl - Constructor
140 
141 CMfcControlCtrl::CMfcControlCtrl()
142 {
143 	InitializeIIDs(&IID_DMfcControl, &IID_DMfcControlEvents);
144 
145 	// TODO: Initialize your control's instance data here.
146 }
147 
148 
149 /////////////////////////////////////////////////////////////////////////////
150 // CMfcControlCtrl::~CMfcControlCtrl - Destructor
151 
152 CMfcControlCtrl::~CMfcControlCtrl()
153 {
154 	// TODO: Cleanup your control's instance data here.
155 }
156 
157 
158 /////////////////////////////////////////////////////////////////////////////
159 // CMfcControlCtrl::OnDraw - Drawing function
160 
161 void CMfcControlCtrl::OnDraw(
162 			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
163 {
164 	// TODO: Replace the following code with your own drawing code.
165 	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
166 	pdc->Ellipse(rcBounds);
167 }
168 
169 
170 /////////////////////////////////////////////////////////////////////////////
171 // CMfcControlCtrl::DoPropExchange - Persistence support
172 
173 void CMfcControlCtrl::DoPropExchange(CPropExchange* pPX)
174 {
175 	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
176 	COleControl::DoPropExchange(pPX);
177 
178 	// TODO: Call PX_ functions for each persistent custom property.
179 
180 }
181 
182 
183 /////////////////////////////////////////////////////////////////////////////
184 // CMfcControlCtrl::OnResetState - Reset control to default state
185 
186 void CMfcControlCtrl::OnResetState()
187 {
188 	COleControl::OnResetState();  // Resets defaults found in DoPropExchange
189 
190 	// TODO: Reset any other control state here.
191 }
192 
193 
194 /////////////////////////////////////////////////////////////////////////////
195 // CMfcControlCtrl message handlers
196 
197 
198 short CMfcControlCtrl::inShort(short val)
199 {
200 	char buf[256];
201 	sprintf( buf, "inByte: value= %d", val);
202 	::MessageBoxA( NULL, buf, "MFCCONTROL.MfcControl", MB_OK);
203 	return val+1;
204 }
205 
206 long CMfcControlCtrl::inLong(long val)
207 {
208 	char buf[256];
209 	sprintf( buf, "inLong: value= %d", val);
210 	::MessageBoxA( NULL, buf, "MFCCONTROL.MfcControl", MB_OK);
211 	return val+1;
212 }
213 
214 BSTR CMfcControlCtrl::inString(BSTR* val)
215 {
216 	CString strResult;
217 	strResult= *val;
218 	char buf[256];
219 	sprintf( buf, "inString: value= %S", *val);
220 	::MessageBoxA( NULL, buf, "MFCCONTROL.MfcControl", MB_OK);
221 	strResult += L" an appended string";
222 	return strResult.AllocSysString();
223 }
224 
225 float CMfcControlCtrl::inFloat(float val)
226 {
227 	char buf[256];
228 	sprintf( buf, "inFloat: value= %f", val);
229 	::MessageBoxA( NULL, buf, "MFCCONTROL.MfcControl", MB_OK);
230 	return val+1;
231 }
232 
233 double CMfcControlCtrl::inDouble(double val)
234 {
235 	char buf[256];
236 	sprintf( buf, "inDouble: value= %g", val);
237 	::MessageBoxA( NULL, buf, "MFCCONTROL.MfcControl", MB_OK);
238 	return val+1;
239 }
240 
241 VARIANT CMfcControlCtrl::inVariant(const VARIANT FAR& val)
242 {
243 	VARIANT vaResult;
244 	VariantInit(&vaResult);
245 	VariantCopyInd( &vaResult, const_cast<VARIANT*>(&val));
246 	if( vaResult.vt == VT_BSTR)
247 	{
248 		char buf[256];
249 		sprintf( buf, "inVariant: value= %S", vaResult.bstrVal);
250 		::MessageBoxA( NULL, buf, "MFCCONTROL.MfcControl", MB_OK);
251 
252 	}
253 	return _variant_t( L" a string from CMfcControlCtrl::inVariant");
254 }
255 
256 LPDISPATCH CMfcControlCtrl::inObject(LPDISPATCH val)
257 {
258 	char buf[256];
259 	_bstr_t bstr;
260 	HRESULT hr= S_OK;
261 	COleVariant var;
262 	DISPID id;
263 	OLECHAR* name=L"prpString";
264 	if( SUCCEEDED(hr= val->GetIDsOfNames( IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id)))
265 	{
266 		DISPPARAMS params={0,0,0,0};
267 		hr= val->Invoke( id, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &params, &var,0,0);
268 
269 	}
270 
271 	if( var.vt== VT_BSTR)
272 		bstr= var.bstrVal;
273 	sprintf( buf, "inObject: value= %S", (wchar_t*)bstr);
274 	::MessageBoxA( NULL, buf, "MFCCONTROL.MfcControl", MB_OK);
275 
276 	return NULL;
277 }
278 
279 
280 void CMfcControlCtrl::outShort(short* val)
281 {
282 	*val= 123;
283 }
284 
285 void CMfcControlCtrl::outLong(long* val)
286 {
287 	*val= 1234;
288 }
289 
290 void CMfcControlCtrl::outString(BSTR FAR* val)
291 {
292 	*val= SysAllocString(L"A string from CMfcControlCtrl::outString ");
293 }
294 
295 void CMfcControlCtrl::outFloat(float* val)
296 {
297 	*val= 3.14f;
298 }
299 
300 void CMfcControlCtrl::outDouble(double* val)
301 {
302 	*val= 3.145;
303 }
304 
305 void CMfcControlCtrl::outVariant(VARIANT FAR* val)
306 {
307 	VariantInit( val);
308 	val->vt= VT_BSTR;
309 	val->bstrVal= SysAllocString( L"a string in a VARIANT");
310 }
311 
312 void CMfcControlCtrl::outObject(LPDISPATCH FAR* val)
313 {
314 	//{BFE10EBE-8584-11D4-005004526AB4}
315 	HRESULT hr= S_OK;
316 	CLSID clsTestControl;
317 	hr= CLSIDFromProgID( L"AxTestComponents.Basic", &clsTestControl);
318 
319 	IDispatch* pDisp= NULL;
320 	hr=	CoCreateInstance( clsTestControl, NULL, CLSCTX_ALL, __uuidof(IDispatch), (void**)&pDisp);
321 
322 	if( SUCCEEDED( hr) && val)
323 	{
324 		COleVariant var;
325 		DISPID id;
326 		OLECHAR* name=L"prpString";
327 		if( SUCCEEDED(hr= pDisp->GetIDsOfNames( IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id)))
328 		{
329 			COleVariant vaParam1(_T("this is property prpString of AxTestComponents.Basic"));
330 			DISPID dispidPut= DISPID_PROPERTYPUT;
331 			DISPPARAMS params;
332 			params.cArgs= 1;
333 			params.cNamedArgs= 1;
334 			params.rgdispidNamedArgs= &dispidPut;
335 			params.rgvarg= &vaParam1;
336 
337 			hr= pDisp->Invoke( id, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT, &params, &var,0,0);
338 			*val= pDisp;
339 		}
340 
341 	}
342 
343 }
344 // VT_I1
345