可以左右居中对齐并可设置DisplayFormat的Edit控件

页面导航:首页 > 软件编程 > Delphi > 可以左右居中对齐并可设置DisplayFormat的Edit控件

可以左右居中对齐并可设置DisplayFormat的Edit控件

来源: 作者: 时间:2016-01-23 10:15 【

欢迎测试![email protected] OWEdit;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TInputDataType = (tFloat,tInteger,tAl

 

 

欢迎测试!

unit OWEdit;

interface

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

type
  TInputDataType = (tFloat,tInteger,tAll);

type
  TOWEdit = class(TEdit)
  private
    { Private declarations }
    FCanvas : TCanvas;
    FDataType: TInputDataType;
    FAlignment : TAlignment;
    FDisplayFormat : String;
    FDeciNum : Word;
    FDisplayText : String;
    procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  protected
    { Protected declarations }
    procedure SetDataType(Value:TInputDataType);
    procedure SetAlignment(Value:TAlignment);
    procedure SetDisplayFormat(Value:String);
    procedure ClipPaste(var M:TMessage); Message WM_PASTE;
    procedure PaintWindow(DC: HDC); override;
    procedure Paint; virtual;
    procedure WMExit(var Message:TWMKillFocus);Message WM_KILLFOCUS;
    procedure GetDisplayText;
    procedure ShowDisplayText;
    function  GetDeciLast:integer;
  public
    { Public declarations }
    OldText : String;
    property Text;
    property Canvas: TCanvas read FCanvas;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy(); override;
    procedure KeyPress(var Key: Char); override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  published
    { Published declarations }
    property DataType: TInputDataType read fDataType write SetDataType default tFloat;
    property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
    property DisplayFormat: string read FDisplayFormat write SetDisplayFormat;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents(Ourway, [TOWEdit]);
end;

constructor TOWEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Text := 0;
  FCanvas := TControlCanvas.Create;
  TControlCanvas(FCanvas).Control := Self;
  FDeciNum := 9999;
end;

destructor TOWEdit.Destroy();
begin
  FCanvas.Free;
  inherited Destroy();
end;

procedure TOWEdit.SetDataType(Value:TInputDataType);
begin
  If Value<>fDataType Then
  begin
    fDataType := Value;
    Case Value of
      tAll: Text := ;
      tFloat: Text:=0.0;
      tInteger: Text:=0;
    end;
    ShowDisplayText;
    Invalidate;
  end;
end;

procedure TOWEdit.SetAlignment(Value:TAlignment);
begin
  If Value<>FAlignment Then
  begin
    FAlignment := Value;
    Invalidate;
  end;
end;

procedure TOWEdit.SetDisplayFormat(Value: string);
begin
  If Value<>FDisplayFormat Then
  begin
    FDisplayFormat := Value;
    if Trim(Value)<> then
      FDeciNum := Length(Value)-Pos(.,Value)+1
    else
      FDeciNum := 9999;
    ShowDisplayText;
    Invalidate;
  end;
end;

procedure TOWEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
  if Key = VK_DELETE then
    if Self.SelStart=pos(.,Self.Text)-1 then
      Key := 0;
  inherited KeyDown(Key,Shift);
end;
procedure TOWEdit.KeyPress(var Key: Char);
var
  kv: Integer;
begin
  kv := Ord(Key);
  case fDataType of
    tInteger:
      if (((kv>58) or (kv<48)) and (kv<>3) and (kv<>22) and (kv<>8) and (kv<>13)) then
         Key := chr(0);
    tFloat:
      begin
        if (((kv>58) or (kv<48)) and (kv<>3) and (kv<>22) and (kv<>46) and (kv<>8) and (kv<>13)) then
           Key := chr(0)
        else
        begin
          if (kv=46) and (Pos(.,self.Text)>0) then//已有小数点
            Key := chr(0)
          else
            if MaxLength<1 then//小数点前面位数不定
            begin
              if ((GetDeciLast>=FDeciNum) and (kv<>8)) then //退格键
                if ((self.SelLength=0)and(pos(.,copy(Self.Text,1,self.SelStart))>0))then
                  Key := chr(0);
            end
            else//输入总长度已定
            begin
              if pos(.,copy(self.Text,1,self.selStart))<1 then
              begin//光标在小数点之前
                if ((self.SelStart>=MaxLength-FDeciNum)and(kv<>8)and(kv<>46)) then
                    Key := chr(0);
              end
              else
              begin//光标在小数点之后
                if ((GetDeciLast>=FDeciNum) and (kv<>8) and (self.SelLength=0)and(pos(.,copy(Self.Text,1,self.SelStart))>0)) then
                    Key := chr(0);
              end;
            end;
        end;
      end;
    else
  end;
  if (kv=8)and(Self.SelStart>0)and(Self.Text[self.SelStart]=.)and(GetDeciLast>1) then
    Key := chr(0);
  //还有一个Delete键没有截获!如果用此键删除小数点,还是有可能出错的。
  //搞定!用KeyDown

Tags:

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <