adspace


How to Make text background transparent?

Answer Posted / Vikas Kumar Sonkar

To make the background of text transparent in Delphi, you can create a transparent brush and set it as the background brush of a TLabel or other suitable component. Here's an example:
1. Set the `Transparent` property of your form to True.
2. Create a new unit for your custom functions (right-click on units, select `Add > Unit...`, name it something like 'CustomFunctions').
3. In the new unit, declare the required types and function:n```pascalnunit CustomFunctions;ninterfacen
type
TColorRef = ^TColor;nend;n```
4. Implement the function in the same unit:n```pascalnuses Vcl.Graphics, System.SysUtils, System.Variants;nvarn Alpha: Byte;nfunction CreateTransparentBrush(const Color: TColor): HBRUSH;nbeginn Result := CreateSolidBrush(RGB(Red(Color), Green(Color), Blue(Color)));n Alpha := (255 - (Red(Color) and $FF) * 2.2) div 100;n SetLastError(0);n if UpdateLayeredWindowAttributes(Handle(Result), 0, 0, LWA_ALPHA n + (Alpha shr 8) or LWA_COLORKEY, COLORREF(RGB(255 - Alpha, 255 - Alpha, 255 - Alpha))) thenn Result := CreateDIBBrush(GetDC(0), nil)n elsen DeleteObject(Result);n End;nend;n```
5. Use the custom function in your application:n```pascalnuses CustomFunctions, Vcl.Forms, Vcl.Controls,n Vcl.Graphics;type
TMyLabel = class(TLabel) {n FTransparentBrush: HBRUSH;n procedure CreateWnd;n procedure DestroyWnd;n procedure SetTextColor(const Value: TColor);n }npublished{n property TransparentBrush: HBRUSH read FTransparentBrush write SetTransparentBrush default nil;n procedure SetTransparentBrush(Value: HBRUSH);n }nnprocedure TMyLabel.CreateWnd;nbeginn inherited;n if not Assigned(FTransparentBrush) thenn FTransparentBrush := CreateTransparentBrush(Color);n end;nend;nnprocedure TMyLabel.DestroyWnd;nbeginn inherit;n DeleteObject(FTransparentBrush);nend;nnprocedure TMyLabel.SetTextColor(const Value: TColor);nbeginn inherited;n if Assigned(FTransparentBrush) thenn Brush.Color := Value;n end;nend;n```
6. Replace the default label on your form with `TMyLabel`. Set its Transparent property to True.
7. Customize the text color and transparency by setting the TextColor property of the TMyLabel.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to Create resource files?

1661


How to Print in Delphi without using the TPrinter Unit ?

1917


How can one prevent a TForm from being moved or resized?

1784


How to Flush binary file from disk cache to disk ?

2058


How to Retrieving the program that is associated with a given extension?

1714


How to Change the owner of a component at runtime ?

1936


How to Remove drives listed in a TDriveComboBox ?

1953


How to Retrieve the line number that a memo's cursor is on?

2020


How to Get the time and date in Universal Time ?

1769


How to Change the default width/height of the editor?

1925


How to Save a QuickReport custom preview to text?

3402


How to Determine the last access time of a given file ?

1847