Author: Jonas Bilinkevicius
Does anyone know how to use a tiled picture as the background for a TRichEdit
control?
Answer:
For a standard TRichEdit there seems to be no way to make it transparent or paint
its background with a tiled bitmap. But there is a workaround if you're using the
Win2000 operating system. There you can make your control transparent by setting
the WS_EX_LAYERED constant to the window extended style and then calling the
SetLayeredWindowAttributes Win API function.
The example listed below is a TRichEdit control with a DrawStyle property.
Depending on its value, the control will have a transparent background or will draw
itself with an alpha transparency.
1 { ... }2 type3 TDrawStyle = (ds_Transparent, ds_NotDistinctly, dsNormal);
4 5 MyTransparentRichEdit = class(TRichEdit)
6 protected7 FDrawStyle: TDrawStyle;
8 procedure CreateParams(var Params: TCreateParams); override;
9 procedure CreateWnd; override;
10 procedure SetDrawStyle(AValue: TDrawStyle);
11 public12 constructor Create(AOwner: TComponent); override;
13 published14 property DrawStyle: TDrawStyle read FDrawStyle write SetDrawStyle;
15 end;
16 17 { ... }18 19 constructor MyTransparentRichEdit.Create(AOwner: TComponent);
20 begin21 inherited Create(AOwner);
22 FDrawStyle := dsNormal;
23 end;
24 25 procedure MyTransparentRichEdit.CreateParams(var Params: TCreateParams);
26 begin27 inherited CreateParams(Params);
28 ifnot (csDesigning in ComponentState) then29 begin30 Params.Style := Params.Style or WS_POPUP;
31 Params.ExStyle := Params.ExStyle + WS_EX_LAYERED;
32 end;
33 end;
34 35 procedure MyTransparentRichEdit.CreateWnd;
36 var37 XPoint: TPoint;
38 begin39 ifnot (csDesigning in ComponentState) then40 begin41 XPoint := TWinControl(Owner).ClientToScreen(POINT(Left, Top));
42 Left := XPoint.X;
43 Top := XPoint.Y;
44 end;
45 inherited CreateWnd;
46 case FDrawStyle of47 ds_Transparent:
48 SetLayeredWindowAttributes(Handle, ColorToRGB(Color), 255, LWA_COLORKEY);
49 ds_NotDistinctly:
50 SetLayeredWindowAttributes(Handle, 0, 150, LWA_ALPHA);
51 end;
52 end;
53 54 procedure MyTransparentRichEdit.SetDrawStyle(AValue: TDrawStyle);
55 begin56 if FDrawStyle <> AValue then57 begin58 FDrawStyle := AValue;
59 RecreateWnd;
60 end;
61 end;