Delphi自带的SpinEdit控件太丑了,自己写一个替换它
2006-02-04 13:53:52 来源:WEB开发网核心提示:unit UpDownEdit;interface uses Windows, SysUtils, Classes, Controls, StdCtrls, ComCtrls, Messages; type TUpDownEdit = class(TCustomControl) PRivate { Privat
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.
- ››Delphi实现把10进制转换成16进制的函数进制转化
- ››Delphi中将字符串按给定字符分隔(似split函数功能...
- ››Delphi 动态创建窗体,锁定窗口赋值
- ››Delphi 与 VC 共享接口和对象
- ››Delphi图像处理 -- 表面模糊
- ››Delphi之多线程实例
- ››Delphi SelectSingleNode的使用 根据节点属性获取...
- ››Delphi接口详述
- ››delphi 远程调试
- ››Delphi与DirectX之DelphiX(34): TDIB.Lightness()...
- ››Delphi Application.MessageBox详解
- ››Delphi只能运行一个程序实例的两种方法
更多精彩
赞助商链接