title: Relationship between AFX_WM_DRAW2D and WM_PAINT in MFC Application
body: <p>What is the relationship between the two messages in the subject? When exactly the AFX_WM_DRAW2D is passed to application queue.</p>
  
  <p>I am asking because I have encountered the strangest recursive call of AFX_WM_DRAW2D  handler. Whenever I am trying to paint something inside the AFX_WM_DRAW2Din compatibe render target the handler is somehow called recursively which I have difficulty to explain. The structure of MFC application is like that:-</p>
  
  <pre><code>BEGIN_MESSAGE_MAP(CTACSIView, CView)
      // Standard printing commands
      ...
      ON_REGISTERED_MESSAGE(AFX_WM_DRAW2D, &amp;CTACSIView::OnDraw2D)
  END_MESSAGE_MAP()
  
  
  afx_msg LRESULT CTACSIView::OnDraw2D(WPARAM wParam, LPARAM lParam)
  {
  
      CHwndRenderTarget* pRenderTarget = (CHwndRenderTarget*)lParam;
      ASSERT_VALID(pRenderTarget);
  
  
      CRect clientRect;
      GetClientRect(clientRect);
  
      if (m_BackGroundHasChanged)
      {
  
  
          CBitmapRenderTarget compatibleRenderTarget;
          pRenderTarget-&gt;CreateCompatibleRenderTarget(compatibleRenderTarget, m_viewScreenSize);
  
          compatibleRenderTarget.BeginDraw();
  
  
          // 
          // ----&gt; HERE THE RECURSIVE CALL OCCURS &lt;----
          //
              D2D1::Matrix3x2F shiftRightTranslation = D2D1::Matrix3x2F::Translation(D2D1::SizeF(TACSI_VIEW_BATTLE_FIELD_OFFSET_X, 0));
      m_pRulerBitmapBrush_X.SetTransform(&amp;shiftRightTranslation);
  
      compatibleRenderTarget.FillRectangle(rulerRectangle_X, &amp;m_pRulerBitmapBrush_X);
  </code></pre>