title: C++ CHTMLView element page loading and then disappearing
body: <p>In my C++ application, a CHTMLView is used to load some HTML into a web page. It works by writing the HTML to a temporary file, and then calling Navigate2() on the CHTMLView to navigate to the file location.</p>
  
  <p>What we are finding happens is that the navigation occurs, the file is written, the completely correct contents of the page appears, but then it quickly disappears and becomes blank. But it's a visual thing; right-clicking and saying "View Source" shows the correct source, and hovering over elements on the page that react to hovering make them appear again (but everything else stays white). Resizing the window or scrolling is the only way to make everything appear.</p>
  
  <p>I have tried navigating first to about:blank and then triggering a navigation to the correct place with a OnDocumentComplete() event. I have even tried navigating first to blank dummy page and then going from there. Nothing changes.</p>
  
  <p>Any advice?!</p>
  
  <p>The derived class is ScriptViewer.</p>
  
  <p>ScriptViewer.h</p>
  
  <pre><code>class CScriptViewer : public CHtmlView
  {
  protected:
      CScriptViewer();           // protected constructor used by dynamic creation
      DECLARE_DYNCREATE(CScriptViewer)
  
  // html Data
  public:
      //{{AFX_DATA(CScriptViewer)
          // NOTE: the ClassWizard will add data members here
      //}}AFX_DATA
  
  // Attributes
  public:
      CAMAgentDesktopDoc* m_pDoc;
      CScriptDlg* m_pDlg;
  
      CString strScriptLocation;
      BOOL m_bInitialLoad;
  
  // Operations
  public:
      void GetAllValues( map&lt;CString,CString&gt;&amp; mValues );
      void GetValuesIn( IHTMLDocument2* pHTMLDoc, map&lt;CString,CString&gt;&amp; mValues );
  
  // Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CScriptViewer)
      public:
      virtual void OnInitialUpdate();
      virtual void OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray&amp; baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel);
      virtual void OnDocumentComplete(LPCTSTR lpszURL);
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL
  
  // Implementation
  protected:
      virtual ~CScriptViewer();
  #ifdef _DEBUG
      virtual void AssertValid() const;
      virtual void Dump(CDumpContext&amp; dc) const;
  #endif
  
      CAMTrace m_trace;
  
      // Generated message map functions
      //{{AFX_MSG(CScriptViewer)
          // NOTE - the ClassWizard will add and remove member functions here.
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
  };
  </code></pre>
  
  <p>ScriptViewer.cpp</p>
  
  <pre><code>CScriptViewer::CScriptViewer()
  {
      //{{AFX_DATA_INIT(CScriptViewer)
          // NOTE: the ClassWizard will add member initialization here
      //}}AFX_DATA_INIT
      m_trace.SetEXEName( _T("CScriptViewer") );
      m_trace.Trace( _T("constructor"), FALSE, 0 );
  
      strScriptLocation = _T("");
      m_bInitialLoad = FALSE;
  
      m_pDoc = NULL;
      m_pDlg = NULL;
  }
  
  CScriptViewer::~CScriptViewer()
  {
      /*
      map&lt;CString,CString&gt; mValues;
      GetAllValues( mValues );
      m_pDlg-&gt;UpdateUserEnteredValues( mValues );
      */
      m_trace.Trace( _T("destructor"), FALSE, 0 );
  }
  
  void CScriptViewer::DoDataExchange(CDataExchange* pDX)
  {
      CHtmlView::DoDataExchange(pDX);
      //{{AFX_DATA_MAP(CScriptViewer)
          // NOTE: the ClassWizard will add DDX and DDV calls here
      //}}AFX_DATA_MAP
  }
  
  
  BEGIN_MESSAGE_MAP(CScriptViewer, CHtmlView)
      //{{AFX_MSG_MAP(CScriptViewer)
          // NOTE - the ClassWizard will add and remove mapping macros here.
      //}}AFX_MSG_MAP
  END_MESSAGE_MAP()
  
  /////////////////////////////////////////////////////////////////////////////
  // CScriptViewer diagnostics
  
  #ifdef _DEBUG
  void CScriptViewer::AssertValid() const
  {
      CHtmlView::AssertValid();
  }
  
  void CScriptViewer::Dump(CDumpContext&amp; dc) const
  {
      CHtmlView::Dump(dc);
  }
  #endif //_DEBUG
  
  /////////////////////////////////////////////////////////////////////////////
  // CScriptViewer message handlers
  
  void CScriptViewer::OnInitialUpdate() 
  {
      try
      {
      m_trace.Trace( _T("OnInitialUpdate") );
  
      ASSERT( m_pDoc );
      ASSERT( m_pDlg );
      }
      catch(...)
      {
  
      }
  }
  
  void CScriptViewer::OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags, LPCTSTR lpszTargetFrameName, CByteArray&amp; baPostedData, LPCTSTR lpszHeaders, BOOL* pbCancel) 
  {
      try
      {
      map&lt;CString,CString&gt; mValues;
      GetAllValues( mValues );
  
      ASSERT( m_pDlg );
  
  // GJS
      if (!m_pDlg) return;
      m_pDlg-&gt;UpdateUserEnteredValues( mValues );
  
      CString strURL = lpszURL;
      int nPosClose = strURL.Find( URL_INSTRUCTION_TO_ADAPTIVE_DESKTOP );
      if ( nPosClose &gt; 0 )
      {
          *pbCancel = TRUE;
          CHtmlView::OnBeforeNavigate2(lpszURL, nFlags, lpszTargetFrameName, baPostedData, lpszHeaders, pbCancel);
  
          m_pDlg-&gt;OnScriptInstructionToDesktop( strURL.Mid( nPosClose + _tcslen(URL_INSTRUCTION_TO_ADAPTIVE_DESKTOP) ), mValues );
      }
      else
      {
          CHtmlView::OnBeforeNavigate2(lpszURL, nFlags, lpszTargetFrameName, baPostedData, lpszHeaders, pbCancel);
      }
      }
      catch(...)
      {
  
      }
  }
  
  void CScriptViewer::OnDocumentComplete(LPCTSTR lpszURL) {
      if (!m_bInitialLoad) {
          //Navigate2(strScriptLocation);
          //m_bInitialLoad = TRUE;
      }
  }
  
  
  
  
  ///////////////////////////////////////////////////////////////////////////////////
  // accessing data values from the HTML pages, after the user has fiddled with them
  
  void CScriptViewer::GetValuesIn( IHTMLDocument2* pHTMLDoc, map&lt;CString,CString&gt;&amp; mValues )
  {
      try
      {
      if ( pHTMLDoc != NULL )
      {
          BSTR bsURL;
          VERIFY( SUCCEEDED( pHTMLDoc-&gt;get_URL( &amp;bsURL ) ) );
  //      TRACE( _T("GetValuesIn(%s)\r\n"), CString(bsURL) );
  
          IHTMLFramesCollection2* pFrames = NULL;
          if ( SUCCEEDED( pHTMLDoc-&gt;get_frames( &amp;pFrames ) ) )
          {
              long lNumFrames = 0;
              VERIFY( SUCCEEDED( pFrames-&gt;get_length( &amp;lNumFrames ) ) );
              for( long l = 0; l &lt; lNumFrames; l++ )
              {
                  COleVariant v1(l);
                  VARIANT vDispFrame;
                  if ( SUCCEEDED( pFrames-&gt;item( v1, &amp;vDispFrame ) ) )
                  {
                      if ( vDispFrame.vt == VT_DISPATCH )
                      {
                          IHTMLWindow2* pWindow = NULL;
                          VERIFY( SUCCEEDED( (vDispFrame.pdispVal)-&gt;QueryInterface( IID_IHTMLWindow2, (LPVOID*)&amp;pWindow ) ) );
                          ASSERT( pWindow );
                          IHTMLDocument2* pSubDoc = NULL;
                          if ( SUCCEEDED( pWindow-&gt;get_document( &amp;pSubDoc ) ) )
                          {
                              GetValuesIn( pSubDoc, mValues );
                              pSubDoc-&gt;Release();
                          }
                          pWindow-&gt;Release();
                      }
                  }
              }
              pFrames-&gt;Release();
          }
  
          IHTMLElementCollection* pElemColl = NULL;
          HRESULT hr = pHTMLDoc-&gt;get_all(&amp;pElemColl);
          if (SUCCEEDED(hr) &amp;&amp; pElemColl)
          {
              long lNumElements = 0;
              VERIFY( SUCCEEDED( pElemColl-&gt;get_length( &amp;lNumElements ) ) );
              for( long l = 0; l &lt; lNumElements; l++ )
              {
                  COleVariant v1(l);
                  COleVariant vzero((long)0);
                  LPDISPATCH pDispTemp = NULL;
                  VERIFY( SUCCEEDED( pElemColl-&gt;item( v1, vzero, &amp;pDispTemp ) ) );
                  ASSERT( pDispTemp != NULL );
                  IHTMLElement* pel = NULL;
                  VERIFY( SUCCEEDED( pDispTemp-&gt;QueryInterface( IID_IHTMLElement, (LPVOID*)&amp;pel ) ) );
  
                  CString str;
                  BSTR bsid;
                  pel-&gt;get_id( &amp;bsid );
                  VARIANT vValue;
                  pel-&gt;getAttribute( CString("value").AllocSysString(), 0, &amp;vValue );
                  CString strID = CString(bsid);
                  if ( !strID.IsEmpty() )
                  {
                      CString strValue = _T("");
                      if ( vValue.vt == VT_BSTR ) {
                          strValue = CString(vValue.bstrVal);
                      } else if ( vValue.vt == VT_I2  ||  vValue.vt == VT_I4 ) {
                          strValue.Format( _T("%d"), vValue.intVal );
                      }
  
                      mValues[strID] = strValue;
  
  //                  str.Format( _T("ID %s, value %s\r\n"),
  //                              strID, strValue );
  //                  strRetval += str;
                  }
  
                  pel-&gt;Release();
              }
              pElemColl-&gt;Release();
          }
          else
              ASSERT(FALSE);
      }
      else
          ASSERT(FALSE);  // passed null object doc
      }
      catch(...)
      {
  
      }
  }
  
  void CScriptViewer::GetAllValues( map&lt;CString,CString&gt;&amp; mValues )
  {
      try
      {
      mValues.clear();
  
      LPDISPATCH pDisp = GetHtmlDocument();
      if ( pDisp )
      {
          IHTMLDocument2* p = NULL;
          if ( SUCCEEDED( pDisp-&gt;QueryInterface( IID_IHTMLDocument2, (LPVOID*)&amp;p ) )  &amp;&amp;  p != NULL )
          {
              GetValuesIn( p, mValues );
              p-&gt;Release();
          }
          else
              ASSERT(FALSE);  // unable to QI for IHTMLDocument2?
  
          pDisp-&gt;Release();
      }
      }
      catch(...)
      {
  
      }
  }
  </code></pre>
  
  <p>Here is the code that handles the navigation:</p>
  
  <pre><code>CString strFilePath = CAMMiscSharedFilePaths::GetFullPathToWindowsTempDir() + _T("\\") + m_call-&gt;m_camCampaignSettings.m_scriptView.m_strName+_T("_temp.htm");
          HRESULT hr = WriteStringToTextFile( strFilePath, strRedirect );     //intentionally left for html settings, as it stores data in windows temp 
  
          if ( SUCCEEDED(hr) ) {
              pView-&gt;strScriptLocation = strFilePath;
  
              CString str = strFilePath;          
              pView-&gt;Navigate2(str);
  
  
          }
  </code></pre>