D7下的只能输入数字的控件(类似PB的MaskEdit)

unit ComerMaskEdit;

//2005-1-1

//comerliang

//D7下的只能输入数字的控件(类似PB的MaskEdit)
interface

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

type
  TComerMaskEdit = class(TEdit)
  private
    FMdNumber: string;
    FIpo : Integer;
    FLen : Integer;
    procedure SetMdNumber(const Value: string);
    procedure WMPaste(var Message: TMessage); message WM_PASTE;
    { Private declarations }
  protected
    procedure CreateWnd;override;
    { Protected declarations }
  public
    constructor Create(AOwner:TComponent);override;
    procedure DoEnter(); override;
    procedure DoExit(); override;
    procedure KeyPress(var Key: Char); override;
    procedure KeyDown (var Key: Word; Shift: TShiftState);override;
    { Public declarations }
  published
    property MdNumber:string read FMdNumber write SetMdNumber;
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Standard', [TComerMaskEdit]);
end;

{TComerMaskEdit}

constructor TComerMaskEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
    Font.Size :=12;
    Font.Name:='宋体';
    Font.Charset:=GB2312_CHARSET;
    FMdNumber := '';
end;

procedure TComerMaskEdit.CreateWnd;
var
  I, k : Integer;
  S, Str : string;
begin
  inherited;
    if not Enabled then
        Font.Color := clNavy;
    Font.Size :=12;
    Font.Name :='宋体';
    Font.Charset :=GB2312_CHARSET;
    if FMdNumber <> '' then
    begin
        I := Pos(',',FMdNumber);
        if I > 0 then
        begin
            S := Copy(FMdNumber,1,I-1);
            FIpo := StrToInt(Copy(FMdNumber,I+1,Length(FMdNumber)-I));
            for k:=1 to FIpo do
                Str := Str + '0';
            Text := '.' + Str;
        end
        else
        begin
            S := FMdNumber;
            FIpo := 0;
            Text := '';
        end;
        MaxLength := StrToInt(S);
        FLen := MaxLength;
        imeMode := imClose;
    end;
end;

procedure TComerMaskEdit.SetMdNumber(const Value: string);
var
  S : string;
begin
    if Value <> '' then
    begin
        S := StringReplace(Value,',','',[rfIgnoreCase]);
        try
            StrToInt(S);
        except
            Application.MessageBox('属性值设置不对!','错误',MB_OK+MB_ICONError);
            FMdNumber := '';
            Exit;
        end;
    end;
    FMdNumber := Value;
end;

procedure TComerMaskEdit.DoEnter();
begin
    inherited;
    SelStart := 0;
end;

procedure TComerMaskEdit.DoExit();
begin
    if (FIpo>0) and (Pos('.',Text)=0) then
        Text := Text + '.' + StringOfChar('0',FIpo)
    else
        inherited;
end;

procedure TComerMaskEdit.KeyPress(var Key: Char);
var
    I, k : Integer;
    AfterDot, BeforSelStart : string;
    //, AfterSelStart,
    Str : string;
    TmpText : string;
    iSelStart: Integer;
    //BeforComma, AfterComma : String;
begin
    //如果有多个字符被选中
    if SelLength > 0 then
        SelStart := 0
    else
    begin
        if FMdNumber <> '' then
        begin
            if (Length(Text)=0) and (FIpo>0) then
            begin
                for k:=1 to FIpo do
                    Str := Str + '0';
                Text := '.' + Str;
            end;
            case Key of
                #13:
                    inherited;
                '-':
                begin
                    if (SelStart<>0) or (Pos('-',Text)>0) then
                        Key := #0
                    else
                        //MaxLength := MaxLength + 1;
                        inherited;
                end;
                #8:
                begin
                    I := Pos('.',Text);
                    if (I > 0) and (SelStart>I) then
                    begin
                        key := #0;
                        iSelStart := SelStart;
                        TmpText := Text;
                        BeforSelStart := Copy(TmpText,1,iSelStart-1);
                        Text := BeforSelStart + Copy(TmpText,iSelStart+1,Length(TmpText)-iSelStart) + '0';
                        SelStart := iSelStart - 1;
                    end
                    else if (I > 0) and (SelStart=I) then
                    begin
                        key := #0;
                        iSelStart := SelStart;
                        SelStart := iSelStart - 1;
                    end
                    else
                        inherited;
                end;
                '0'..'9':
                begin
                    I := Pos('.',Text);
                    //限制位数
                    if I > 0 then
                    begin
                        if SelStart = Length(Text) then
                            key := #0
                        else
                        begin
                            AfterDot := Copy(Text,I+1,Length(Text)-FIpo);
                            if Length(AfterDot) > FIpo then
                                key := #0
                            else if SelStart >= I then
                            begin
                                iSelStart := SelStart;
                                TmpText := Text;
                                BeforSelStart := Copy(TmpText,1,iSelStart);
                                Text := BeforSelStart + Copy(TmpText,iSelStart+2,Length(TmpText)-iSelStart-1);
                                SelStart := iSelStart;
                            end
                            else
                                inherited;
                        end;
                    end
                    else if (I<=0) and (FIpo>0) then  //这种情况基本不存在
                    begin
                        if Length(Text) >= FLen-FIpo-1 then
                            key := #0
                        else
                            inherited;
                    end
                    else  //FIpo=0
                        inherited;
                end;
                '.':
                begin
                    Key := #0;
                    if FIpo>0 then
                        SelStart := Pos('.',Text);
                end;
                else
                    Key := #0;
            end;
        end
        else
            //Key := #0;
            inherited;
    end;
end;

procedure TComerMaskEdit.KeyDown(var Key: Word; Shift: TShiftState);
var
    I : Integer;
    BeforSelStart : string;
    TmpText : string;
    iSelStart: Integer;
begin
    //如果有多个字符被选中
    if SelLength > 0 then
    begin
        Key := 0;
        SelStart := 0;
    end
    else
    begin
        if FMdNumber <> '' then
        begin
            if Key=VK_DELETE then
            begin
                I := Pos('.',Text);
                if (I > 0) and (SelStart>=I) then
                begin
                    key := 0;
                    iSelStart := SelStart;
                    TmpText := Text;
                    BeforSelStart := Copy(TmpText,1,iSelStart);
                    if (SelStart=Length(Text)) then
                        Text := BeforSelStart + Copy(TmpText,iSelStart+2,Length(TmpText)-iSelStart-1)
                    else
                        Text := BeforSelStart + Copy(TmpText,iSelStart+2,Length(TmpText)-iSelStart-1) + '0';
                    SelStart := iSelStart;
                end
                else if (I > 0) and (SelStart=I-1) then
                begin
                    key := 0;
                    iSelStart := SelStart;
                    SelStart := iSelStart + 1;
                end;
            end
            else if (Key=VK_TAB) or (Key=VK_LEFT) or (Key=VK_UP) or (Key=VK_RIGHT) or (Key=VK_DOWN) or (Key=VK_END) or (Key=VK_HOME) then
                inherited
            else
                Key := 0;

        end
        else
            inherited;
    end;
end;

procedure TComerMaskEdit.WMPaste(var Message: TMessage);
begin
    if FMdNumber='' then
        inherited;
end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Comer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值