title: Common controls are not properly painted with WM_CTLCOLORSTATIC handler after I set Visual Styles on
body: <h2>INTRODUCTION AND RELEVANT INFORMATION:</h2>
  
  <p>I have 2 <em>dialog boxes</em> created via <em>Resource editor</em>. Since I use <em>Microsoft Visual Studio Express edition</em>, I had to download <a href="http://www.resedit.net/" rel="noreferrer">free resource editor</a> to create them. In my program, I have <code>Visual Styles</code> enabled like this:</p>
  
  <pre><code>#include &lt;commctrl.h&gt;
  
  #pragma comment( lib, "comctl32.lib")
  
  #pragma comment( linker, "/manifestdependency:\"type='win32' \
          name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
          processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
          language='*'\"")
  </code></pre>
  
  <p>As far as I know, <em>check box</em>, <em>radio button</em>, and <em>group box</em> get <code>WM_CTLCOLORSTATIC</code> message for painting their text.</p>
  
  <p>This is what I have coded for the first and second <em>dialog box</em>:</p>
  
  <pre><code>case WM_CTLCOLORSTATIC:
      {
          SetBkMode( (HDC)wParam, TRANSPARENT );
          SetTextColor( (HDC)wParam, RGB( 0, 0, 0 ) );
          return (INT_PTR)( (HBRUSH)GetStockObject(NULL_BRUSH) );
      }
  </code></pre>
  
  <p><em>I want those controls to have transparent text background and black color of their text.</em></p>
  
  <h2>THE PROBLEM:</h2>
  
  <p>On <em>Windows XP</em>, here is the result image for first dialog:</p>
  
  <p><img src="https://i.stack.imgur.com/Mc4A5.png" alt="enter image description here"></p>
  
  <p><em>Group box</em> has blue text, and brown border, while <em>check box</em> has everything black. On <em>Windows 7</em>, after starting the same program, I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/8efGF.png" alt="enter image description here"></p>
  
  <p>Here, <em>group box</em> and <em>check box</em> have proper text color, yet background of the <em>check box</em> and the border of the <em>group box</em> are wrong. In my <em>dialog box</em>, I have <em>static controls</em> and <strong>they are painted properly</strong> both on <em>Windows 7</em> and <em>Windows XP</em>.</p>
  
  <h2>WHAT HAVE I TRIED SO FAR:</h2>
  
  <p>I have browsed through SO archive, but haven't found anything I could use to modify my <code>WM_CTLCOLORSTATIC</code> handler.</p>
  
  <p>I have found a workaround which <strong>removes</strong> <em>Visual Styles</em> from those controls, so it can achieve the desired result, but I need to keep the <em>Visual Styles</em> and make the background of the text transparent, thus this solution can not satisfy me.</p>
  
  <p>After looking through <em>Visual Styles reference</em> and a little experimenting, I have found a workaround for <em>radio button</em> and <em>check box</em> ( but not for <em>group box</em> ) with the following code:</p>
  
  <pre><code>case WM_CTLCOLORSTATIC:
      if( (HWND)lParam == GetDlgItem( hwnd, IDC_RADIO1 ) ) 
      {
          RECT r;
          GetClientRect( hwnd, &amp;r );
          DrawThemeParentBackground( (HWND)lParam, (HDC)wParam, &amp;r );
      }
      else
      {
          SetTextColor( (HDC)wParam, RGB( 0, 0, 0 ) );
          SetBkMode( (HDC)wParam, TRANSPARENT );
      }
      return (INT_PTR)( (HBRUSH)GetStockObject(NULL_BRUSH) );
  </code></pre>
  
  <p>Still, I have "hit the wall" with this:</p>
  
  <p>In my dialog box there is a <em>treeview</em> and once I select node and press <kbd>spacebar</kbd> ( or any other key for that matter ) dialog's background bitmap gets on top of my static controls.</p>
  
  <p>After I comment out <code>DrawThemeParentBackground()</code>, recompile and start program again everything works fine ( when I select tree's node and press <kbd>spacebar</kbd> ) but then I am "at square one".</p>
  
  <h2>THE QUESTIONS:</h2>
  
  <ol>
  <li><p>How can I modify my <code>WM_CTLCOLORSTATIC</code> handler to fix my problem ?</p></li>
  <li><p>If the above is not possible, can I get the desired effect with <code>NM_CUSTOMDRAW</code> ? </p></li>
  </ol>
  
  <p><em>NOTE:</em></p>
  
  <p><em>I think that I will have to draw <code>group box</code> using <code>GDI</code>. If that is the case, I will accept this solution too, as my main concern is <code>checkbox</code> and <code>radio button</code>.</em></p>
  
  <h2>EDITED WITH SUBMITTED EXAMPLE PROJECT:</h2>
  
  <p>On request, I am submitting a <a href="http://www.sscce.org/" rel="noreferrer"><em>SSCCE</em></a>. To create the project follow these steps:</p>
  
  <p>1.) Create default Win32 project in Visual Studio.</p>
  
  <p>2.) In the <code>stdafx.h</code> copy/paste the following directives, below <code>#include &lt;windows.h&gt;</code>:</p>
  
  <pre><code>#include &lt;commctrl.h&gt;
  #include &lt;Uxtheme.h&gt;
  
  #pragma comment( linker, "/manifestdependency:\"type='win32' \ 
                      name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
                      processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
                      language='*'\"")
  
  #pragma comment( lib, "comctl32.lib")
  #pragma comment( lib,"Msimg32.lib")    // needed for GradientFill(...) API
  #pragma comment( lib, "UxTheme.lib")
  </code></pre>
  
  <p>3.) In the resource editor, alter <em>About</em> dialog box by adding a 2 <em>radio buttons</em>, 2 <em>checkboxes</em> and a <em>group box</em> enclosing them ( see the above pictures too see what I mean ) and add a <em>treeview</em> control ( this control will "trigger" the problem ).</p>
  
  <p>4.) In your main <code>cpp</code> file, add the helper function for drawing the custom background:</p>
  
  <pre><code>void GradientTriangle( HDC MemDC, 
      LONG x1, LONG y1, 
      LONG x2, LONG y2, 
      LONG x3, LONG y3, 
      COLORREF top, COLORREF bottom )
  {
      TRIVERTEX vertex[3];
  
      vertex[0].x     = x1;
      vertex[0].y     = y1;
      vertex[0].Red   = GetRValue(bottom) &lt;&lt; 8;
      vertex[0].Green = GetGValue(bottom) &lt;&lt; 8;
      vertex[0].Blue  = GetBValue(bottom) &lt;&lt; 8;
      vertex[0].Alpha = 0x0000;
  
      vertex[1].x     = x3;
      vertex[1].y     = y3; 
      vertex[1].Red   = GetRValue(bottom) &lt;&lt; 8;
      vertex[1].Green = GetGValue(bottom) &lt;&lt; 8;
      vertex[1].Blue  = GetBValue(bottom) &lt;&lt; 8;
      vertex[1].Alpha = 0x0000;
  
      vertex[2].x     = x2;
      vertex[2].y     = y2;
      vertex[2].Red   = GetRValue(top) &lt;&lt; 8;
      vertex[2].Green = GetGValue(top) &lt;&lt; 8;
      vertex[2].Blue  = GetBValue(top) &lt;&lt; 8;
      vertex[2].Alpha = 0x0000;
  
      // Create a GRADIENT_TRIANGLE structure that
      // references the TRIVERTEX vertices.
  
      GRADIENT_TRIANGLE gTriangle;
  
      gTriangle.Vertex1 = 0;
      gTriangle.Vertex2 = 1;
      gTriangle.Vertex3 = 2;
  
      // Draw a shaded triangle.
  
      GradientFill( MemDC, vertex, 3, &amp;gTriangle, 1, GRADIENT_FILL_TRIANGLE);
  }
  </code></pre>
  
  <p>5.) Initiate <code>common controls</code> in <code>_tWinMain</code>:</p>
  
  <pre><code>// initialize common controls
  
  INITCOMMONCONTROLSEX iccex;
  iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  iccex.dwICC = ICC_LISTVIEW_CLASSES | ICC_UPDOWN_CLASS | ICC_STANDARD_CLASSES ;
  InitCommonControlsEx(&amp;iccex);
  </code></pre>
  
  <p>6.) Insert some items in the <em>treeview</em> in the <code>WM_INITDIALOG</code> :</p>
  
  <pre><code>case WM_INITDIALOG:
      {
          HWND TreeView = GetDlgItem( hDlg, IDC_TREE1 );
  
          // add root item
  
          TVINSERTSTRUCT tvis = {0};
  
          tvis.item.mask = TVIF_TEXT;
          tvis.item.pszText = L"This is root item";
          tvis.hInsertAfter = TVI_LAST;
          tvis.hParent = TVI_ROOT;
  
          HTREEITEM hRootItem = reinterpret_cast&lt;HTREEITEM&gt;( SendMessage( TreeView , 
              TVM_INSERTITEM, 0, reinterpret_cast&lt;LPARAM&gt;( &amp;tvis ) ) );
  
          // add firts subitem for the hTreeItem
  
          memset( &amp;tvis, 0, sizeof(TVINSERTSTRUCT) );
  
          tvis.item.mask = TVIF_TEXT;
          tvis.item.pszText = L"This is first subitem";
          tvis.hInsertAfter = TVI_LAST;
          tvis.hParent = hRootItem;
  
          HTREEITEM hTreeSubItem1 = reinterpret_cast&lt;HTREEITEM&gt;( SendMessage( TreeView , 
              TVM_INSERTITEM, 0, reinterpret_cast&lt;LPARAM&gt;( &amp;tvis ) ) );
  
          // now we insert second subitem for hRootItem
  
          memset( &amp;tvis, 0, sizeof(TVINSERTSTRUCT) );
  
          tvis.item.mask = TVIF_TEXT | TVIF_STATE; // added extra flag
          tvis.item.pszText = L"This is second subitem";
          tvis.hInsertAfter = TVI_LAST;
          tvis.hParent = hRootItem;
  
          HTREEITEM hTreeSubItem2 = reinterpret_cast&lt;HTREEITEM&gt;( SendMessage( TreeView , 
              TVM_INSERTITEM, 0, reinterpret_cast&lt;LPARAM&gt;( &amp;tvis ) ) );
      }
      return (INT_PTR)TRUE;
  </code></pre>
  
  <p>7.) Paint the custom background of the dialog:</p>
  
  <pre><code>case WM_ERASEBKGND:
      {
          RECT r;
          GetClientRect( hDlg, &amp;r );
  
          GradientTriangle( (HDC)wParam, r.right, r.bottom - r.top, 
              r.left, r.bottom - r.top,
              r.left, r.top,
              RGB( 0x0, 0x0, 0xFF ), RGB( 0xFF, 0xFF, 0x0 ) );
  
          GradientTriangle( (HDC)wParam, r.right, r.bottom - r.top, 
              r.right, r.top,
              r.left, r.top, 
              RGB( 0xFF, 0x0, 0x0 ), RGB( 0x0, 0xFF, 0x0 ) );
      }
      return TRUE;
  </code></pre>
  
  <p>8.) Add the handler for <code>WM_CTLCOLORSTATIC</code>:</p>
  
  <pre><code>case WM_CTLCOLORSTATIC:
      if( ( (HWND)lParam == GetDlgItem( hDlg, IDC_RADIO1 ) )       
          || ( (HWND)lParam == GetDlgItem( hDlg, IDC_CHECK1 ) ) ) 
      {
          RECT r;
          GetClientRect( hDlg, &amp;r );
          DrawThemeParentBackground( (HWND)lParam, (HDC)wParam, &amp;r );
      }
      else
      {
          SetTextColor( (HDC)wParam, RGB( 0, 0, 0 ) );
          SetBkMode( (HDC)wParam, TRANSPARENT );
      }
      return (INT_PTR)( (HBRUSH)GetStockObject(NULL_BRUSH) );
  </code></pre>
  
  <p>That would be all required steps and information to create minimal program that demonstrates the problem. Now run the application and observe the differences:</p>
  
  <p><em>Radio button</em> with <code>IDC_RADIO1</code> and <em>checkbox</em> with <code>IDC_CHECK1</code> will be properly painted ( transparent text with black color ), while other <em>checkbox</em> and <em>radio button</em> will not. Furthermore, after you click on a <em>treeview</em> and press <kbd>spacebar</kbd> you will see the problem with dialogs background.</p>
  
  <h2>END OF EDIT</h2>
  
  <p>Thank you.</p>
  
  <p>Best regards.</p>