adspace
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
How to Create resource files?
How to Print in Delphi without using the TPrinter Unit ?
How can one prevent a TForm from being moved or resized?
How to Flush binary file from disk cache to disk ?
How to Retrieving the program that is associated with a given extension?
How to Change the owner of a component at runtime ?
How to Remove drives listed in a TDriveComboBox ?
How to Retrieve the line number that a memo's cursor is on?
How to Get the time and date in Universal Time ?
How to Change the default width/height of the editor?
How to Save a QuickReport custom preview to text?
How to Determine the last access time of a given file ?