Drawstring works on Win8 and later OS, but with win 7, when creating a HUD form as the child window, a Layered Window is created and an image is painted in the window. However, this type of windows has its own drawing specifics. For instance, when a layered window is used and the text has full opacity, fonts draw a transparent “hole” in the background. However, if a transparency is added to the text color, the fonts become opaque .
You can still overcome the issue by drawing text on the image surface and then painting the image on the form. That is, the Draw method can be implemented in the following manner:
public void Draw(GraphicsCache cache, Rectangle bounds)
{
Rectangle textBounds = new Rectangle(0, 0, 200, 50);
Bitmap bmp = new Bitmap(textBound.Width, textBounds.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.FillRectangle(Brushes.Purple, textBounds);
g.DrawString("Some test text", new Font("Tahoma", (float)8.25), Brushes.Red, textBounds);
}
cache.Graphics.DrawImageUnscaled(bmp, new Point(25, 265));
} procedure THeadsUpDisplay.DrawHeadsUpBackground;
Var
Bmp : TBitmap;
graphics : GpGraphics;
gbrush : GpLineGradient;
grect : TGPRect;
BkColorTop, BkColorBottom : TColor;
Begin
BkColorTop := clRed;
BkColorBottom := clBlue;
Bmp := FForm_HeadsUpTranslucent.Bitmap;
Bmp.Canvas.Brush.Color := 0;
Bmp.Canvas.FillRect(Bounds(0, 0, FForm_HeadsUpTranslucent.Width, FForm_HeadsUpTranslucent.Height));
GdipCreateFromHDC(Bmp.Canvas.Handle, graphics);
Try
grect := MakeRect(0, 0, Bmp.Width, Bmp.Height);
GdipCreateLineBrushFromRectI(@grect,
MakeARGBColor(BkColorTop, GetTransparency),
MakeARGBColor(BkColorBottom, GetTransparency),
LinearGradientModeVertical, WrapModeTile, gbrush);
Try
GdipFillRectangleI(graphics, gbrush, grect.X, grect.Y, grect.Width, grect.Height);
Finally
GdipDeleteBrush(gbrush);
End;
Finally
GdipDeleteGraphics(graphics);
End;
End;
procedure THeadsUpDisplay.PaintTextToDC(ADC: HDC; const ALeft, ATop: Integer;
TransparencySupport: Boolean; ATransparency: Byte);
Var
R : Integer;
C : Integer;
x, y : Integer;
CellText : WideString;
GraphicsObj : TGPGraphics;
GPFont : TGPFont;
GpBrush : TGPBrush;
GpOrigin : TGpPointF;
Begin
X := ALeft;
y := ATop;
GraphicsObj := TGPGraphics.Create(ADC);
GPFont := TGPFont.Create(ADC, FFont.Handle);
// In Win 7
// make the alpha value smaller than the 255, will draw the text as opaque
// If use 255, the text will be drawing as fully transparency
// Also the The desktop composition, font smooth feature may affect the text drawing
// https://docs.microsoft.com/en-us/windows/win32/dwm/dwm-overview
// https://supportcenter.devexpress.com/ticket/details/t369905/transparent-text-on-splashscreen-windows-7-rdc
GpBrush := TGPSolidBrush.Create(MakeARGBColor(FFont.Color, 254));
Try
CellText := FContent;
GpOrigin.x := x;
GpOrigin.y := y;
GraphicsObj.DrawString(CellText, Length(CellText), GPFont, GpOrigin, GpBrush);
Finally
FreeAndNil(GpBrush);
FreeAndNil(GpFont);
FreeAndNil(GraphicsObj);
End;
End;
References:
https://supportcenter.devexpress.com/ticket/details/t369905/transparent-text-on-splashscreen-windows-7-rdc
https://docs.microsoft.com/en-us/windows/win32/dwm/dwm-overview
https://forum.unity.com/threads/solved-windows-transparent-window-with-opaque-contents-lwa_colorkey.323057