1. 程式人生 > >MFC OCX 事件 / 屬性 / 接口參數相關小結

MFC OCX 事件 / 屬性 / 接口參數相關小結

don ini tde 實現 byte etc idt 分享 info

1、事件

1.1 事件的添加

控件的事件一般都是由對外的接口引發。事件應該是屬於窗口的,所以在Ctrl類上單擊右鍵-》添加。

  事件函數的名字就是事件名稱,參數就是在添加事件時候設置的參數。控件裏邊通過參數將結果給了事件函數,從而將值傳出。

參考鏈接:http://blog.csdn.net/wd_cloud/article/details/40893033

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

技術分享
///將圖像內存數據封裝為接口需要的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);
c++調用中參數封裝 技術分享
//VARIANT* pPixArray;(接口參數)
unsigned char* pBuffer = NULL;
SafeArrayAccessData(pPixArray.parray, (void**)&pBuffer);
///此處使用pBuffer所指向的數據
SafeArrayUnaccessData(pPixArray.parray);
HRESULT ret = VariantClear(&pPixArray);
ocx接口內實現 技術分享
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////////
c#中數據傳遞

參考鏈接:http://blog.csdn.net/xdg_blog/article/details/53169852

MFC OCX 事件 / 屬性 / 接口參數相關小結