C ++ GDI+ TEXTO DE DIBUJO en una ventana en capas transparentes
Pregunta
(C ++ no administrado) Ya tuve éxito dibujar archivos PNG a una ventana en capas transparente que puedo arrastrar por el escritorio, pero ahora mi problema es dibujar texto en una ventana en capas transparentes
Aquí está mi código y mi intento de dibujar texto en el medio, es importante tener en cuenta que estoy usando la shatchEdC en lugar de usar el de los mensajes wm_paint
Editar] Código actualizado Después de los comentarios, ahora solo estoy tratando de escribir texto en el mapa de bits antes de obtener la versión HBITMAP que necesito usar esta vez que estoy usando DrawTring porque TextOut () no es GDI+, espero que Drawstring Realmente es GDI+ LOL todavía no funciona, aunque me pregunto qué estoy haciendo mal
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);
}
He estado tratando de escribir texto en mi ventana en capas durante dos días, pero a partir de esos intentos, sé que hay varias formas de hacer esto (desafortunadamente no tengo idea de cómo exactamente)
La opción habitual que veo es dibujar texto en un mapa de bits, luego hacer que el mapa de bits en sí
- Use GDI+ para cargar un mapa de bits
- Crea un objeto gráfico desde el mapa de bits
- Use el cordón para escribir texto en el mapa de bits
- Deseche el objeto gráfico
- Use el método de guardado de mapas de bits para guardar el resultado en un archivo
Aparentemente, también se puede hacer un objeto gráfico de un DC, luego dibujar texto en el DC, pero nuevamente no tengo idea de cómo hacer esto
Solución
El enfoque general se ve bien, pero creo que tienes algunos problemas con el DrawString
llamar. Consulte la documentación (especialmente la muestra) en MSDN.
Gx.DrawString(thetext, 4, NULL, therect, NULL, NULL)
Los terceros, quinto y sexto parámetros (fuente, formato y pincel) probablemente deben especificarse. La documentación no dice que sean opcionales. Paso NULL
Porque esto probablemente está causando que GDI+ trate la llamada como un NO-OP.
El segundo parámetro no debe incluir la terminación l ' 0' en la cadena. Probablemente sea más seguro de usar -1 si su cadena siempre termina.