zoukankan      html  css  js  c++  java
  • activex嵌入浏览器时线程触发事件

      在浏览器中嵌入activex后,线程中触发的事件就没有动静了,如果在调试的情况下,还能发现浏览器有非法错发生。而同样的activex如果使用应用程序来调用则正常。

    解决方法是取巧的方式,在线程中发出消息,控件响应消息后再FireEvent。

    1. 创建控件项目。
    2. 类向导, 使用 Add 方法将启动二线程并返回。 下面的代码显示方法启动二线程并立即返回 MFCActiveX 控件中。 全局函数以作为二线程工作函数还声明:
       LONG ThreadProc(LPVOID pParam);

    void CFireeventCtrl::StartLengthyProcess()
    {
    DWORD dwID;
    HANDLE threadHandle = CreateThread(NULL,NULL,
    (LPTHREAD_START_ROUTINE)ThreadProc,
    (LPVOID)this, NULL, &dwID);
    TRACE("Started the thread %x\n",dwID);
    }
    3. 添加任何要从使用类向导二线程激发事件。
    4. 定义要从二线程发送自定义邮件。 还, 将消息映射项添加到控件消息映射接收自定义消息时, 将调用消息处理函数。 此消息处理程序将触发需事件。 样本如何执行此 MFCActiveX 控件中的如下:
          //define a custom message:
    #define WM_THREADFIREEVENT WM_APP+101

    //add an entry for the message to the message map of the control
    BEGIN_MESSAGE_MAP(CFireeventCtrl, COleControl)
    //{{AFX_MSG_MAP(CFireeventCtrl)
    //}}AFX_MSG_MAP
    ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
    ON_MESSAGE(WM_THREADFIREEVENT,OnFireEventForThread) //custom handler
    END_MESSAGE_MAP()

    //add a handler for the custom message that will fire our event
    LRESULT CFireeventCtrl::OnFireEventForThread(WPARAM wParam,
    LPARAM lParam)
    {
    FireLengthyProcessDone();
    return TRUE;
    }
    5. 为二线程, 线程过程中当是二线程来触发事件, 时间张贴回主线程步骤 3 中定义自定义消息。 激发事件。 以下代码演示:
          LONG ThreadProc(LPVOID pParam)
    {
    Sleep(2000); //simulate lengthy processing
    CFireeventCtrl *pCtrl = (CFireeventCtrl*)pParam;
    PostMessage(pCtrl->m_hWnd,
    WM_THREADFIREEVENT,
    (WPARAM)NULL,
    (LPARAM)NULL);
    return TRUE;
    }

    同时因为浏览器貌似并不创建activex的窗口,所以还要增加创建窗口的代码。
    MFC 提供函数调用 COleControl::CreateControlWindow() 来创建控件窗口。 MFC 实现 IOleObject::SetClientSite() 调用 COleControl::OnSetClientSite()。 将以下到 - COleControl 派生类:
       // CMyControl is derived from COleControl.
    void CMyControl::OnSetClientSite()
    {
    if (m_pClientSite)
    // 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.
    VERIFY (CreateControlWindow (::GetDesktopWindow(), CRect(0,0,0,0),
    CRect(0,0,0,0)));
    COleControl::OnSetClientSite();
    }
    参考:
    http://topic.csdn.net/t/20040109/17/2650433.html#
    http://support.microsoft.com/kb/195188/zh-cn
    http://support.microsoft.com/kb/157437
  • 相关阅读:
    经常使用排序算法
    windows和Linux内存的对齐方式
    Oracle实现数据不存在则插入,数据存在则更新(insert or update)
    hysbz 2243 染色(树链剖分)
    HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
    逆序排列
    PHP盛宴——经常使用函数集锦
    怎样 TabHostFragment自己定义 tab键(indicator)
    不是IT圈人的IT创业优劣势!
    2星|汪丁丁《经济的限度》:访谈文字稿+几篇偏专业的文章,不适合无经济学专业背景知识的读者阅读
  • 原文地址:https://www.cnblogs.com/lidabo/p/2815204.html
Copyright © 2011-2022 走看看