delphi Memo加个PromptText


本文整理自网络,侵删。

 TEdit有PromptText属性,用来显示一个提示,又不影响输入。TMemo没有,在官方提了需求也没响应。实在没办法,自己加个。话不多说,上代码:

type
  TForm6 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Memo1ApplyStyleLookup(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FPrompt:TText;//用他来显示PromptText文字
  public
    { Public declarations }
  end;

var
  Form6: TForm6;

implementation

{$R *.fmx}

procedure TForm6.Button1Click(Sender: TObject);
begin
   FPrompt.Text:='输入内容....';//显示提示文字
end;

procedure TForm6.Button2Click(Sender: TObject);
begin
 FPrompt.Text:='';//隐藏提示文字
end;

//处理TMemo.ApplyStyleLookup事件,在背景上写文字对象
procedure TForm6.Memo1ApplyStyleLookup(Sender: TObject);
var
  o:TLayout;
begin
    //建产文字对象,风格你自己定.
    if not Assigned(FPrompt) then
    begin
      FPrompt:=TText.Create(Self);
      FPrompt.Align:=TAlignLayout.Left;
      FPrompt.Width:=TMemo(Sender).Width;
      FPrompt.TextSettings.HorzAlign:=TTextAlign.Leading;
      FPrompt.TextSettings.FontColor:=TAlphaColorRec.Red;
      FPrompt.Margins.Left:=2;
      FPrompt.HitTest:=False;
    end;
    o := (Sender as TMemo).FindStyleResource('content') as TLayout ;
    FPrompt.Parent:=o;//将FPrompt显示到Memo的背景上
end;

end.

感谢清幽傲竹,告诉我用ApplyStyleLookup事件来取到背景对象!

相关阅读 >>

Delphi hexstrtobytes

Delphi 用idhttp获取utf-8编码的网页

Delphi fmx窗体中控件对齐方式介绍

Delphi获取jpg图片的高度、宽度

Delphi 免杀下载者源代码

Delphi idtcp上传文件

Delphi 快速选择文件夹路径

Delphi中updown组件的使用方法

Delphi服务程序(service)的调试方法

Delphi memo1 行倒序排列三种方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...