WEB开发网
开发学院软件开发Delphi Delphi自带的SpinEdit控件太丑了,自己写一个替换... 阅读

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.

Tags:Delphi 自带 SpinEdit

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接