在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, 
00);
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, 
11, FWatermarkHint);
  
end;
end;

end.
posted @ 2010-09-01 16:57  欧阳蓝缺  阅读(268)  评论(0编辑  收藏  举报