IE窗口移动时的句柄 - Visual C ++

时间:2009-09-02 18:13:36

标签: internet-explorer visual-c++ com atl bho

我想处理窗口移动事件。我的第一次尝试是处理DWebBrowserEvents2::WindowSetHeightWindowSetLeftWindowSetTopWindowSetWidth事件,但由于某些原因,这些事件根本没有被解雇。我通过处理HTMLWindowEvents2::onresize来处理窗口大小调整。当窗户移动时我该如何处理?

我也尝试过实现IHTMLOMWindowServices,但是当窗口位置发生变化时,不会调用其成员。对于什么是一个好的接口实现有任何建议吗?我正在使用Visual C ++中的ATL创建IE扩展。

3 个答案:

答案 0 :(得分:0)

HTML窗口不是正确的地方。您需要升级到浏览器窗口级别,例如:

            // Get a handle for the IE window and set a hook for the window resize/move events
        m_spWebBrowser->get_HWND(reinterpret_cast<SHANDLE_PTR*>(&m_pWebBrowserHwnd));
        if(m_pWebBrowserHwnd)
        {
            int idHook = WH_CALLWNDPROC; // Type of hook: all Window calls
            HOOKPROC hookProc = MyClass::OnWindowMessage; // Procedure to be called
            HINSTANCE hMod = NULL; // DLL to monitor
            DWORD dwThreadId = GetWindowThreadProcessId(m_pWebBrowserHwnd,NULL); // IE Thread to monitor
            m_ResizeHook = SetWindowsHookEx(idHook, hookProc, hMod, dwThreadId); 
            Trace (L"Hook for window move/resize is set up");
        }

然后onWindowMessage处理正确的消息:

LRESULT CALLBACK MyClass::OnWindowMessage(int nCode, WPARAM wParam, LPARAM lParam)
{

LRESULT result = CallNextHookEx(NULL, nCode, wParam, lParam);

if( (lParam!=NULL) && (nCode == HC_ACTION))
{
    CWPSTRUCT *CwpStruct = (CWPSTRUCT *) lParam;
    switch(CwpStruct->message)
    {
    case WM_MOVE:
        HWND manipluatedHWND = CwpStruct->hwnd;
            //Code to execute when window moves
        break;
    }
}

享受:)

答案 1 :(得分:0)

好的,这是一些粗略的代码

    class ATL_NO_VTABLE CHelloWorldBHO :
       public CComObjectRootEx<CComSingleThreadModel>,
       public CComCoClass<CHelloWorldBHO, &CLSID_HelloWorldBHO>,
       public IObjectWithSiteImpl<CHelloWorldBHO>,
       public IDispatchImpl<IHelloWorldBHO, &IID_IHelloWorldBHO, &LIBID_HelloWorldLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
       public IDispEventImpl<1, CHelloWorldBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>, 
      //Other interfaces here
{

             BEGIN_SINK_MAP(CHelloWorldBHO)
                SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)//Do stuff OnDocumentComplete
             END_SINK_MAP()

         void STDMETHODCALLTYPE OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL){
             //Do other stuff here
             SetUpWindowsHook();//This calls a function that sets a window hook, similar to the code that you've posted
         }

}

答案 2 :(得分:0)

使用Timer处理这种情况。不太优雅,但有效!