MFC OCX 事件 / 属性 / 接口参数相关小结
1、事件
1.1 事件的添加
控件的事件一般都是由对外的接口引发。事件应该是属于窗口的,所以在Ctrl类上单击右键-》添加。
事件函数的名字就是事件名称,参数就是在添加事件时候设置的参数。控件里边通过参数将结果给了事件函数,从而将值传出。
参考链接:http://blog.csdn.net/wd_cloud/article/details/40893033
注:事件函数原型不能有返回值,应当为void。否则js调用时会引发ie崩溃的可能。
1.2 子线程中事件的触发传出
//define a custom message:
a) #define WM_THREADFIREEVENT WM_USER+101
b) ON_MESSAGE(WM_THREADFIREEVENT,OnFireEventForThread) //custom handler
c) LRESULT CFireeventCtrl::OnFireEventForThread(WPARAM wParam, LPARAM lParam)
{
FireLengthyProcessDone();
return TRUE;
}
d) 线程内:PostMessage(pCtrl->m_hWnd, WM_THREADFIREEVENT, (WPARAM)NULL, (LPARAM)NULL);
上述的hWnd,如果在IE使用空间,这个窗口句柄是空的,会导致回调照样失败,这时候需要重载OnSetClientSize。
e) void CMyControl::OnSetClientSite()
{
// It doesn't matter who the parent window is or what the size of
// the window is because the control's window will be reparented
// and resized correctly later when it's in-place activated.
if (m_pClientSite)
VERIFY (CreateControlWindow (::GetDesktopWindow(), CRect(0,0,0,0), CRect(0,0,0,0)));
COleControl::OnSetClientSite();
}
参考链接:http://www.cnblogs.com/lidabo/archive/2012/12/12/2815059.html
https://support.microsoft.com/en-us/help/200839/how-to-enable-activex-control-event-handling-on-a-web-page
2、接口参数
2.1 byte[]数组的传递:SAFEARRAY - > VARIANT
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
///将图像内存数据封装为接口需要的VARIANT类型 VARIANT pImgBuffer; SAFEARRAY *psa = NULL; SAFEARRAYBOUND rgsabound; VariantInit(&pImgBuffer); rgsabound.cElements = bytes; rgsabound.lLbound = 0; psa = SafeArrayCreate(VT_UI1, 1, &rgsabound); if ( psa == NULL ) return; BYTE *pBitmapData = NULL; //new BYTE[bytes];(不需要new) SafeArrayAccessData(psa, (void **)&pBitmapData); memcpy(pBitmapData, tempBuffer, bytes); ///将SAFEARRAY放入VARIANT中,并设置类型 pImgBuffer.vt = VT_ARRAY | VT_UI1; SafeArrayCopy(psa, &pImgBuffer.parray); SafeArrayUnaccessData(psa); //这里也就不需要使用delete释放pBitmapData所指向的空间 //YOUR_INTERFACE(pImgBuffer/*VARIANT*/, img.GetHeight(), img.GetWidth()); if ( tempBuffer ) delete[] tempBuffer; tempBuffer = NULL; SafeArrayDestroy(psa);
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//VARIANT* pPixArray;(接口参数) unsigned char* pBuffer = NULL; SafeArrayAccessData(pPixArray.parray, (void**)&pBuffer); ///此处使用pBuffer所指向的数据 SafeArrayUnaccessData(pPixArray.parray); HRESULT ret = VariantClear(&pPixArray);
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
FileInfo fi = new FileInfo("GrayPixelsBuf"); byte[] buf = new byte[fi.Length]; FileStream fs = fi.OpenRead(); fs.Read(buf, 0, Convert.ToInt32(fs.Length)); fs.Close(); ////buf传递给ocx////////
参考链接:http://blog.csdn.net/xdg_blog/article/details/53169852