title: Create 32 bit color Icon programmatically
body: <p>I would like to create 32 bit color icons programmatically using C++ and Win API. For this purpose I use the following code which I found <a href="https://stackoverflow.com/a/16473172/7277003">here</a>.</p>
  
  <pre><code>HICON CreateSolidColorIcon(COLORREF iconColor, int width, int height)
  {
      // Obtain a handle to the screen device context.
      HDC hdcScreen = GetDC(NULL);
  
      // Create a memory device context, which we will draw into.
      HDC hdcMem = CreateCompatibleDC(hdcScreen);
  
      // Create the bitmap, and select it into the device context for drawing.
      HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, width, height);
      HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmp);
  
      // Draw your icon.
      // 
      // For this simple example, we're just drawing a solid color rectangle
      // in the specified color with the specified dimensions.
      HPEN hpen = CreatePen(PS_SOLID, 1, iconColor);
      HPEN hpenOld = (HPEN)SelectObject(hdcMem, hpen);
      HBRUSH hbrush = CreateSolidBrush(iconColor);
      HBRUSH hbrushOld = (HBRUSH)SelectObject(hdcMem, hbrush);
      Rectangle(hdcMem, 0, 0, width, height);
      SelectObject(hdcMem, hbrushOld);
      SelectObject(hdcMem, hpenOld);
      DeleteObject(hbrush);
      DeleteObject(hpen);
  
      // Create an icon from the bitmap.
      // 
      // Icons require masks to indicate transparent and opaque areas. Since this
      // simple example has no transparent areas, we use a fully opaque mask.
      HBITMAP hbmpMask = CreateCompatibleBitmap(hdcScreen, width, height);
      ICONINFO ii;
      ii.fIcon = TRUE;
      ii.hbmMask = hbmpMask;
      ii.hbmColor = hbmp;
      HICON hIcon = CreateIconIndirect(&amp;ii);
      DeleteObject(hbmpMask);
  
      // Clean-up.
      SelectObject(hdcMem, hbmpOld);
      DeleteObject(hbmp);
      DeleteDC(hdcMem);
      ReleaseDC(NULL, hdcScreen);
  
      // Return the icon.
      return hIcon;
  }
  </code></pre>
  
  <p>In principle the code works and I can use it to create colored icons at runtime using the Win API. However, I have some problems and questions about that code (and creating icons in general) which I would like to discuss.</p>
  
  <ul>
  <li>The icons created with this function don't seem to be of 32 bit color depth. If I use a color like RGB(218, 112, 214) I would expect it to be some light purple. However, the actual displayed color is gray. How can I change the code such that the color is really 32 bit RGB?</li>
  <li>The icon created is completly filled with the color, I would like to have a thin black boarder around it... how can this be achieved?</li>
  <li>In the MSDN <a href="https://msdn.microsoft.com/de-de/library/windows/desktop/ms648051(v=vs.85).aspx#_win32_Creating_an_Icon" rel="nofollow noreferrer">documentation</a> (a bit downwards) it is mentioned that <em>"Before closing, your application must use DestroyIcon to destroy any icon it created by using CreateIconIndirect. It is not necessary to destroy icons created by other functions. "</em> However, in the documentation for e.g. <a href="https://msdn.microsoft.com/de-de/library/windows/desktop/ms648059(v=vs.85).aspx" rel="nofollow noreferrer">CreateIcon</a> in MSDN it is said that <em>"When you are finished using the icon, destroy it using the DestroyIcon function."</em> which is pretty much a contradiction. When do I actually have to destroy the icon?</li>
  <li>Do these rules then also apply when I add the icon to an image list and this list to a combobox? I.e. do I have to clean up the image list and each associated icon?</li>
  </ul>
  
  <p>Any help is highly appreciated.</p>