打开窗体时Edit是这样的:如何点击Edit后这样了:没输入任何内容,鼠标离开(Edit失去焦点)后又恢复了:很多搜索框都有上面类似功能的,就是不知大家是怎么实现的,所有发帖求提示求方法
(我想到一个比较麻烦的办法是在Edit上加个Label,当鼠标点击进入时就隐藏label,当鼠标离开时判断是否有输入内容,没时就显示label)

解决方案 »

  1.   

    放一个图片和两个重叠的文本框,获得焦点的时候,把图片和有文字的文本框隐藏掉,失去焦点则把他show出来
      

  2.   

    借助edit的tag属性,tag属性默认为0,当输入文字后,tag :=1,鼠标离开时,如果没有内容,判断tag :=0,然后显示默认文字
      

  3.   

    不知是否有现成的实现该功能的Edit控件呢?
      

  4.   

    edit的事件
    OnEnter时判断是否是你的提示信息 是则显示空
    OnExit时判断是否空 空则继续显示你的提示信息
      

  5.   

    用一个image和一个edit,image和edit大小一致,并重叠。
    初始状态:
      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;
      

  6.   

    用onEnter,onExit事件const strHint='请输入关键字查找...';Form1的onCreate:
      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;
      

  7.   

    不过要那个搜索的图标,就要用到图片覆盖了,改变图片的Visible
      

  8.   

    自己派生一个新类出来。如果是 DELPHI XE的话,本身有这个组件 TButtonedEdit  TCustomButtonedEdit = class(TCustomEdit)
      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;
      

  9.   

    图片可以放在Edit的前边 里边的文字可以在进入和退出事情中处理