Delphi自带的SpinEdit控件太丑了自己写一个替换它

MM小流氓0

MM小流氓0

2016-02-19 14:18

今天图老师小编要向大家分享个Delphi自带的SpinEdit控件太丑了自己写一个替换它教程,过程简单易学,相信聪明的你一定能轻松get!
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; overridepublished    { 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;   endprocedure Registerimplementation 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;enddestructor TUpDownEdit.Destroy;begin  Edit.Free;  UpDown.Free;  inherited;endprocedure TUpDownEdit.EditChange(Sender: TObject);begin  UpDown.Position := StrToIntDef(Edit.Text, 0);  FPosition := UpDown.Position;  if Assigned(FOnChange) then    FOnChange(Self);endprocedure 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;    endend  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;   endprocedure 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;endprocedure 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;endprocedure 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;endprocedure 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;endprocedure TUpDownEdit.WMSize(var Msg: TWMSize);begin   Edit.Width := Width - 15;  UpDown.Left := Edit.Width + 1;  UpDown.Height := Height;  inheritedendend.
展开更多 50%)
分享

猜你喜欢

Delphi自带的SpinEdit控件太丑了自己写一个替换它

编程语言 网络编程
Delphi自带的SpinEdit控件太丑了自己写一个替换它

自己写的一个图表控件

电脑网络
自己写的一个图表控件

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

共享自己写一个框架DreamScript

Web开发
共享自己写一个框架DreamScript

偶写的第一个控件,一个用选择代替输入的Edit控件

编程语言 网络编程
偶写的第一个控件,一个用选择代替输入的Edit控件

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

自己写的一个简单ASP调用存储过程查询

ASP
自己写的一个简单ASP调用存储过程查询

利用Ajax技术写一个迷你留言板WEB控件

Web开发
利用Ajax技术写一个迷你留言板WEB控件

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

在TwebBrowser中调用隐藏的IE命令

在TwebBrowser中调用隐藏的IE命令

span div p 之间的不同区别

span div p 之间的不同区别
下拉加载更多内容 ↓