如何在TEdit控件中设置文本对齐方式

发布于 2024-10-08 00:13:29 字数 70 浏览 3 评论 0原文

我正在使用 Turbo C++ 资源管理器版(免费版)。有人知道如何设置 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此刻的回忆 2024-10-15 00:13:29

您可能会发现这个问题的解决方案很有趣:

http://bcbjournal.com/bcbcaq/ ?loc=edits&caq=28

它通过为控件启用 ES_RIGHT Windows 样式使编辑框右对齐,但它是在创建组件时执行此操作的。由于历史原因,标准 Windows 编辑控件在创建后不支持更改对齐方式(官方是这样),如 这篇关于旧事新事的帖子。然而,正如您从检查各种声明和评论中可以看出的那样,这种情况已经发生了变化,尽管仍然没有记录,但应该是可能的。

因此,如果您想在不创建自己的组件的情况下执行此操作,可以使用 Windows API 函数 SetWindowLong 如下所示:

DWORD alignment = ES_RIGHT;
DWORD oldStyle = GetWindowLong(Edit1->Handle, GWL_STYLE);
DWORD newStyle = (oldStyle & ~(ES_LEFT | ES_CENTER | ES_RIGHT)) | alignment;
SetWindowLong(Edit1->Handle, GWL_STYLE, newStyle);

请注意,您可能需要致电 SetWindowPos 以使更改生效,如前面链接的帖子中的评论所述文本。

You might find this solution to the problem interesting:

http://bcbjournal.com/bcbcaq/?loc=edits&caq=28

It makes the edit box right aligned by enabling the ES_RIGHT Windows style for the control, however it does this when creating the component. For historical reasons the standard windows edit control does not support changing alignment after it has been created (officially that is) as mentioned in this post on The Old New Thing. However as you can tell from examining various claims and comments this has changed and though still undocumented should be possible.

So if you want to do this without creating your own component you can use the Windows API function SetWindowLong like this:

DWORD alignment = ES_RIGHT;
DWORD oldStyle = GetWindowLong(Edit1->Handle, GWL_STYLE);
DWORD newStyle = (oldStyle & ~(ES_LEFT | ES_CENTER | ES_RIGHT)) | alignment;
SetWindowLong(Edit1->Handle, GWL_STYLE, newStyle);

Please note you might have to call SetWindowPos for the changes to take effect, as noted in the comments in the post linked earlier in the text.

仅此而已 2024-10-15 00:13:29

在 Delphi 上,我通过重载 TEdit 类型来做到这一点,以这种方式:

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

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;

在实现部分,我放置这样的实现:

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;

就是这样。

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

希望这对某人有用。

PD:对于 TStringGrid 也可以实现相同的想法,只需在 stackoverflow.com 上搜索 CellsAlignment 或阅读帖子 Delphi:如何使 TStringGrid 中的单元格文本居中对齐?

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

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;

Then on the form OnCreate event i put something like this:

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

That is it.

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.

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?

葵雨 2024-10-15 00:13:29

要设置对齐属性 - 显示左对齐、居中对齐或右对齐的文本,您可以设置 Alignment 属性,例如,对于名为 Edit1 的编辑控件(它是表单对象的指针成员),将对齐方式设置为右对齐,您可以使用:

Edit1->Alignment = taRightJustify;

其他对齐属性是 taLeftJustify 和 taCenter。

这仅影响控件上字符的位置,而不会影响从右到左或从左到右对齐的情况。这可以通过设置 BiDiMode 属性 - bdLeftToRight、bdRightToLeft 进行调整(这些在从左到右和从右到左的语言中更重要)

或者我是否在问题中遗漏了一些明显的东西?

To set the alignment property - which displays the text either left, center or right aligned, you set the Alignment property, e.g. for an edit control called Edit1, which is a pointer member of the form object, to set the alignment to right-justification, you would use:

Edit1->Alignment = taRightJustify;

the other justification properties are taLeftJustify and taCenter.

This only affects the location of the characters on the control, not if it's right-to-left or left-to-right aligned. That can be adjusted by setting the BiDiMode property - bdLeftToRight, bdRightToLeft (these are more important in left-to-right and right-to-left languages)

Or am I missing something obvious in the question?

俏︾媚 2024-10-15 00:13:29

我还使得能够全部位于另一个单独的单元中,所以这里是:

unit AlignedTEdit;

interface

uses Windows,Classes,Controls,StdCtrls;

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;

implementation

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;

end.

这是一个完整的单元,将其保存到名为 AlignedTEdit.pas 的文件中。

然后,在任何表单上,您都有一个 TEdit 添加 ,AlignedTEdit 在界面使用子句的末尾。

PD:对于 TStringGrid 也可以实现相同的想法,只需在 stackoverflow.com 上搜索 TStringGrid.SetCellsAlignment 或阅读帖子 Delphi:如何使 TStringGrid 中的单元格文本居中对齐?

I have also make able to be all in another separated unit, so here it is:

unit AlignedTEdit;

interface

uses Windows,Classes,Controls,StdCtrls;

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;

implementation

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;

end.

This is a whole unit, save it to a file called AlignedTEdit.pas.

Then on any form you have a TEdit add ,AlignedTEdit at the end of the interface uses clause.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文