如何在TEdit控件中设置文本对齐方式
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会发现这个问题的解决方案很有趣:
http://bcbjournal.com/bcbcaq/ ?loc=edits&caq=28
它通过为控件启用
ES_RIGHT
Windows 样式使编辑框右对齐,但它是在创建组件时执行此操作的。由于历史原因,标准 Windows 编辑控件在创建后不支持更改对齐方式(官方是这样),如 这篇关于旧事新事的帖子。然而,正如您从检查各种声明和评论中可以看出的那样,这种情况已经发生了变化,尽管仍然没有记录,但应该是可能的。因此,如果您想在不创建自己的组件的情况下执行此操作,可以使用 Windows API 函数
SetWindowLong
如下所示:请注意,您可能需要致电
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: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.在 Delphi 上,我通过重载 TEdit 类型来做到这一点,以这种方式:
在接口部分,在任何 TForm 声明之前,我放置:
在实现部分,我放置这样的实现:
然后在表单
OnCreate
事件上放这样的话:就是这样。
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:
On the implementation section i put the implementation for such:
Then on the form
OnCreate
event i put something like this: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?要设置对齐属性 - 显示左对齐、居中对齐或右对齐的文本,您可以设置 Alignment 属性,例如,对于名为 Edit1 的编辑控件(它是表单对象的指针成员),将对齐方式设置为右对齐,您可以使用:
其他对齐属性是 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:
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?
我还使得能够全部位于另一个单独的单元中,所以这里是:
这是一个完整的单元,将其保存到名为
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:
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 forTStringGrid.SetCellsAlignment
or read post Delphi: How to make cells' texts in TStringGrid center aligned?