delphi 文本框内容空白时加上提示文字,输入内容时提示文字不显示
需求:
当文本框没有输入内容且输入焦点不在文本框时,显示提示文字:“请输入帐号”
当文本框得到输入焦点时提示文字不显示
实现的思路有两种,
一种是在外部控制,在文本框的OnEnter事件中,将提示文字清除,在OnExit事件中如果文本框的Text为空时给文本框加上提示文字,
而且提示文字的颜色和正常文字的颜色有区别,一般大家使用灰色
另一种是重新制作一个控件,当文本框获取焦点且Text为空时,使用WM_PAINT消息,在其中绘制提示文字
ifNot Focused and (Text='') and (HelpText<>'') then
begin
//绘制提示文字
end;
加入提示文字和提示文字的字体设置这两个属性
//提示性文字字体颜色
property HelpTextFont:TFont read FHelpTextFont write SetHelpTextFont;
//提示性文字
propertyHelpText:WideString read FHelpText write SetHelpText;
我使用自定义控件的方式来实现,重用性比较高
下面贴出这个控件的全部代码:
unit SkinHintEdit;
interface
uses
Windows,Classes,Controls,SysUtils,Messages,StdCtrls,Forms,
Graphics;
const
//提示文字颜色
CONST_DEF_HELPTEXT_FONTCOLOR=clGray;
CONST_DEF_HELPTEXT_FONTNAME='Tahoma';
CONST_DEF_HELPTEXT_FONTSIZE=9;
CONST_DEF_HELPTEXT_FONTSTYLE=[];
type
TSkinHintEdit=class(TCustomEdit)
private
FHelpText: WideString;
FHelpTextFont: TFont;
procedure SetHelpText(const Value: WideString);
procedure SetHelpTextFont(const Value: TFont);
protected
procedure WMPaint(var Message:TMessage);message WM_PAINT;
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
///////////////自定义扩展属性/////////////////
//指导性文字字体颜色
property HelpTextFont:TFont read FHelpTextFont write SetHelpTextFont;
//指导性文字
property HelpText:WideString read FHelpText write SetHelpText;
///////////////默认文本框自带属性////////////
property Align;
property Alignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BevelWidth;
property BiDiMode;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property Ctl3D;
property DoubleBuffered;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property MaxLength;
property NumbersOnly;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentDoubleBuffered;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property TextHint;
property Touch;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGesture;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('SkinEngine',[TSkinHintEdit]);
end;
{ TSkinHintEdit }
constructor TSkinHintEdit.Create(AOwner: TComponent);
begin
FHelpText:='';
//提示文字颜色
FHelpTextFont:=TFont.Create;
FHelpTextFont.Color:=CONST_DEF_HELPTEXT_FONTCOLOR;
FHelpTextFont.Name:=CONST_DEF_HELPTEXT_FONTNAME;
FHelpTextFont.Size:=CONST_DEF_HELPTEXT_FONTSIZE;
FHelpTextFont.Style:=CONST_DEF_HELPTEXT_FONTSTYLE;
inherited Create(AOwner);
end;
destructor TSkinHintEdit.Destroy;
begin
FreeAndNil(FHelpTextFont);
inherited;
end;
procedure TSkinHintEdit.SetHelpText(const Value: WideString);
begin
FHelpText := Value;
end;
procedure TSkinHintEdit.SetHelpTextFont(const Value: TFont);
begin
FHelpTextFont.Assign(Value);
Invalidate;
end;
procedure TSkinHintEdit.WMPaint(var Message: TMessage);
var
ClientDC:HDC;
ClientCanvas:TCanvas;
HelpTextDrawLeft,
HelpTextDrawTop:Integer;
begin
Inherited;
if Not Focused and (Text='') and (HelpText<>'') then
begin
ClientDC:=GetDC(Handle);
Try
ClientCanvas:=TCanvas.Create;
ClientCanvas.Handle:=ClientDC;
ClientCanvas.Font.Assign(FHelpTextFont);
HelpTextDrawLeft:=0;
HelpTextDrawTop:=(Height-ClientCanvas.TextHeight(FHelpText) ) div 2-2;
ClientCanvas.TextOut(HelpTextDrawLeft,HelpTextDrawTop,FHelpText);
Finally
ReleaseDC(Handle,ClientDC);
ClientCanvas.Free;
End;
end;
end;
end.