{******************************************************************}
{ EditUtil.pas                                                     }
{                                                                  }
{ Author    : A.Nasir Senturk                                      }
{ Home Page : http://www.shenturk.com                              }
{ Email     : shenturk@gmail.com                                   }
{                                                                  }
{ Date      : 21.01.2007                                           }
{                                                                  }
{ Sizden iki şey rica edicem:                                      }
{ 1. Lutfen bu baslik kismini kaldirmayiniz.                       }
{ 2. Mumkunse bagis yapiniz.                                       }
{ *****************************************************************}

unit EditUtil;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, GdipApi, GdipObj, DirectDraw, TextUtil, ExtCtrls, WinInet,
  InetUtil, InetThrd;

type
  TGdiEdit = class(TWinControl)
  //TGdiEdit = class(TEdit)
  private
    FCanvas: TGPGraphics;
    FBuffer: TGPBitmap;
    FEdit: TEdit;
    procedure AllocateHandle;
    procedure ReleaseHandle;
  protected
    procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
    procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
    procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
    procedure CreateWnd; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Paint(Canvas: TGPGraphics); virtual;
    property Edit: TEdit read FEdit write FEdit;
    property Font;
  end;

implementation

{ TGdiEdit }

procedure TGdiEdit.AllocateHandle;
begin
  FBuffer := TGPBitmap.Create(Width, Height, PixelFormat32bppARGB);
  FCanvas := TGPGraphics.Create(FBuffer);
end;

constructor TGdiEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Font.Name := 'Arial';//'Trebuchet MS';
  Font.Size := 14;
  Font.Style := [fsBold];
  DoubleBuffered := True;
  ControlStyle := ControlStyle - [csOpaque] + [csParentBackground];
end;

procedure TGdiEdit.CreateWnd;
begin
  inherited CreateWnd;
  SetWindowLong(Parent.Handle, GWL_STYLE, GetWindowLong(Parent.Handle,
    GWL_STYLE) and not WS_CLIPCHILDREN);
end;

destructor TGdiEdit.Destroy;
begin
  ReleaseHandle;
  inherited Destroy;
end;

procedure TGdiEdit.Paint(Canvas: TGPGraphics);
begin
  Canvas.DrawImage(FBuffer, Left, Top, Width, Height);
end;

procedure TGdiEdit.ReleaseHandle;
begin
  FreeAndNil(FBuffer);
  FreeAndNil(FCanvas);
end;

procedure TGdiEdit.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin

end;

procedure TGdiEdit.WMPaint(var Message: TWMPaint);
var
  DC: HDC;
  PS: TPaintStruct;
  //Canvas: TGPGraphics;
  SolidBrush: TGPSolidBrush;
  GdiColor: TGPColor;
begin
  ReleaseHandle;
  AllocateHandle;
  DC := BeginPaint(Handle, PS);
  if DC = 0 then;
  //Canvas := TGPGraphics.Create(Handle, False);
  //Canvas := TGPGraphics.Create(DC);
  SolidBrush := TGPSolidBrush.Create(MakeColor(255, 255, 255, 255));
  try
    FCanvas.FillRectangle(SolidBrush, 0, 0, Width, Height);
    GdiColor := aclBlack;
    if FEdit.SelLength > 0 then
      GdiColor := aclNavy;
    if FEdit.Focused then
      GdiPlusDrawText(FCanvas, FEdit.Text + '_', MakeRectF(ClientRect), Font,
        StringAlignmentCenter, GdiColor, False)
    else
      GdiPlusDrawText(FCanvas, FEdit.Text, MakeRectF(ClientRect), Font,
        StringAlignmentCenter, GdiColor, False);
    //Paint(FCanvas);
    //Canvas.DrawImage(FBuffer, 0, 0, FBuffer.GetWidth, FBuffer.GetHeight);
  finally
    //Canvas.Free;
    SolidBrush.Free;
  end;
  EndPaint(Handle, PS);
  Message.Result := 0;
end;

procedure TGdiEdit.WMWindowPosChanging(var Message: TWMWindowPosChanging);
var
  Rect : TRect;
begin
  Rect := ClientRect;
  InvalidateRect(Handle, @Rect, False);
end;


end.
