title: Set focus to embedded MSHTML
body: <p>In my application I have an embedded MSHTML control. The problem is that I cannot set focus to this control. I can click, select, push buttons etc, but it won't accept keyboard input and mouse wheel notifications as a result. Even left-clicks on the control do not set the focus. (The question is related to <a href="https://stackoverflow.com/questions/296548/embedded-mshtml-mouse-wheel-ignored">this issue.</a>)</p>
  
  <p>Ultimately, I've tried 4 approaches. (Error handling is omitted here for brevity.) The first is sending WM_SETFOCUS to the control. </p>
  
  <pre><code>HWND iehwnd = ::FindWindowEx (m_oleObjectHWND, NULL, 
       L"Shell DocObject View", NULL);
  if(iehwnd != NULL)
  {
      iehwnd = ::FindWindowEx (iehwnd, NULL, L"Internet Explorer_Server", NULL);
      if(iehwnd != NULL)
      {
          ::PostMessage(iehwnd, WM_SETFOCUS, (WPARAM)GetHWND(), NULL);
      }
  }
  </code></pre>
  
  <p>Using Winspector, I made sure that the messages came through to the control's window, but it seems to ignore them.</p>
  
  <p>The second is calling the focus() method of IHtmlWindow2. </p>
  
  <pre><code>    IHTMLDocument2* pHTMLDoc2 = NULL;
      m_webBrowser-&gt;get_Document((IDispatch**)&amp;pHTMLDoc2);
  
      IHTMLWindow2* pWindow = 0;
      pHTMLDoc2-&gt;get_parentWindow(&amp;pWindow);
      pWindow-&gt;focus();
  </code></pre>
  
  <p>This time, I get E_FAIL from focus(). </p>
  
  <p>MSDN says that the call may fail when the document hasn't loaded, but pHTMLDoc2->get_readyState() returns "complete". So this isn't the case.</p>
  
  <p>The third is doing the UIACTIVATE verb.</p>
  
  <pre><code>    hret = m_oleObject-&gt;DoVerb(OLEIVERB_UIACTIVATE, NULL, 
          clientSite, 0, (HWND)GetHWND(), &amp;posRect);
  </code></pre>
  
  <p>This seems to achieve nothing.</p>
  
  <p>The fourth is calling focus() of IHtmlDocument4.</p>
  
  <pre><code>    IDispatch* pdisp = NULL;
      m_webBrowser-&gt;get_Document((IDispatch**)&amp;pdisp);
  
      IHTMLDocument4* pHTMLDoc4 = NULL;
      pdisp-&gt;QueryInterface(IID_IHTMLDocument4, (void**)(&amp;pHTMLDoc4));
  
      pHTMLDoc4-&gt;focus();
  </code></pre>
  
  <p>Does nothing.</p>
  
  <p>I've spent a full day on this already and know google's links by heart now :) Hoping for a hint from experts.</p>