在TEdit中显示水印提示
unit MyEdit;
interface
uses Messages, Classes, StdCtrls, Controls, Graphics;
type
TMyEdit = class(TEdit)
private
FCanvas: TControlCanvas;
FWatermarkHint: string;
protected
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property WatermarkHint: string read FWatermarkHint write FWatermarkHint;
end;
implementation
{ TMyEdit }
procedure TMyEdit.CMExit(var Message: TCMExit);
begin
inherited;
Perform(WM_PAINT, 0, 0);
end;
constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCanvas := TControlCanvas.Create;
FCanvas.Control := Self;
end;
destructor TMyEdit.Destroy;
begin
FCanvas.Free;
inherited;
end;
procedure TMyEdit.WMPaint(var Message: TWMPaint);
begin
inherited;
if (not Focused) and (Self.Text = '') and (FWatermarkHint <> '') then
begin
FCanvas.Font := Self.Font;
FCanvas.Font.Color := clGrayText;
FCanvas.TextRect(Self.ClientRect, 1, 1, FWatermarkHint);
end;
end;
end.