打开窗体时Edit是这样的:如何点击Edit后这样了:没输入任何内容,鼠标离开(Edit失去焦点)后又恢复了:很多搜索框都有上面类似功能的,就是不知大家是怎么实现的,所有发帖求提示求方法
(我想到一个比较麻烦的办法是在Edit上加个Label,当鼠标点击进入时就隐藏label,当鼠标离开时判断是否有输入内容,没时就显示label)
(我想到一个比较麻烦的办法是在Edit上加个Label,当鼠标点击进入时就隐藏label,当鼠标离开时判断是否有输入内容,没时就显示label)
解决方案 »
- 如何禁止模拟键盘输入
- var buf 什么意思?
- 大家说说严隆平这个技术人员的年收入有马明哲高吗?
- $是啥意思?大家帮忙
- 使用QuickRep如何打印出“网格”
- 为什么在MDI主窗体上放一背景图片后,子窗体显示在图片后面呢?
- 有关QuickReport两个简单问题!
- 公司要我开发一套基于 GPRS 技术的远程抄表系统的演示软件,我现在无从下手
- 在TMemo 控件中如何动态把一个英文单词,或者汉字变色,例如"中国程序员"动态的把"国"字变成红色,如何实现
- 访问数据库,BDE的性能好还是DAO的性能好?
- 截取字符串的问题
- delphi xe2 生成的文件不直接支持unicode吗?怎么在英文XP下中文还是乱码?难道还需装中文语言包?
OnEnter时判断是否是你的提示信息 是则显示空
OnExit时判断是否空 空则继续显示你的提示信息
初始状态:
image.Visible:=True;
edit.Visible:=False;
在image的onClick事件中:
edit.Visible:=True;
image.Visible:=False;
edit.SetFocus;
在edit的OnExit事件中:
if Trim(edit.Text)='' then
begin
edit.Visible:=False;
image.Visible:=True;
end;
Edit1.Text=strHintEdit1的onEnter:
if Edit1.Text=strHint then
Edit1.Text:='';
Edit1.Font.Color:= clWindowText;Edit1的onExit:
if Edit1.Text='' then
Edit1.Text:=strHint;
if Edit1.Text=strHint then
Edit1.Font.Color:= clGrayText;
private
FCanvas: TControlCanvas;
FImages: TCustomImageList;
FImageChangeLink: TChangeLink;
FLeftButton: TEditButton;
FRightButton: TEditButton;
function AdjustTextHint(Margin: Integer; const Value: string): string;
procedure ImageListChange(Sender: TObject);
procedure SetImages(const Value: TCustomImageList);
function GetOnLeftButtonClick: TNotifyEvent;
function GetOnRightButtonClick: TNotifyEvent;
procedure SetLeftButton(const Value: TEditButton);
procedure SetOnLeftButtonClick(const Value: TNotifyEvent);
procedure SetOnRightButtonClick(const Value: TNotifyEvent);
procedure SetRightButton(const Value: TEditButton);
protected
procedure DoSetTextHint(const Value: string); override;
function GetEditButtonClass: TEditButtonClass; dynamic;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure UpdateEditMargins; dynamic;
procedure WndProc(var Message: TMessage); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DefaultHandler(var Message); override;
property Images: TCustomImageList read FImages write SetImages;
property LeftButton: TEditButton read FLeftButton write SetLeftButton;
property RightButton: TEditButton read FRightButton write SetRightButton;
property OnLeftButtonClick: TNotifyEvent read GetOnLeftButtonClick write SetOnLeftButtonClick;
property OnRightButtonClick: TNotifyEvent read GetOnRightButtonClick write SetOnRightButtonClick;
end; TButtonedEdit = class(TCustomButtonedEdit)
published
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 Images;
property ImeMode;
property ImeName;
property LeftButton;
property MaxLength;
property OEMConvert;
property NumbersOnly;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentDoubleBuffered;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property RightButton;
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 OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnGesture;
property OnLeftButtonClick;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnRightButtonClick;
property OnStartDock;
property OnStartDrag;
end;
constructor TCustomButtonedEdit.Create(AOwner: TComponent);
begin
inherited;
FCanvas := TControlCanvas.Create;
FCanvas.Control := Self;
FImageChangeLink := TChangeLink.Create;
FImageChangeLink.OnChange := ImageListChange;
FLeftButton := GetEditButtonClass.Create(Self, bpLeft);
FRightButton := GetEditButtonClass.Create(Self, bpRight);
end;destructor TCustomButtonedEdit.Destroy;
begin
FreeAndNil(FCanvas);
FreeAndNil(FImageChangeLink);
FreeAndNil(FLeftButton);
FreeAndNil(FRightButton);
inherited;
end;function TCustomButtonedEdit.AdjustTextHint(Margin: Integer; const Value: string): string;
var
LWidth, Count: Integer;
begin
if (Margin = 0) or (Win32MajorVersion >= 6) then
inherited DoSetTextHint(Value)
else
begin
// This is a hack!! Due to a presumed bug in Windows XP any text hint
// set with EM_SETCUEBANNER does not respect left margins set with
// EM_SETMARGINS. The following works around the issue.
FCanvas.Font := Font;
LWidth := FCanvas.TextWidth(' '); // do not localize
Count := Margin div LWidth;
if (Margin mod LWidth) > 0 then
Inc(Count);
inherited DoSetTextHint(StringOfChar(' ', Count) + Value);
end;
end;procedure TCustomButtonedEdit.DoSetTextHint(const Value: string);
begin
AdjustTextHint(0, Value);
end;function TCustomButtonedEdit.GetEditButtonClass: TEditButtonClass;
begin
Result := TEditButton;
end;function TCustomButtonedEdit.GetOnLeftButtonClick: TNotifyEvent;
begin
Result := LeftButton.Glyph.OnClick;
end;function TCustomButtonedEdit.GetOnRightButtonClick: TNotifyEvent;
begin
Result := RightButton.Glyph.OnClick;
end;procedure TCustomButtonedEdit.ImageListChange(Sender: TObject);
begin
if HandleAllocated then
begin
FLeftButton.UpdateBounds;
FRightButton.UpdateBounds;
UpdateEditMargins;
end;
end;procedure TCustomButtonedEdit.DefaultHandler(var Message);
{$IF DEFINED(CLR)}
var
LMessage: TMessage;
{$IFEND}
begin
inherited;
{$IF DEFINED(CLR)}
LMessage := UnwrapMessage(TObject(Message));
case LMessage.Msg of
{$ELSE}
case TMessage(Message).Msg of
{$IFEND}
CN_CTLCOLOREDIT:
begin
FLeftButton.Glyph.Invalidate;
FRightButton.Glyph.Invalidate;
end;
WM_SIZE: FRightButton.UpdateBounds;
end;
end;procedure TCustomButtonedEdit.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if Operation = opRemove then
begin
if AComponent = FImages then
begin
FImages := nil;
FLeftButton.UpdateBounds;
FRightButton.UpdateBounds;
UpdateEditMargins;
end
else if (LeftButton <> nil) and (AComponent = LeftButton.DropDownMenu) then
LeftButton.DropDownMenu := nil
else if (RightButton <> nil) and (AComponent = RightButton.DropDownMenu) then
RightButton.DropDownMenu := nil;
end;
end;procedure TCustomButtonedEdit.SetImages(const Value: TCustomImageList);
begin
if Value <> FImages then
begin
if FImages <> nil then
FImages.UnRegisterChanges(FImageChangeLink);
FImages := Value;
if FImages <> nil then
begin
FImages.RegisterChanges(FImageChangeLink);
FImages.FreeNotification(Self);
end;
FLeftButton.UpdateBounds;
FRightButton.UpdateBounds;
UpdateEditMargins;
end;
end;procedure TCustomButtonedEdit.SetLeftButton(const Value: TEditButton);
begin
FLeftButton.Assign(Value);
end;procedure TCustomButtonedEdit.SetOnLeftButtonClick(const Value: TNotifyEvent);
begin
LeftButton.Glyph.OnClick := Value;
end;procedure TCustomButtonedEdit.SetOnRightButtonClick(const Value: TNotifyEvent);
begin
RightButton.Glyph.OnClick := Value;
end;procedure TCustomButtonedEdit.SetRightButton(const Value: TEditButton);
begin
FRightButton.Assign(Value);
end;procedure TCustomButtonedEdit.UpdateEditMargins;
var
LMargin, RMargin: Integer;
begin
if HandleAllocated then
begin
LMargin := 0;
RMargin := 0;
if (Images <> nil) then
begin
if LeftButton.Visible then
LMargin := Images.Width + 2;
if RightButton.Visible then
RMargin := Images.Width + 2;
end;
SendMessage(Handle, EM_SETMARGINS, EC_LEFTMARGIN or EC_RIGHTMARGIN, MakeLong(LMargin, RMargin));
AdjustTextHint(LMargin, TextHint);
Invalidate;
end;
end;procedure TCustomButtonedEdit.WndProc(var Message: TMessage);
var
LLeft, LTop: Integer;
begin
case Message.Msg of
CN_CTLCOLORSTATIC,
CN_CTLCOLOREDIT:
if FImages <> nil then
begin
if LeftButton.Visible then
begin
LLeft := LeftButton.Glyph.Left;
LTop := LeftButton.Glyph.Top;
if ThemeServices.ThemesEnabled and Ctl3D then
begin
Inc(LLeft);
Inc(LTop);
end;
ExcludeClipRect(Message.WParam, LLeft + 1, LTop + 1,
LeftButton.Glyph.Width + LeftButton.Glyph.Left, LeftButton.Glyph.Height);
end; if RightButton.Visible then
begin
LTop := RightButton.Glyph.Top;
if ThemeServices.ThemesEnabled and Ctl3D then
Inc(LTop);
ExcludeClipRect(Message.WParam, RightButton.Glyph.Left, LTop + 1,
RightButton.Glyph.Width + RightButton.Glyph.Left, RightButton.Glyph.Height);
end;
end;
end; inherited; case Message.Msg of
CM_BORDERCHANGED,
CM_CTL3DCHANGED:
begin
if not (csLoading in ComponentState) then
begin
LeftButton.UpdateBounds;
RightButton.UpdateBounds;
end;
end;
CM_FONTCHANGED:
if not (csLoading in ComponentState) then
UpdateEditMargins;
end;
end;