title: How do I draw an icon in a Windows dialog box?
body: <p>I am trying to draw an icon in the title bar of a Windows dialog box. I am not trying to replace the normal icon for the dialog. I am trying to draw an icon in addition to the minimize, maximize/restore and close icons. To that effect, I am sending the WM_GETTITLEBARINFOEX message so I can calculate my position. Try as I might, I can't get the icon in my image list to draw. I have been able to draw the icon in the client area. I finally just tried drawing at (0,0) and at (100,0) and now in the middle of the title bar. I just never see the icon in the title bar. I also added code to draw below the title bar just to make sure my icon is fine and the icon draws in the window.</p>
  <p>First I override OnNcPaint, call the base class and then my routine to draw my icon.</p>
  <p>void MyDialog::OnNcPaint()
  {</p>
  <pre><code>CDialog::OnNcPaint();
  
  CWindowDC dc(this);
  
  DrawNonClientArea( &amp;dc );
  </code></pre>
  <p>}</p>
  <p>void MyDialog::DrawAttachIcon(CDC* pDC)
  {</p>
  <pre><code>if( -1 != m_AttachIconIndex &amp;&amp; pDC )
  {
      TITLEBARINFOEX TitleBarInfoEx;
      TitleBarInfoEx.cbSize = sizeof( TitleBarInfoEx );
  
      ::SendMessage( GetSafeHwnd(), WM_GETTITLEBARINFOEX, 0, (LPARAM)&amp;TitleBarInfoEx );
  
      // As per the MSDN, rectangle indexes are 0 (reserved), 1 (reserved), 2 - minimize button, 3 - maximize button, 4 - help button and 5 close button.
      CSize sizeClose( TitleBarInfoEx.rgrect[5].right - TitleBarInfoEx.rgrect[5].left, TitleBarInfoEx.rgrect[5].bottom - TitleBarInfoEx.rgrect[5].top );
      CSize sizeMaximize( TitleBarInfoEx.rgrect[3].right - TitleBarInfoEx.rgrect[3].left, TitleBarInfoEx.rgrect[3].bottom - TitleBarInfoEx.rgrect[3].top );
      CSize sizeMinimize( TitleBarInfoEx.rgrect[2].right - TitleBarInfoEx.rgrect[2].left, TitleBarInfoEx.rgrect[2].bottom - TitleBarInfoEx.rgrect[2].top );
  
      int padding_x = TitleBarInfoEx.rgrect[5].left - TitleBarInfoEx.rgrect[3].right;
      int padding_y = TitleBarInfoEx.rgrect[5].top - TitleBarInfoEx.rcTitleBar.top;
  
      CPoint pt;
  
      pt.x = TitleBarInfoEx.rgrect[2].left - sizeMinimize.cx;
      pt.y = TitleBarInfoEx.rgrect[2].top;
  
      // I give up. Try to draw in the middle of the caption.
      pt.x = (TitleBarInfoEx.rcTitleBar.right - TitleBarInfoEx.rcTitleBar.left)/2; pt.y = 0;
  
      m_ilIconAttachToFrame.Draw( pDC, m_AttachIconIndex, pt, ILD_NORMAL );
      pt.y += 200;
      m_ilIconAttachToFrame.Draw( pDC, m_AttachIconIndex, pt, ILD_NORMAL );
  }
  </code></pre>
  <p>}</p>
  <p>That last two lines was to just draw in the window and the icon draws where I expect. I also blasted a filled rect first on the icons (close, minimize ...), then the whole title area using the rect from the title bar info. And even blasted a rect over the entire window, which resulted in only the client area being painted with the color. Is there an exclusion rect over the caption? I called GetClipRgn and the return value was zero, so I don't think there is.</p>