⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scurredit.pas

📁 AlphaControls是一个Delphi标准控件的集合
💻 PAS
📖 第 1 页 / 共 2 页
字号:
begin
  if DisplayFormat <> Value then begin
    AssignStr(FDisplayFormat, Value);
    sStyle.Invalidate;
    DataChanged;
  end;
end;

function TsCustomNumEdit.GetDisplayFormat: string;
begin
  Result := FDisplayFormat^;
end;

procedure TsCustomNumEdit.SetFocused(Value: Boolean);
begin
  if FFocused <> Value then begin
    FFocused := Value;
    sStyle.Invalidate;
    FFormatting := True;
    try
      DataChanged;
    finally
      FFormatting := False;
    end;
  end;
end;

{
procedure TsCustomNumEdit.SetFormatOnEditing(Value: Boolean);
begin
  if FFormatOnEditing <> Value then begin
    FFormatOnEditing := Value;
    if FFormatOnEditing then inherited Alignment := Alignment
    else inherited Alignment := taLeftJustify;
    if FFormatOnEditing and FFocused then ReformatEditText
    else if FFocused then begin
      UpdateData;
      DataChanged;
    end;
  end;
end;
}
procedure TsCustomNumEdit.SetDecimalPlaces(Value: Cardinal);
begin
  if FDecimalPlaces <> Value then begin
    FDecimalPlaces := Value;
    DataChanged;
    sStyle.Invalidate;
  end;
end;

function TsCustomNumEdit.FormatDisplayText(Value: Extended): string;
begin
  if DisplayFormat <> '' then begin
    Result := FormatFloat(DisplayFormat, Value)
  end
  else begin
    Result := FloatToStr(Value);
  end;
end;

function TsCustomNumEdit.GetDisplayText: string;
begin
  Result := FormatDisplayText(FValue);
end;

procedure TsCustomNumEdit.Clear;
begin
  Text := '';
end;

procedure TsCustomNumEdit.DataChanged;
var
  EditFormat: string;
begin
  EditFormat := '0';
  if FDecimalPlaces > 0 then EditFormat := EditFormat + '.' + MakeStr('#', FDecimalPlaces);
  EditText := FormatFloat(EditFormat, FValue);
end;

function TsCustomNumEdit.CheckValue(NewValue: Extended; RaiseOnError: Boolean): Extended;
begin
  Result := NewValue;
  if (FMaxValue <> FMinValue) then begin
    if (FMaxValue > FMinValue) then begin
      if NewValue < FMinValue
        then Result := FMinValue
        else if NewValue > FMaxValue then Result := FMaxValue;
    end
    else begin
      if FMaxValue = 0 then begin
        if NewValue < FMinValue then Result := FMinValue;
      end
      else if FMinValue = 0 then begin
        if NewValue > FMaxValue then Result := FMaxValue;
      end;
    end;
  end;
end;

procedure TsCustomNumEdit.CheckRange;
begin
  if not (csDesigning in ComponentState) and CheckOnExit then
    CheckValue(StrToFloat(TextToValText(EditText)), True);
end;

procedure TsCustomNumEdit.UpdateData;
begin
  ValidateEdit;
  EditText := DelChars(EditText, #13);
  EditText := DelChars(EditText, #10);
  FValue := CheckValue(StrToFloat(TextToValText(EditText)), False);
end;

function TsCustomNumEdit.GetValue: Extended;
begin
  if not (csDesigning in ComponentState) then
    try
      UpdateData;
    except
      FValue := FMinValue;
    end;
  Result := FValue;
end;

procedure TsCustomNumEdit.SetValue(AValue: Extended);
begin
  FValue := CheckValue(AValue, False);
  DataChanged;
  sStyle.Invalidate;
end;

function TsCustomNumEdit.GetAsInteger: Longint;
begin
  Result := Trunc(Value);
end;

procedure TsCustomNumEdit.SetMinValue(AValue: Extended);
begin
  if FMinValue <> AValue then begin
    FMinValue := AValue;
    Value := FValue;
  end;
end;

procedure TsCustomNumEdit.SetMaxValue(AValue: Extended);
begin
  if FMaxValue <> AValue then begin
    FMaxValue := AValue;
    Value := FValue;
  end;
end;

function TsCustomNumEdit.GetText: string;
begin
  Result := inherited Text;
  Result := DelChars(Result, #13);
  Result := DelChars(Result, #10);
end;

function TsCustomNumEdit.TextToValText(const AValue: string): string;
begin
  Result := DelRSpace(AValue);
  if DecimalSeparator <> ThousandSeparator then begin
    Result := DelChars(Result, ThousandSeparator);
  end;
  if (DecimalSeparator <> '.') and (ThousandSeparator <> '.') then
    Result := ReplaceStr(Result, '.', DecimalSeparator);
  if (DecimalSeparator <> ',') and (ThousandSeparator <> ',') then
    Result := ReplaceStr(Result, ',', DecimalSeparator);
  if Result = ''
    then Result := '0'
    else if Result = '-' then Result := '-0';
end;

procedure TsCustomNumEdit.SetText(const AValue: string);
begin
  if not (csReading in ComponentState) then begin
    FValue := CheckValue(StrToFloat(TextToValText(AValue)), False);
    DataChanged;
    sStyle.Invalidate;
  end;
end;

procedure TsCustomNumEdit.ReformatEditText;
var
  S: string;
  IsEmpty: Boolean;
  OldLen, SelStart, SelStop: Integer;
begin
  FFormatting := True;
  try
    S := inherited Text;
    OldLen := Length(S);
    IsEmpty := (OldLen = 0) or (S = '-');
    if HandleAllocated then GetSel(SelStart, SelStop);
    if not IsEmpty then S := TextToValText(S);
    S := FormatFloatStr(S, Pos(',', DisplayFormat) > 0);
    inherited Text := S;
    if HandleAllocated and (GetFocus = Handle) and not
        (csDesigning in ComponentState) then begin
      Inc(SelStart, Length(S) - OldLen);
      SetCursor(SelStart);
    end;
  finally
    FFormatting := False;
  end;
end;

procedure TsCustomNumEdit.Change;
begin
  if not FFormatting then begin
    if FFormatOnEditing and FFocused then ReformatEditText;
    inherited Change;
  end;
end;

procedure TsCustomNumEdit.WMPaste(var Message: TMessage);
var
  S: string;
begin
  S := EditText;
  try
    inherited;
    UpdateData;
  except
    EditText := S;
    SelectAll;
    if CanFocus then SetFocus;
    if BeepOnError then MessageBeep(0);
  end;
end;

procedure TsCustomNumEdit.CMEnter(var Message: TCMEnter);
begin
  SetFocused(True);
  if FFormatOnEditing then ReformatEditText;
  inherited;
end;

procedure TsCustomNumEdit.CMExit(var Message: TCMExit);
begin
  try
    CheckRange;
    UpdateData;
  except
    SelectAll;
    if CanFocus then SetFocus;
    raise;
  end;
  SetFocused(False);
  SetCursor(0);
  DoExit;
end;

procedure TsCustomNumEdit.WMPaint(var Message: TWMPaint);
begin
  if (csDestroying in ComponentState) or (csLoading in ComponentState) then Exit;
  if not PaintEdit(Self, GetDisplayText, FAlignment, FFocused or DroppedDown, FCanvas, Message) then inherited;
end;

procedure TsCustomNumEdit.PopupWindowShow;
var
  sC : TsControlsManager;
begin
  TsCalcForm(FPopupWindow).FEditor := Self;
  TsCalcForm(FPopupWindow).FText := Text;
  if not TsCalcForm(FPopupWindow).sControlsManager1.Active then begin
    sC := GetsControlsManager(GetParentForm(Self), sStyle.GroupIndex);
    if sC <> nil then begin
      TsCalcForm(FPopupWindow).sControlsManager1.Assign(sC); //???
    end;
  end;
  inherited;
end;

procedure TsCustomNumEdit.WndProc(var Message: TMessage);
begin
  if (Message.Msg = SM_CLEARINDEXES) and Assigned(FPopupWindow) then begin
    BroadCastS(FPopupWindow, Message);
  end;
  if (Message.Msg = SM_SETNEWSKIN) and Assigned(FPopupWindow) then begin
    BroadCastS(FPopupWindow, Message);
  end;
  if (Message.Msg = SM_REFRESH) and Assigned(FPopupWindow) then begin
    BroadCastS(FPopupWindow, Message);
  end;
  inherited;
end;

{ TsCalcEdit }

constructor TsCalcEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ControlState := ControlState + [csCreating];
  FDefBmpName := 'BTN_CALC';

  try
    FPopupWindow := TsCalcForm.Create(Self);
    if TsCalcForm(FPopupWindow).sDragBar1 <> nil then begin
      TsCalcForm(FPopupWindow).sDragBar1.Visible := False;
    end;

    TsCalcForm(FPopupWindow).Height := TsCalcForm(FPopupWindow).Height - 24;
    TsCalcForm(FPopupWindow).FormStyle := fsStayOnTop;
    TsCalcForm(FPopupWindow).FPrecision := 16;
  finally
    ControlState := ControlState - [csCreating];
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -