欢迎光临
我们一直在努力

delphi自带的spinedit控件太丑了,自己写一个替换它_delphi教程

建站超值云服务器,限时71元/月

unit UpDownEdit;


interface

 

uses

  Windows, SysUtils, Classes, Controls, StdCtrls, ComCtrls, Messages;

 

type

  TUpDownEdit = class(TCustomControl)

  private

    { Private declarations }

    UpDown: TUpDown;

    Edit: TEdit;

    FMin: Integer;

    FMax: Integer;

    FOnChange: TNotifyEvent;

    FPosition: Integer;

    procedure WMSize(var Msg: TWMSize); message wm_Size;

    procedure SetMax(const Value: Integer);

    procedure SetMin(const Value: Integer);

    procedure EditChange(Sender: TObject);

    procedure EditKeyPress(Sender: TObject; var Key: Char);

    procedure UpDownClick(Sender: TObject; Button: TUDBtnType);

    procedure SetPosition(const Value: Integer);

  protected

    { Protected declarations }

  public

    { Public declarations }

    constructor Create(AOwner: TComponent); override;

    destructor Destroy; override;

  published

    { Published declarations }

    property Max: Integer read FMax write SetMax;

    property Min: Integer read FMin write SetMin;

    property Position: Integer read FPosition write SetPosition;

    property OnChange: TNotifyEvent read FOnChange write FOnChange;

 

  end;

 

procedure Register;

 

implementation

 

procedure Register;

begin

  RegisterComponents(Standard, [TUpDownEdit]);

end;

 

{ TUpDownEdit }

 

constructor TUpDownEdit.Create(AOwner: TComponent);

begin

  inherited Create(AOwner);

  SetBounds(0, 0, 57, 21);

  Edit := TEdit.Create(Self);

  Edit.Left := 0;

  Edit.Top := 0;

  Edit.Width := 40;

  Edit.Align := alLeft;

  Edit.Parent := self;

  Edit.Text := 0;

//  SetWindowLong(Edit.Handle, GWL_STYLE, GetWindowLong(Edit.Handle, GWL_STYLE) or ES_NUMBER);

 

  UpDown := TUpDown.Create(self);

  UpDown.Height := Height; //20;

  UpDown.Width := 14;

  UpDown.Left := Edit.Width + 1;

  UpDown.Parent := self;

  FMin := 0;

  FMax := 100;

 

  Edit.OnChange := EditChange;

  Edit.OnKeyPress := EditKeyPress;

  UpDown.OnClick := UpDownClick;

end;

 

destructor TUpDownEdit.Destroy;

begin

  Edit.Free;

  UpDown.Free;

  inherited;

end;

 

procedure TUpDownEdit.EditChange(Sender: TObject);

begin

  UpDown.Position := StrToIntDef(Edit.Text, 0);

  FPosition := UpDown.Position;

  if Assigned(FOnChange) then

    FOnChange(Self);

end;

 

procedure TUpDownEdit.EditKeyPress(Sender: TObject; var Key: Char);

var

  s: set of char;

  i: integer;

  Str, Text: string;

begin

  s := [#8, 0..9];

  if Key = #8 then exit;

 

  if not (Key in s) then

  begin

    Key := #0;

    Exit;

  end;

 

//控制输入数字的大小

  if TEdit(Sender).SelLength > 0 then

  begin

    Text := TEdit(Sender).Text;

    Str := Copy(Text, 1, TEdit(Sender).SelStart – 1)

      + Key +

      Copy(Text, TEdit(Sender).SelStart + TEdit(Sender).SelLength + 1, Length(Text));

    i := StrToInt(Str);

    if i > FMax then

    begin

      Key := #0;

      Exit;

    end;

  end

  else

    if StrToInt(TEdit(Sender).Text + Key) > FMax then

    begin

      Key := #0;

      Exit;

    end

    else

      if StrToInt(TEdit(Sender).Text + Key) < FMin then

      begin

        Key := #0;

        Exit;

      end;

 

 

 

end;

 

procedure TUpDownEdit.SetMax(const Value: Integer);

begin

  FMax := Value;

  UpDown.Max := FMax;

  if StrToIntDef(Edit.Text, 0) > FMax then

  begin

    UpDown.Position := FMax;

    Edit.Text := IntToStr(FMax);

    FPosition := UpDown.Position;

  end;

end;

 

procedure TUpDownEdit.SetMin(const Value: Integer);

begin

  FMin := Value;

  UpDown.Min := FMin;

  if StrToIntDef(Edit.Text, 0) < FMin then

  begin

    UpDown.Position := FMin;

    Edit.Text := IntToStr(FMin);

    FPosition := UpDown.Position;

    if Assigned(FOnChange) then

      FOnChange(Self);

  end;

end;

 

procedure TUpDownEdit.SetPosition(const Value: Integer);

begin

  if (Value >= FMin) or (Value <= FMax) then

  begin

    FPosition := Value;

    UpDown.Position := FPosition;

    Edit.Text := IntToStr(FPosition);

    if Assigned(FOnChange) then

      FOnChange(Self);

  end;

end;

 

procedure TUpDownEdit.UpDownClick(Sender: TObject; Button: TUDBtnType);

begin

  if Max = 0 then

  begin

    Max := 100;

    UpDown.Max := Max;

  end;

  UpDown.Min := Min;

  Edit.Text := IntToStr(UpDown.Position);

  Edit.SetFocus;

  Edit.SelectAll;

  if Assigned(FOnChange) then

    FOnChange(Self);

  FPosition := UpDown.Position;

end;

 

procedure TUpDownEdit.WMSize(var Msg: TWMSize);

begin

 

  Edit.Width := Width – 15;

  UpDown.Left := Edit.Width + 1;

  UpDown.Height := Height;

  inherited;

 

end;

 

end.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » delphi自带的spinedit控件太丑了,自己写一个替换它_delphi教程
分享到: 更多 (0)