unit Unit1;
interface
uses
System.Classes,
Winapi.Windows,
Winapi.Messages,
Vcl.Controls,
Vcl.StdCtrls,
Vcl.Graphics;
type
TEdit =
class(
Vcl.StdCtrls.TEdit )
private
FCanvas : TCanvas;
FMyTextHint:
string;
procedure WMPaint(
var Message: TWMPaint );
message WM_PAINT;
procedure SetMyTextHint(
const Value:
string );
protected
procedure WndProc(
var Message: TMessage );
override;
procedure Paint;
virtual;
procedure PaintWindow(
DC: HDC );
override;
property Canvas: TCanvas
read FCanvas;
public
constructor Create( AOwner: TComponent );
override;
destructor Destroy;
override;
property MyTextHint:
string read FMyTextHint
write SetMyTextHint;
end;
implementation
{ TEdit }
constructor TEdit.Create( AOwner: TComponent );
begin
inherited Create( AOwner );
FCanvas := TControlCanvas.Create;
TControlCanvas( FCanvas ).Control := Self;
end;
destructor TEdit.Destroy;
begin
FCanvas.Free;
inherited;
end;
procedure TEdit.Paint;
var
R: TRect;
begin
R := ClientRect;
Inc( R.Left, 1 );
Inc( R.Top, 1 );
Canvas.Brush.Assign( Self.Brush );
Canvas.Font.Assign( Self.Font );
Canvas.Font.Color := clGray;
DrawText( Canvas.Handle, PChar( FMyTextHint ), -1, R, DT_LEFT
or DT_NOPREFIX
or DT_WORDBREAK
or DrawTextBiDiModeFlagsReadingOnly );
end;
procedure TEdit.PaintWindow(
DC: HDC );
begin
FCanvas.Lock;
try
FCanvas.Handle :=
DC;
try
TControlCanvas( FCanvas ).UpdateTextFlags;
Paint;
finally
FCanvas.Handle := 0;
end;
finally
FCanvas.Unlock;
end;
end;
procedure TEdit.SetMyTextHint(
const Value:
string );
begin
if FMyTextHint <> Value
then
begin
FMyTextHint := Value;
Invalidate;
end;
end;
procedure TEdit.WMPaint(
var Message: TWMPaint );
begin
if Text = '
'
then
begin
ControlState := ControlState + [ csCustomPaint ];
inherited;
ControlState := ControlState - [ csCustomPaint ];
end
else
inherited;
end;
procedure TEdit.WndProc(
var Message: TMessage );
begin
inherited WndProc(
message );
with message do
case Msg
of
CM_MOUSEENTER, CM_MOUSELEAVE, WM_LBUTTONUP, WM_LBUTTONDOWN, WM_KEYDOWN, WM_KEYUP, WM_SETFOCUS, WM_KILLFOCUS, CM_FONTCHANGED, CM_TEXTCHANGED:
begin
Invalidate;
end;
end;
// case
end;
end.