• 全部
  • 语言基础/算法/系统设计
  • 数据库相关
  • 图形处理/多媒体
  • 网络通信/分布式开发
  • VCL组件开发及应用
  • Windows SDK/API
  • 问答

如何使TEdit中的文字右对齐?

gdx 2001-06-03 09:37:00
用其他控件不算!
...全文
127 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
rh 2001-06-03
如何使TEDIT控件中的TEXT显示在控件的中间或下面(一般在上面),
像TLABEL中的LAYOUT至为CENTER一样
var
Rct: TRect;
begin
Rct := edit1.ClientRect;
Inc(Rct.Top, 3);
sendmessage(edit1.handle, EM_SETRECT, 0, Integer(@Rct));
end;
EM_SETRECT 只对于 Multiline Edit 有效? :-)
An application sends an EM_SETRECT message to set the formatting rectangle of a
multiline edit control.
换成 Memo 就可以了。
procedure TForm1.Button2Click(Sender: TObject);
var
Rct: TRect;
begin
SendMessage(Memo1.Handle, EM_GETRECT, 0, Integer(@Rct));
Inc(Rct.Top, 5);
SendMessage(Memo1.Handle, EM_SETRECT, 0, Integer(@Rct));
end;
那也简单, 那些语句前加一句
SetWindowLong(edit.Handle,GWL_STYLE, GetWindowLong(edit.Handle, GWL_STYLE) or ES_MULTILINE);
不过需要多写个OnKeyPress事件过滤掉输入的回车符.
最好自己继承TEdit做个有这功能的控件. 事实上很简单的.
unit JackEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TKeySet = (ksReturn, ksEscape);
TKeySets = set of TKeySet;

TJackEdit = class(TEdit)
private
{ Private declarations }
FAlignment: TAlignment;
FKeys: TKeySets;

procedure SetAlignment(value: TAlignment);
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure KeyPress(var Key: Char); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property Keys: TKeySets read FKeys write FKeys;
end;


procedure Register;

implementation

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

procedure TJackEdit.KeyPress(var Key: Char);
begin
if ksReturn in Keys then
begin
if Key = Chr(Vk_Return) then
begin
Key := Chr(0);
(Owner as TControl).Perform(wm_NextDlgCtl,0,0);
end;
end;
if ksEscape in Keys then
begin
if Key = Chr(Vk_Escape) then
begin
Key := Chr(0);
(Owner as TControl).Perform(wm_NextDlgCtl,1,0);
end;
end;
inherited KeyPress(Key);
end;

procedure TJackEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
case Alignment of
taLeftJustify : Params.Style := Params.Style or (ES_LEFT or Es_MULTILINE);
taRightJustify : Params.Style := Params.Style or (ES_RIGHT or ES_MULTILINE);
taCenter : Params.Style := Params.Style or (ES_CENTER or Es_MULTILINE);
end;
end;

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

destructor TJackEdit.Destroy;
begin
inherited Destroy;
end;

procedure Register;
begin
RegisterComponents('Jack', [TJackEdit]);
end;

end.
回复
rh 2001-06-03
继承TEdit,重载CreateParams,修改Param即可。
procedure TEdit_Ex.CreateParams(var Params: TCreateParams);
begin
inherited;
if self.FTextOnRight then Params.Style:=Params.Style or ES_RIGHT;
end;
回复
gdx 2001-06-03
我以前曾用VC,VC中的EDITCtrl可在SetExtendStyle中设Alignment参数,在Delphi中,我想
是否可在CreateParam中修改,请给出代码!顺便给TEdit加上多行显示功能.
还请大虾指点!
回复
ghz2000 2001-06-03
看来要在前面补空格了(在onchange)
回复
xycleo 2001-06-03
up
回复
gdx 2001-06-03
我知道了
回复
gdx 2001-06-03
请问如何加分?
Answer也会加分
回复
gdx 2001-06-03
回复
gdx 2001-06-03
回复
发帖
Delphi
创建于2007-08-02

4870

社区成员

Delphi 开发及应用
申请成为版主
帖子事件
创建了帖子
2001-06-03 09:37
社区公告
暂无公告