title: C++ GDI+ drawing text on a transparent layered window
body: <p>(unmanaged C++)
  I already succeeded drawing PNG files to a transparent layered window that I can drag around the desktop, but now my problem is drawing text on a transparent layered window</p>
  
  <p>Here's my code and my attempt at drawing text in the middle, it's important to note that i'm using the screenDC instead of using the one in WM_PAINT messages</p>
  
  <p>[edit]
  updated code after the comments, now i'm just trying to write text on the bitmap before getting the HBITMAP version which i need to use 
  this time I'm using DrawString because textout() isn't GDI+, I hope DrawString really is GDI+ lol
  still doesn't work though, wonder what i'm doing wrong</p>
  
  <pre><code>void Draw() // draws a frame on the layered window AND moves it based on x and y
  {
      HDC screenDC( NULL ); // grab screen
      HDC sourceDC( CreateCompatibleDC(screenDC) );
  
      POINT pos = {x,y}; // drawing location
      POINT sourcePos = {0,0}; // top left of image
      SIZE size = {100,100}; // 100x100 image
  
      BLENDFUNCTION blendFunction = {0};
      HBITMAP bufferBitmap = {0};
      Bitmap* TheBitmap = crnimage; // crnimage was already loaded earlier
  
      // ------------important part goes here, my attempt at drawing text ------------//
  
   Gdiplus::Graphics     Gx(TheBitmap);
  
  // Font* myFont =    new Font(sourceDC);
   Font myFont(L"Arial", 16);
  
  
   RectF therect;
   therect.Height = 20;
   therect.Width = 180;
   therect.X = 0;
   therect.Y = 0;
  
   StringFormat format;
   format.SetAlignment(StringAlignmentCenter);
   format.GenericDefault();
   Gdiplus::SolidBrush   GxTextBrush(Gdiplus::Color(255, 255, 0,255));
  
  
   WCHAR thetext[] = L"Sample Text";
  
   int stats = Gx.DrawString(thetext, -1, &amp;myFont, therect, &amp;format, &amp;GxTextBrush);
   if(stats) // DrawString returns nonzero if there is an error
       msgbox(stats); 
   stats = Gx.DrawRectangle(&amp;Pen(Color::Red, 3), therect);
   // the rectangle and text both draw fine now
  
   // ------------important part goes here, my attempt at drawing text ------------//
  
      TheBitmap-&gt;GetHBITMAP(0, &amp;bufferBitmap);
      HBITMAP oldBmpSelInDC;
      oldBmpSelInDC = (HBITMAP)SelectObject(sourceDC, bufferBitmap);
  
      // some alpha blending
      blendFunction.BlendOp = AC_SRC_OVER;
      blendFunction.SourceConstantAlpha = wndalpha;
      blendFunction.AlphaFormat = AC_SRC_ALPHA;
      COLORREF colorKey( RGB(255,0,255) );
      DWORD flags( ULW_ALPHA);
  
      UpdateLayeredWindow(hWnd, screenDC, &amp;pos, &amp; size, sourceDC, &amp;sourcePos,
      colorKey, &amp;blendFunction, flags);
  
      // release buffered image from memory
      SelectObject(sourceDC, oldBmpSelInDC);
      DeleteDC(sourceDC);
      DeleteObject(bufferBitmap); 
  
      // finally release the screen
      ReleaseDC(0, screenDC);
  }
  </code></pre>
  
  <p>I've been trying to write text on my layered window for two days now, but from those attempts I know there are several ways I can go about doing this 
  (unfortunately I have no idea how exactly)</p>
  
  <p>The usual option I see is drawing text on a bitmap, then rendering the bitmap itself</p>
  
  <blockquote>
    <ol>
    <li>Use Gdi+ to load a bitmap</li>
    <li>Create a Graphics object from the bitmap</li>
    <li>Use DrawString to write text to the bitmap</li>
    <li>Dispose of the Graphics object</li>
    <li>Use the bitmap Save method to save the result to a file</li>
    </ol>
  </blockquote>
  
  <p>Apparently one can also make a graphics object from a DC, then draw text on the DC, but again i have no clue as to how to do this</p>