国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Delphi > 正文

Delphi自帶的SpinEdit控件太丑了,自己寫一個替換它

2019-11-18 18:12:58
字體:
來源:轉載
供稿:網友
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中MIDAS是什么(MIDAS之一)

下一篇:Delphi中使用動態SQL的幾個問題

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 无为县| 无棣县| 罗城| 舒城县| 皮山县| 新疆| 沙田区| 柘城县| 黄陵县| 泰和县| 元阳县| 东台市| 南京市| 芦溪县| 英山县| 青田县| 上林县| 厦门市| 东平县| 乌拉特前旗| 塘沽区| 安福县| 叶城县| 潜江市| 北海市| 汉沽区| 威远县| 色达县| 兴安盟| 白山市| 辽源市| 凤城市| 永年县| 富宁县| 竹溪县| 博野县| 天镇县| 肥东县| 漳州市| 富裕县| 大洼县|