📄 fccalculator.pas
字号:
else aLeft := -pad+ (Width-5-BtnWidth)-(2-simplecalcflag)*(BtnWidth+ButtonMargin);
Btns[bt9] := ButtonRecord(bt9,5+row*(btnheight+ButtonMargin),aLeft, BtnWidth, btnHeight, '9',clBlue,'Puts this number in display.');
inc(row);
Btns[bt6] := ButtonRecord(bt6,5+row*(btnheight+ButtonMargin),aLeft, BtnWidth, btnHeight, '6',clBlue,'Puts this number in display.');
inc(row);
Btns[bt3] := ButtonRecord(bt3,5+row*(btnheight+ButtonMargin),aLeft, BtnWidth, btnHeight, '3',clBlue,'Puts this number in display.');
if cboSimpleCalc in Options then begin
inc(row);
Btns[btPlusMinus] := ButtonRecord(btPlusMinus,5+row*(btnheight+ButtonMargin),aLeft, BtnWidth, btnHeight, '+/-',clBlue,'Changes sign of displayed number.');
end
else begin
inc(row);
Btns[btDecimal] := ButtonRecord(btDecimal,5+row*(btnheight+ButtonMargin),aLeft, BtnWidth, btnHeight, DecimalSeparator,clBlue,'Inserts a decimal point.');
end;
if cboHideEditor in Options then row := 1
else row := 2;
if cboSimpleCalc in Options then dec(row);
Btns[btDivide] := ButtonRecord(btDivide,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-(1-simplecalcflag)*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, '/',clRed,'Divides.');
inc(row);
Btns[btMultiply] := ButtonRecord(btMultiply,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-(1-simplecalcflag)*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, '*',clRed,'Multiplies.');
inc(row);
Btns[btSubtract] := ButtonRecord(btSubtract,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-(1-simplecalcflag)*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, '-',clRed,'Subtracts.');
inc(row);
Btns[btAdd] := ButtonRecord(btAdd,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-(1-simplecalcflag)*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, '+',clRed,'Adds.');
if (csDesigning in ComponentState) or not (cboSimpleCalc in Options) then begin
if cboHideEditor in Options then row := 1
else row := 2;
Btns[btsqrt] := ButtonRecord(btsqrt,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-0*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, 'sqrt',clBlue,'Calculates squareroot of number.');
inc(row);
Btns[btPercent] := ButtonRecord(btPercent,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-0*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, '%',clBlue,'Calculates Percentage');
inc(row);
Btns[btInverse] := ButtonRecord(btInverse,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-0*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, '1/x',clBlue,'Calculates the reciprocal.');
inc(row);
end;
if not (cboSimpleCalc in Options) then
Btns[btEquals] := ButtonRecord(btEquals,5+row*(btnheight+ButtonMargin), (Width-5-BtnWidth)-0*(BtnWidth+ButtonMargin), BtnWidth, btnHeight, '=',clRed,'Performs any operation on previous numbers.');
flag := False;
for j:= 0 to ControlCount-1 do
if COntrols[j] is TfcCalcButton then flag := True;
if flag then begin
for j:= 0 to ControlCount-1 do
if COntrols[j] is TfcCalcButton then
with TfcCalcButton(Controls[j]) do begin
SetBounds(Btns[fbtntype].Left, Btns[fbtntype].Top, Btns[fbtntype].Width,Btns[fbtntype].Height);
ButtonFontColor := Btns[fbtntype].Color;
ButtonColor := Btns[fbtntype].ButtonColor;
// ButtonColor := PanelColor;
Hint := Btns[fbtnType].Hint;
// FCalcOptions := Options;
Font.Height := self.Font.Height;
Font.Name := self.Font.Name;
Font.Style := self.Font.Style;
OnClick := Compute;
if (FBackGroundBitmap <> nil) and (FBackGroundBitmap.Graphic <> nil) and not FBackgroundbitmap.Graphic.empty then
FTransparent := True
else FTransparent := False;
case TfcCalcButtonType(Tag) of
btMRecall..btMAdd: begin
if (cboHideMemory in Options) or (cboSimpleCalc in Options) then
ControlStyle := ControlStyle + [csNoDesignVisible]
else ControlStyle := ControlStyle - [csNoDesignVisible];
end;
btSqrt,btPercent,btInverse,btClear:
begin
if cboSimpleCalc in Options then
ControlStyle := ControlStyle + [csNoDesignVisible]
else ControlStyle := ControlStyle - [csNoDesignVisible];
end;
end;
end;
end
else begin
for i := Low(TfcCalcButtonType) to High(TfcCalcButtonType) do
begin
with TfcCalcButton.Create(Self) do begin
Parent := Self;
SetBounds(Btns[i].Left, Btns[i].Top, Btns[i].Width,Btns[i].Height);
fBtnType:=Btns[i].BtnType;
Hint := Btns[i].Hint;
Caption := Btns[i].Caption;
ButtonFontColor := Btns[i].Color;
// ButtonColor := PanelColor;
ButtonColor := Btns[i].ButtonColor;
OnClick := Compute;
// FCalcOptions := Options;
Font.Height := self.Font.Height;
Font.Name := self.Font.Name;
Font.Style := self.Font.Style;
if (FBackGroundBitmap <> nil) and (FBackGroundBitmap.Graphic <> nil) and not FBackgroundbitmap.Graphic.empty then
FTransparent := True
else FTransparent := False;
Tag := Ord(i);
case TfcCalcButtonType(Tag) of
btMRecall..btMAdd: begin
if (cboHideMemory in Options) or (cboSimpleCalc in Options) then
ControlStyle := ControlStyle + [csNoDesignVisible]
else ControlStyle := ControlStyle - [csNoDesignVisible];
end;
btSqrt,btPercent,btInverse,btClear:
begin
if cboSimpleCalc in Options then
ControlStyle := ControlStyle + [csNoDesignVisible]
else ControlStyle := ControlStyle - [csNoDesignVisible];
end;
end;
end;
end;
end;
if (not (cboHideMemory in Options)) and not (cboSimpleCalc in Options) then begin
if FMemoryBevel = nil then begin
FMemoryBevel := TfcCalcBevel.Create(Self);
FMemoryBevel.Parent := Self;
end;
with FMemoryBevel do begin
Parent := Self;
SetBounds(5,clrBtnh+1,BtnWidth,BtnHeight-1);
FColor := PanelColor;
end;
if FMemoryStatus = nil then begin
FMemoryStatus := TfcCalcLabel.Create(Self);
with FMemoryStatus do begin
Parent := Self;
Autosize := False;
Transparent := True;
Alignment := taCenter;
end;
end;
j := clrbtnh;
with FMemoryStatus do begin
Font.Height := self.Font.Height;
Font.Name := self.Font.Name;
Font.Style := self.Font.Style;
SetBounds(5,j+((BtnHeight-FMemoryStatus.Canvas.textheight('M'))div 2),BtnWidth,BtnHeight);
end;
end
else begin
if FMemoryBevel <> nil then begin
FMemoryBevel.Free;
FMemoryBevel:=nil;
end;
end;
if FResultEdit = nil then begin
FResultEdit := TRightEdit.Create(Self);
with FResultEdit do begin
//3/1/2002-PYW-Add support for TDBCtrlGrid
ControlStyle := ControlStyle + [csReplicatable];
Parent := Self;
Visible := not (cboHideEditor in Options);
AutoSize := False;
// EditAlignment := eaRightAlignEditing;
TLabel(FResultEdit).Alignment := taRightJustify;
Font.Height := -13;
Font.Name := 'Arial';
Font.Style := [];
Text := '0';
ReadOnly := True;
OnKeyDown := ResultKeyDown;
OnKeyUp := ResultKeyUp;
end;
if cboShowDecimal in Options then
Windows.HideCaret(FResultEdit.handle);
end;
if (cboHideEditor in Options) then FResultEdit.Visible := False
else if (csDesigning in ComponentState) then FResultEdit.Visible := True;
if not FResultEdit.Visible and (csDesigning in ComponentState) then
FResultEdit.SetBounds(5, 5, 0, 0)
else FResultEdit.SetBounds(5, 5, Width-10, btnHeight);
if False and (cboShowStatus in Options) then begin
if (FStatusBevel = nil) then begin
FStatusBevel := TfcCalcBevel.Create(Self);
FStatusBevel.Parent := Self;
end;
if (FStatusLabel = nil) then begin
FStatusLabel := TfcCalcStatusLabel.Create(Self);
with FStatusLabel do begin
Parent := Self;
Autosize := False;
Transparent := True;
Alignment := taLeftJustify;
ShowHint := True;
Font.Height := self.Font.Height;
Font.Name := self.Font.Name;
Font.Style := self.Font.Style;
end;
end;
FStatusBevel.SetBounds(5, Height-5-btnHeight, Width-10,btnHeight);
FStatusLabel.SetBounds(9, Height-5-btnHeight+((BtnHeight-FStatusLabel.Canvas.textheight('M'))div 2), Width-16,btnHeight-4);
end
else begin
if FStatusBevel <> nil then begin
FStatusBevel.Visible := False;
FStatusBevel.SetBounds(5, Height-5-FStatusBevel.height, 0,0);
FStatusBevel.Free;
FStatusBevel := nil;
end;
if FStatusLabel <> nil then begin
FStatusLabel.Visible := False;
FStatusLabel.SetBounds(7, Height-5-btnHeight+2,0,0);
FStatusLabel.Free;
FStatusLabel := nil;
end;
end;
FullRepaint;
end;
procedure TfcCalculator.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
Message.result:= 1;
end;
Procedure TfcCalculator.Reset; {Clear everything out}
begin
FCurrentValue := 0.0;
CalcEdit.Text := '0';
FResultValue:=0.0;
if FStatusLabel <> nil then FStatusLabel.Caption:='';
FClearOnNextKey:=false;
FLastOp:=btNone;
FNextToLastOp:=btNone;
FLastStatus:='';
FLastOperatorEquals := false;
FLastOperand:=0.0;
FClearStatus:=False;
FDecimalEntered := False;
FLastButtonType:=btNone;
end;
procedure TfcCalculator.Compute(Sender: TObject);
begin
DoCalc(TfcCalcButtonType(TSpeedButton(Sender).Tag));
end;
type EfcCalcException = Class(Exception);
procedure TfcCalculator.DoCalc(ButtonType:TfcCalcButtonType);
const
MemoryStoreMap: array [Boolean] of string = (' M','');
var
Temp,teststr: string;
TempValue: Double;
index,digits:integer;
selectallflag:boolean;
function DecimalInStr(str:string): boolean;
begin
result := false;
if Pos(DecimalSeparator,str)>0 then
result := true;
end;
function GetFormattedtext(val:extended;showdecimal:boolean=False):string;
var s,fmtstr:string;
i,places:integer;
begin
result :='';
if cboDigitGrouping in Options then begin
if showdecimal and (FDecimalPlaces > -1) then begin
places := FDecimalPlaces;
fmtstr := '#,##0.';
for i:=1 to places do
fmtstr := fmtstr+'0';
result := FormatFloat(fmtstr,Val);
end
// 3/1/2002-PYW-Added code to handle very large numbers with Exponential notation.
else if (Frac(Val)=0) then begin //(Val - Trunc(Val)) = 0.0 then begin
if (FDecimalPlaces < 0) or (not showdecimal) then
result := FormatFloat('#,##0',Val)
else begin
places := FDecimalPlaces;
fmtstr := '#,##0.';
for i:=1 to places do
fmtstr := fmtstr+'0';
result := FormatFloat(fmtstr,Val);
end;
end
else begin
// Limit to 15 significant digits
s:= FloatToStrF(Val, ffFixed, 15, 15);
// Now val is accurate to 15 digits precision, so now lets get it in our format
Val:= StrtoFloat(s);
s:= FormatFloat('#.##############', Val);
places := length(s)-pos(DecimalSeparator,S);
fmtstr := '#,##0.';
for i:=1 to places do
fmtstr := fmtstr+'0';
result := FormatFloat(fmtstr,Val);
end;
end
else begin
if showdecimal and (FDecimalPlaces > -1) then begin
places := FDecimalPlaces;
fmtstr := '###0.';
for i:=1 to places do
fmtstr := fmtstr+'0';
result := FormatFloat(fmtstr,Val);
end
// 3/1/2002-PYW-Added code to handle very large numbers with Exponential notation.
else if (Frac(Val)=0) then begin//Val - Trunc(Val)) = 0.0 then begin
if (FDecimalPlaces < 0) or (not showdecimal) then
result := FormatFloat('###0',Val)
else begin
places := FDecimalPlaces;
fmtstr := '###0.';
for i:=1 to places do
fmtstr := fmtstr+'0';
result := FormatFloat(fmtstr,Val);
end;
end
else begin
// Limit to 15 significant digits
s:= FloatToStrF(Val, ffFixed, 15, 15);
// Now val is accurate to 15 digits precision, so now lets get it in our format
Val:= StrtoFloat(s);
s:= FormatFloat('#.##############', Val);
places := length(s)-pos(DecimalSeparator,S);
fmtstr := '###0.';
for i:=1 to places do
fmtstr := fmtstr+'0';
result := FormatFloat(fmtstr,Val);
end;
end;
end;
begin
// Options := Options+ [cboDigitGrouping];
selectallflag := false;
try
case ButtonType of
bt0..bt9:
begin
if FClearOnNextKey then begin
if FLastOperatorEquals or FClearStatus then Reset
else CalcEdit.Text := '0';
FDecimalEntered := False;
end;
FClearOnNextKey := false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -