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

2016-02-19 14:18 36 1 收藏

今天图老师小编要向大家分享个Delphi自带的SpinEdit控件太丑了自己写一个替换它教程,过程简单易学,相信聪明的你一定能轻松get!

【 tulaoshi.com - 编程语言 】

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.

来源:https://www.tulaoshi.com/n/20160219/1606692.html

延伸阅读
一个优秀的网格控件CGridCtrl 作者:戴绍忠   网格控件的用途非常广泛,在我的一个项目中需要实现类似EXCEL的界面,为此我采用了一个优秀的CGridCtrl控件,其原作者为 Chris Maunder (原作者所写的MFC Grid control的最新版本可以到http://www.codetools.com/miscctrl/gridctrl.asp查阅)为了方便地实现单元格...
标签: PHP
在开始这篇文章时,作者假设读者已能够写出一个用户的登入认证程序. ---------------------------------------------------------- 记数器可完成访问 web 页的总次数,但却不能得知一个时段中访问量的动态记载,下面就来介绍如何写一个各个时段动态显示访问量的方法. 要记载访问量,首先就要在 mysql 内建立一个数据库,姑且给这...
标签: ASP
<!--#include file="../lib/filelib.asp"-- <% Response.write "<title上传文件至当前文件夹</title" Response.Write "<body bgcolor=""#D6D3CE"" leftmargin=""0"" topmargin=""0"" title = "" 请您遵守国家相关法律法规上传文件。上传前请杀毒,否则系统将会自动删除此文件!""" '**Start Encode** Action=Request("A") If ...
感情走到尽头,尽管会有不舍,但也是验证了一句话,逝去的就很难再挽回。的确,两个人最初因为相爱在一起,但最终还是会因为各种原因选择分道扬镳。这其中的各种心酸或许只有当事人才会更好知晓。下面,图老师小编从女人心理角度为大家盘点一个离婚女人的离婚感悟,一起看看什么样的男人值得嫁! 离婚感悟 1、按他的要求去做...
  可视化控件(Visual Component)实际上就是一个类(class),要编写一个类,可以直接在*.pas文件中编写。但是要编写控件,则必须使用包(package)。从File菜单中选择New,新建一个Package,这就是存放和安装控件用的包。然后单击Package窗口中的Add按钮,添加一个元件(Unit)。 在弹出的对话框最上方选择New Component。因为一...

经验教程

319

收藏

21
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部