Mega Code Archive

 
Categories / Delphi / VCL
 

Building a Transparent Edit Control

Title: Building a Transparent Edit Control Question: How can I build a transparent edit control? Answer: Building a transparent edit control can be the cool face of your application. Why? Because your entry forms can look like "paper forms". Adding this cool features for your application can be very easy, as you will see bellow. First of all I want to specify that is not my original code. I have found a transparent edit, listbox, checkbox and memo controls in FusedControls, but the main goal of this library is the flicker. Is impossible for anyone to distribute a applicatin with this controls because the flicker is in supportable. An now, go to work. All we have to do is to overrwide the CreateParams and some common events. In order to resolve the flicker I have decide to let our edit control transparent only when the focus is not on it. So, I have override the DoEnter and DoExit. The entire code is bellow. Just copy, paste in a blank unit and install the component. unit RbsWiredEdit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; const TWM_RbsInvalidate=WM_USER+1; type TRbsWiredEdit = class(TEdit) private { Private declarations } procedure RbsInvalidate(var Message:TMessage); message TWM_RbsInvalidate; procedure CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT); message CN_CTLCOLOREDIT; procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND; procedure WMMove(var Message: TMessage); message WM_MOVE; protected { Protected declarations } FTransparent: boolean; procedure CreateWnd; override; procedure CreateParams(var Params: TCreateParams); override; procedure DoExit; override; procedure DoEnter; override; public { Public declarations } constructor Create(AOwner: TComponent); override; procedure Invalidate; override; published { Published declarations } end; procedure Register; implementation constructor TRbsWiredEdit.Create(AOwner:TComponent); begin inherited create(AOwner); ftransparent:=true; end; procedure TRbsWiredEdit.CreateWnd; begin inherited CreateWnd; if fTransparent then begin SetWindowLong(Parent.Handle, GWL_STYLE, GetWindowLong(Parent.Handle, GWL_STYLE) and not WS_CLIPCHILDREN); end; end; procedure TRbsWiredEdit.RbsInvalidate(var Message:TMessage); var r:TRect; begin if (Parentnil) and FTransparent then begin r:=ClientRect; r.TopLeft:=Parent.ScreenToClient(ClientToScreen(r.TopLeft)); r.BottomRight:=Parent.ScreenToClient(ClientToScreen(r.BottomRight)); RedrawWindow(Handle,nil,0,RDW_FRAME+RDW_INVALIDATE); end; end; procedure TRbsWiredEdit.CNCTLCOLOREDIT(var Message:TWMCTLCOLOREDIT); begin if FTransparent then with Message do begin SetBkMode(ChildDC,Windows.TRANSPARENT); Result:=GetStockObject(HOLLOW_BRUSH) end else inherited; end; procedure TRbsWiredEdit.WMEraseBkgnd(var Message:TWMERASEBKGND); begin if FTransparent and not (csDesigning in ComponentState) then PostMessage(Handle,TWM_RbsInvalidate,0,0) else inherited; end; procedure TRbsWiredEdit.WMMove(var message:TMessage); begin inherited; if FTransparent then SendMessage(Handle,TWM_RbsInvalidate,0,0) else Invalidate; end; procedure TRbsWiredEdit.CreateParams(var Params:TCreateParams); begin inherited CreateParams(Params); if (CsDesigning in ComponentState) then exit; with Params do begin ExStyle:=ExStyle or WS_EX_TRANSPARENT; end; end; procedure TRbsWiredEdit.DoExit; begin inherited; FTransparent:=true; SetCursor(0); RecreateWnd; end; procedure TRbsWiredEdit.DoEnter; var exstyle,stdstyle:longint; begin inherited; Ftransparent:=false; StdStyle:= Windows.GetWindowLong(handle, GWL_EXSTYLE); exStyle:= StdStyle and not WS_EX_TRANSPARENT; Windows.SetWindowLong(handle, GWL_EXSTYLE, exStyle); invalidate; end; procedure TRbsWiredEdit.Invalidate; begin if FTransparent then SendMessage(Handle,TWM_RbsInvalidate,0,0) else inherited; end; procedure Register; begin RegisterComponents('Rombest', [TRbsWiredEdit]); end; end.