Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#16

AW: Leeres Eingabefeld mit Standardwert füllen

  Alt 24. Dez 2015, 10:25
Nun ja, es braucht da keine Stunden an Entwicklungszeit
Delphi-Quellcode:
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.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat