如何在TEdit控件中设置文本对齐 [英] How to set textalignment in TEdit control

查看:237
本文介绍了如何在TEdit控件中设置文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是turbo c ++ explorer版本(免费版)。是否有人知道我如何设置TEdit控件的textAlignment?

I'm using turbo c++ explorer edition (the free edition). Is there somebody that know how i can set the textAlignment of a TEdit control?

推荐答案

在Delphi我通过重载TEdit类型,以这种方式:

On Delphi i do it by overloading TEdit type, in this way:

在接口部分,在任何TForm声明之前:

On interface section, before any TForm declaration i put:

type
    TEdit=class(StdCtrls.TEdit)
    private
      FAlignment:TAlignment;
      procedure SetAlignment(Value:TAlignment);
    protected
      procedure CreateParams(var Params:TCreateParams);override;
    public
      constructor Create(AOwner:TComponent);override;
    published
      property Alignment:TAlignment read FAlignment write SetAlignment default taLeftJustify;
    end;

在实现部分,我把这样的实现:

On the implementation section i put the implementation for such:

procedure TEdit.SetAlignment(Value:TAlignment);
begin
     if FAlignment<>Value
     then begin
               FAlignment:=Value;
               RecreateWnd;
          end;
end;

procedure TEdit.CreateParams(var Params:TCreateParams);
const
     Alignments:array[TAlignment] of Cardinal=(ES_LEFT,ES_RIGHT,ES_CENTER);
begin
     inherited CreateParams(Params)
     Params.Style:=Params.Style or Alignments[FAlignment];
end;

constructor TEdit.Create(AOwner:TComponent);
begin
     inherited Create(AOwner);
     FAlignment:=taLeftJustify;
end;

然后在表单 OnCreate 像这样:

MyEditBox.Alignment:=taLeftJustify;
MyEditBox.Alignment:=taCenter;
MyEditBox.Alignment:=taRightJustify;

就是这样。

它可以改进很多,它只是一个概念验证添加到TEdit,它不是关于创建一个新类(与其他名称),也不是关于创建一个新的组件。

Pelase note it can be improved a lot, it is just a proof-of-concept of adding it to TEdit, it is not about creating a new class (with other name) and neither it is about creating a new component.

希望这可以对某人有用。

Hope this can be usefull to someone.

PD:同样的想法可以为TStringGrid做,只是搜索stackoverflow .com CellsAlignment 或阅读职位 Delphi:如何使单元格的文本在TStringGrid中心对齐?

P.D.: The same idea is possible to be done for TStringGrid, just search on stackoverflow.com for CellsAlignment or read post Delphi : How to make cells' texts in TStringGrid center aligned?

这篇关于如何在TEdit控件中设置文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆