怎样是TEdit里的内容居右显示?

解决方案 »

  1.   

    似乎没有太好的办法,你可以定义个长度,然后当有键按下的时候,把字符拷贝到最右端,然后清空key
      

  2.   

    让 TEdit 只接受数字&右对齐,并且回车跳转//直接的办法
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if NOT (key in ['0'..'9']) then Key=#0;
    end;
    //彻底的办法 自定义控件
    unit XEdit;
    interface
    uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls;
    type
      TXCustomEdit = class(TCustomEdit)
      private
        FAlignment: TAlignment;
        FAutoTab: Boolean;
        FNumber: Boolean;
        procedure SetAlignment(const Value: TAlignment);
        procedure CNKeyDown(var Message: TWMKeyDown); message CN_KEYDOWN;
        procedure SetNumber(const Value: Boolean);
        protected
        procedure CreateParams(var Params: TCreateParams); override;
      public
        constructor Create(AOwner: TComponent); override;
      published
        property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
        Property AutoTab: Boolean Read FAutoTab Write FAutoTab default True;    Property Number: Boolean Read FNumber Write SetNumber Default False;
      end; 
     {TEditEx}
      TXuEdit = class(TXCustomEdit)
      published
        property Anchors;
        property AutoSelect;
        property AutoSize;
        property BevelEdges;
        property BevelInner; 
        property BevelKind default bkNone;
        property BevelOuter;
        property BiDiMode;
        property BorderStyle; 
        property CharCase; 
        property Color;
        property Constraints;
        property Ctl3D;
        property DragCursor;
        property DragKind; 
        property DragMode;
        property Enabled;
        property Font;
        property HideSelection;
        property ImeMode;
        property ImeName; 
        property MaxLength;
        property OEMConvert;
        property ParentBiDiMode;
        property ParentColor;
        property ParentCtl3D;
        property ParentFont;
        property ParentShowHint;
        property PasswordChar;
        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 OnEndDock;
         property OnEndDrag;
         property OnEnter; 
        property OnExit; 
        property OnKeyDown; 
        property OnKeyPress;
         property OnKeyUp; 
        property OnMouseDown;
         property OnMouseMove;
         property OnMouseUp;
         property OnStartDock;
         property OnStartDrag;
        end;
    implementation
    {实现}
    procedure TXCustomEdit.CNKeyDown(var Message: TWMKeyDown);
    begin
      if (Message.CharCode=VK_RETURN) AND FAutoTab then
       Message.CharCode:=VK_TAB;
       inherited;
    end;
    constructor TXCustomEdit.Create(AOwner: TComponent);
    begin
      FAutoTab:=True;
      FNumber:=False;
      FAlignment:=taLeftJustify; 
      inherited Create(AOwner);
     end;
    procedure TXCustomEdit.CreateParams(var Params: TCreateParams);
    const
       Alignments: array[Boolean, TAlignment] of DWORD =    ((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER)); 
     NumberSet: Array[Boolean] of DWORD =(0, ES_NUMBER);
    begin
      inherited CreateParams(Params);
      with Params do
      begin
        Style := Style or Alignments[UseRightToLeftAlignment, FAlignment] or             NumberSet[FNumber];
      end;
    end;
    procedure TXCustomEdit.SetAlignment(const Value: TAlignment);
    begin
      if FAlignment <> Value then
      begin
        FAlignment := Value;
        RecreateWnd;
      end;
    end;
    procedure TXCustomEdit.SetNumber(const Value: Boolean);
    begin
      if FNumber &lt;&gt; Value then
      begin
        FNumber := Value; 
        if FNumber then FAlignment:= taRightJustify; 
       RecreateWnd;
      end;
    end;
    {Register 就自己写吧}
    end.
      

  3.   

    procedure TXCustomEdit.CreateParams(var Params: TCreateParams);
    const
       Alignments: array[Boolean, TAlignment] of DWORD =    ((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER)); 
     NumberSet: Array[Boolean] of DWORD =(0, ES_NUMBER);
    begin
      inherited CreateParams(Params);
      with Params do
      begin
        Style := Style or Alignments[UseRightToLeftAlignment, FAlignment] or             NumberSet[FNumber];
      end;
    end;
    楼上的已经给出了核心的代码,OVERRIDE并少改一下就OK了,
      

  4.   

    还真有点麻烦..
    我一向都用FORMAT输出的..简单又快..何必...当然..我很佩服这个写控件的兄弟...
      

  5.   

    翻MSDN的结果:但是caret工作不正常的要死,逆转后光标希奇古怪。
    在2003server下面似乎绝大多数标准control都能左右互换,工作正常,尤其是光标。var
      windowStyle: cardinal;
    begin
      windowStyle :=GetWindowLong(edit1.Handle,GWL_EXSTYLE);
      if (windowStyle and WS_EX_LAYOUTRTL) = WS_EX_LAYOUTRTL then
        windowStyle := windowStyle - WS_EX_LAYOUTRTL
      else  if (windowStyle and WS_EX_LAYOUTRTL) = 0 then
        windowStyle := windowStyle + WS_EX_LAYOUTRTL
      else ;  SetWindowLong(edit1.Handle, GWL_EXSTYLE,windowStyle);
      //SendMessage(edit1.Handle, EM_SETSEL, 0, MAKELONG($ffff, $ffff));
      InvalidateRect(edit1.Handle, nil, TRUE);
    end;p.s.如果windowStyle := windowStyle + WS_EX_LAYOUTRTL符号搞错,可以mirror。
      

  6.   

    还见过一个做法,ms的,用的c加SDK,把老的edit灭了再createwindow一个新的右到左的,