unit UpDownEdit;interfaceusesWindows, SysUtils, Classes, Controls, StdCtrls, ComCtrls, Messages;typeTUpDownEdit = 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;implementationprocedure Register;beginRegisterComponents('Standard', [TUpDownEdit]);end;{ TUpDownEdit }constructor TUpDownEdit.Create(AOwner: TComponent);begininherited 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;beginEdit.Free; UpDown.Free; inherited;end;procedure TUpDownEdit.EditChange(Sender: TObject);beginUpDown.Position := StrToIntDef(Edit.Text, 0); FPosition := UpDown.Position; if Assigned(FOnChange) thenFOnChange(Self);end;procedure TUpDownEdit.EditKeyPress(Sender: TObject; var Key: Char);vars: set of char; i: integer; Str, Text: string;begins := [#8, '0'..'9']; if Key = #8 then exit; if not (Key in s) thenbeginKey := #0; Exit; end;//控制输入数字的大小if TEdit(Sender).SelLength > 0 thenbeginText := 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 thenbeginKey := #0; Exit; end; endelseif StrToInt(TEdit(Sender).Text + Key) > FMax thenbeginKey := #0; Exit; endelseif StrToInt(TEdit(Sender).Text + Key) < FMin thenbeginKey := #0; Exit; end;end;procedure TUpDownEdit.SetMax(const Value: Integer);beginFMax := Value; UpDown.Max := FMax; if StrToIntDef(Edit.Text, 0) > FMax thenbeginUpDown.Position := FMax; Edit.Text := IntToStr(FMax); FPosition := UpDown.Position; end;end;procedure TUpDownEdit.SetMin(const Value: Integer);beginFMin := Value; UpDown.Min := FMin; if StrToIntDef(Edit.Text, 0) < FMin thenbeginUpDown.Position := FMin; Edit.Text := IntToStr(FMin); FPosition := UpDown.Position; if Assigned(FOnChange) thenFOnChange(Self); end;end;procedure TUpDownEdit.SetPosition(const Value: Integer);beginif (Value >= FMin) or (Value <= FMax) thenbeginFPosition := Value; UpDown.Position := FPosition; Edit.Text := IntToStr(FPosition); if Assigned(FOnChange) thenFOnChange(Self); end;end;procedure TUpDownEdit.UpDownClick(Sender: TObject; Button: TUDBtnType);beginif Max = 0 thenbeginMax := 100; UpDown.Max := Max; end; UpDown.Min := Min; Edit.Text := IntToStr(UpDown.Position); Edit.SetFocus; Edit.SelectAll; if Assigned(FOnChange) thenFOnChange(Self); FPosition := UpDown.Position;end;procedure TUpDownEdit.WMSize(var Msg: TWMSize);beginEdit.Width := Width - 15; UpDown.Left := Edit.Width + 1; UpDown.Height := Height; inherited;end;end.