怎样让Edit中的Text居中显示,默认都是靠左的,请问怎样居中! ? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不带DB的可能没有!可能考虑用第三方控件,如:rx Express Editors控件可以实现,你想实现看看源码也许有用 http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=7946 那个FAQ里面说的很清楚了吧,就是重新覆盖edit的createparam过程,使其拥有Center的样式 unit QFltEdit;interfaceuses Windows, Messages, SysUtils, Classes,StdCtrls,Controls,Forms;type TFloatEdit = class(TCustomEdit) private { Private declarations } FPrecision:Integer; FTextAlign:TAlignment; //procedure SetPrecision(value:Integer); procedure SetTextAlign(value:TAlignment); procedure WMChar(var Message: TWMChar); message WM_CHAR; protected { Protected declarations } function DoKeyPress(var Message: TWMKey): Boolean; public { Public declarations } constructor Create(AOwner:TComponent);override; procedure CreateParams(var Params: TCreateParams); override; function TextAsInteger:Integer; function TextAsFloat:Extended; published { Published declarations } property Precision:Integer read FPrecision write FPrecision default 2; property TextAlign:TAlignment read FTextAlign write SetTextAlign ; property Anchors; property AutoSelect; property AutoSize; property BorderStyle; property CharCase; property Color; property Constraints; property DragMode; property Enabled; property Font; property HideSelection; property MaxLength; property ParentColor; property ParentFont; property ParentShowHint; property PopupMenu; property ReadOnly; property ShowHint; property TabOrder; property TabStop; property Text; property Visible; property OnChange; property OnClick; property OnContextPopup; property OnDblClick; property OnDragDrop; property OnDragOver; property OnEndDrag; property OnEnter; property OnExit; property OnKeyDown; property OnKeyPress; property OnKeyUp; property OnMouseDown; property OnMouseMove; property OnMouseUp; property OnStartDrag; end;implementationuses Tools;{VK_NUMPAD0 0 key (numeric keypad)VK_NUMPAD1 1 key (numeric keypad)VK_NUMPAD2 2 key (numeric keypad)VK_NUMPAD3 3 key (numeric keypad)VK_NUMPAD4 4 key (numeric keypad)VK_NUMPAD5 5 key (numeric keypad)VK_NUMPAD6 6 key (numeric keypad)VK_NUMPAD7 7 key (numeric keypad)VK_NUMPAD8 8 key (numeric keypad)VK_NUMPAD9 9 key (numeric keypad)}function TFloatEdit.TextAsInteger:Integer;begin try Result:=Round(StrToFloat(Text)); Except on EConvertError do Result:=0; end;end;function TFloatEdit.TextAsFloat:Extended;begin try Result:=StrToFloat(Text); Except on EConvertError do Result:=0.0; end;end;function TFloatEdit.DoKeyPress(var Message: TWMKey): Boolean;begin Result:=false; Message.Result:=1; if (StrScan(PChar(Text),'.')<>nil) and (Char(Message.CharCode) = '.' )or not(Char(Message.CharCode) in ['0'..'9','.'])and not (Message.CharCode in [VK_BACK]) then begin Message.Result:=0; Result:=true; end;end;procedure TFloatEdit.WMChar(var Message:TWMKey);begin if not DoKeyPress(Message) then inherited;end;procedure TFloatEdit.CreateParams(var Params: TCreateParams);const Alignments: array[Boolean, TAlignment] of DWORD = ((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));begin inherited CreateParams(Params); with Params do begin case FTextAlign of taLeftJustify: begin Style :=Style and not ES_CENTER; Style :=Style and not ES_RIGHT; Style := Style or ES_LEFT; end; taRightJustify: begin Style :=Style and not ES_CENTER; Style :=Style and not ES_LEFT; Style := Style or ES_RIGHT; end; taCenter: begin Style :=Style and not ES_LEFT; Style :=Style and not ES_RIGHT; Style := Style or ES_CENTER; end; end;// Style := Style or FTextAlign;//Alignments[UseRightToLeftAlignment, FTextAlign]; end;end;procedure TFloatEdit.SetTextAlign(Value:TAlignment);begin FTextAlign:=Value; RecreateWnd;end;constructor TFloatEdit.Create(AOwner:TComponent);begin inherited Create(AOwner);end;end.这个控件写的不成功,但可以实现您说的功能,您可以在运行期(必须是运行期)设置控件的TextAlign 属性为TAlignment(在DELPHI的帮助里有说明)的一个值就可以了 居中:SetWindowLong(Edit1.handle, GWL_STYLE,GetWindowlong(edit1.Handle, GWL_STYLE) +ES_CENTER);靠右:SetWindowLong(Edit1.handle, GWL_STYLE,GetWindowlong(edit1.Handle, GWL_STYLE) +ES_RIGHT);设置后,刷新一下Edit1.Refresh ; 設置edit1的Align的屬性為clenter procedure TForm1.Button1Click(Sender: TObject);var rect:Trect;dc:hdc;cha:pointer;beginSendMessage(edit1.Handle,EM_LIMITTEXT,30,0);rect:=edit1.ClientRect;dc:=GetDc(edit1.Handle);drawText(dc,'hello',5,rect,dt_center);end; 上面多位兄弟说的不错:只有修改或继承 重写方法CreateParams(var Params: TCreateParams)加上居中属性. 自己写几句代码也行呀,当客户的焦点移开时区一下Text的长度,根据edit的长度计算一下,在自己加入几个空格不就行了呀。:) 关于RAVE的问题!!!! 大伙给看看吧,停着急的 查出的记录修改问题 有誰能给我屏蔽win2000的ctrl+alt+delete 的delphi源码! 哪位高手用过网卡做上位机的双机热备 !!!!!谁帮我写个字符串换行得函数?!!!!!! 拿分了!快点来呀! 有高手运用Delphi处理过图象吗?请问在Delphi中能使用小波分析进行图象处理吗?(内空)谢谢! 悬赏解决技术问题 奇怪!难道没一个中国的程序员知道这个问题的答案?!我可是问了n次呀! 菜鸟刚看到这样一个趣味题,有空的朋友近来看看! 怎么用delphi怎么画2维xy坐标图
可能考虑用第三方控件,如:rx
Windows, Messages, SysUtils, Classes,StdCtrls,Controls,Forms;type
TFloatEdit = class(TCustomEdit)
private
{ Private declarations }
FPrecision:Integer;
FTextAlign:TAlignment;
//procedure SetPrecision(value:Integer);
procedure SetTextAlign(value:TAlignment);
procedure WMChar(var Message: TWMChar); message WM_CHAR;
protected
{ Protected declarations }
function DoKeyPress(var Message: TWMKey): Boolean;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
procedure CreateParams(var Params: TCreateParams); override;
function TextAsInteger:Integer;
function TextAsFloat:Extended;
published
{ Published declarations }
property Precision:Integer read FPrecision write FPrecision default 2;
property TextAlign:TAlignment read FTextAlign write SetTextAlign ;
property Anchors;
property AutoSelect;
property AutoSize;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property MaxLength;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
end;implementation
uses Tools;
{
VK_NUMPAD0 0 key (numeric keypad)
VK_NUMPAD1 1 key (numeric keypad)
VK_NUMPAD2 2 key (numeric keypad)
VK_NUMPAD3 3 key (numeric keypad)
VK_NUMPAD4 4 key (numeric keypad)
VK_NUMPAD5 5 key (numeric keypad)
VK_NUMPAD6 6 key (numeric keypad)
VK_NUMPAD7 7 key (numeric keypad)
VK_NUMPAD8 8 key (numeric keypad)
VK_NUMPAD9 9 key (numeric keypad)
}
function TFloatEdit.TextAsInteger:Integer;
begin
try
Result:=Round(StrToFloat(Text));
Except on EConvertError do
Result:=0;
end;
end;
function TFloatEdit.TextAsFloat:Extended;
begin
try
Result:=StrToFloat(Text);
Except on EConvertError do
Result:=0.0;
end;
end;
function TFloatEdit.DoKeyPress(var Message: TWMKey): Boolean;
begin
Result:=false;
Message.Result:=1;
if (StrScan(PChar(Text),'.')<>nil) and (Char(Message.CharCode) = '.' )or
not(Char(Message.CharCode) in ['0'..'9','.'])and not (Message.CharCode in [VK_BACK]) then
begin
Message.Result:=0;
Result:=true;
end;
end;
procedure TFloatEdit.WMChar(var Message:TWMKey);
begin
if not DoKeyPress(Message) then inherited;
end;procedure TFloatEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));begin
inherited CreateParams(Params);
with Params do
begin
case FTextAlign of
taLeftJustify:
begin
Style :=Style and not ES_CENTER;
Style :=Style and not ES_RIGHT;
Style := Style or ES_LEFT;
end;
taRightJustify:
begin
Style :=Style and not ES_CENTER;
Style :=Style and not ES_LEFT;
Style := Style or ES_RIGHT;
end;
taCenter:
begin
Style :=Style and not ES_LEFT;
Style :=Style and not ES_RIGHT;
Style := Style or ES_CENTER;
end;
end;
// Style := Style or FTextAlign;//Alignments[UseRightToLeftAlignment, FTextAlign];
end;
end;
procedure TFloatEdit.SetTextAlign(Value:TAlignment);
begin
FTextAlign:=Value;
RecreateWnd;
end;
constructor TFloatEdit.Create(AOwner:TComponent);
begin
inherited Create(AOwner);end;
end.
这个控件写的不成功,但可以实现您说的功能,
您可以在运行期(必须是运行期)设置控件的TextAlign 属性为TAlignment(在DELPHI的帮助里有说明)的一个值就可以了
SetWindowLong(Edit1.handle, GWL_STYLE,GetWindowlong(edit1.Handle, GWL_STYLE) +ES_CENTER);
靠右:
SetWindowLong(Edit1.handle, GWL_STYLE,GetWindowlong(edit1.Handle, GWL_STYLE) +ES_RIGHT);
设置后,刷新一下
Edit1.Refresh ;
var rect:Trect;
dc:hdc;
cha:pointer;
begin
SendMessage(edit1.Handle,EM_LIMITTEXT,30,0);
rect:=edit1.ClientRect;
dc:=GetDc(edit1.Handle);
drawText(dc,'hello',5,rect,dt_center);
end;
重写方法CreateParams(var Params: TCreateParams)加上居中属性.