C ++ GDI + drawing text in a transparent layered window

(unmanaged C ++) I already managed to draw PNG files in a transparent layered window that I can drag to the desktop, but now my problem is to draw text in a transparent layered window

Here is my code and my attempt to draw the text in the middle, it is important to note that I use screenDC instead of using the message in WM_PAINT messages

[edit] updated code after comments, now I'm just trying to write text in a bitmap before getting the HBITMAP version that I need to use this time I use DrawString because textout () is not GDI +, I hope DrawString really GDI + lol still doesn't work, I wonder what I'm doing wrong

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, &myFont, therect, &format, &GxTextBrush); if(stats) // DrawString returns nonzero if there is an error msgbox(stats); stats = Gx.DrawRectangle(&Pen(Color::Red, 3), therect); // the rectangle and text both draw fine now // ------------important part goes here, my attempt at drawing text ------------// TheBitmap->GetHBITMAP(0, &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, &pos, & size, sourceDC, &sourcePos, colorKey, &blendFunction, flags); // release buffered image from memory SelectObject(sourceDC, oldBmpSelInDC); DeleteDC(sourceDC); DeleteObject(bufferBitmap); // finally release the screen ReleaseDC(0, screenDC); } 

I’ve been trying to write text in my multi-level window for two days now, but from the attempts that I know there are several ways to do this (unfortunately, I have no idea how)

The usual option that I see is to draw text in a bitmap and then render the bitmap itself

  • Use Gdi + to download a bitmap
  • Create a graphic from a bitmap
  • Use DrawString to write text to a bitmap
  • Dispose of the Graphics Object
  • Use the save bitmap method to save the result to a file

Apparently, you can also make a graphic object from DC, and then draw text on DC, but again I don’t know how to do it

+4
source share
1 answer

The general approach looks right, but I think you have problems calling DrawString . Check the documentation (especially the sample) on MSDN .

 Gx.DrawString(thetext, 4, NULL, therect, NULL, NULL) 

You may need to specify the third, fifth, and sixth parameters (font, format, and brush). The documentation does not indicate that they are optional. Passing NULL for them is likely to cause GDI + to handle the call as no-op.

The second parameter should not include the trailing L '\ 0' in the string. It is probably safer to use -1 if your line is always complete.

+2
source

Source: https://habr.com/ru/post/1336804/


All Articles