How to Make text background transparent?
Answer / 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 |
How to Add accelerator characters to TTabsheets?
How to Disable system menu in code?
How to tell in code if my application is running on Windows NT ?
How to Retrieve a long file name from the command line ?
How to get a list of CDROM drives on a computer?
How to Use InvalidateRect()to repaint the entire form?
How to Create a delay without a timer?
How to Modify application server SQL from the client?
How SQL Links determines if an MSSQL table can refreshed?
What operating systems does Delphi support?
How to Change the font in a hint window ?
How to Set a transparent background color for TImageList?