📄 wwcalculator.pas
字号:
Alignment := taLeftJustify;
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-FMemoryStatus.Canvas.textheight('M'))div 2), Width-16,btnHeight-4);
end
else if not (cboShowStatus in Options) then 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 TwwCalculator.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
Message.result:= 1;
end;
Procedure TwwCalculator.Reset; {Clear everything out}
begin
FCurrentValue := 0.0;
FResultEdit.Text := '0';
FDecimalEntered:=False;
FResultValue:=0.0;
if FStatusLabel <> nil then FStatusLabel.Caption:='';
FClearOnNextKey:=false;
FLastOp:=btNone;
FNextToLastOp:=btNone;
FLastStatus:='';
FLastOperatorEquals := false;
FLastOperand:=0.0;
end;
procedure TwwCalculator.Compute(Sender: TObject);
begin
DoCalc(TwwCalcButtonType(TSpeedButton(Sender).Tag));
end;
procedure TwwCalculator.DoCalc(ButtonType:TwwCalcButtonType);
const
MemoryStoreMap: array [Boolean] of string = (' M','');
var
Temp: string;
TempValue: Double;
flag:boolean;
begin
try
case ButtonType of
bt0..bt9:
begin
if FClearOnNextKey then begin
if FLastOperatorEquals then Reset
else FResultEdit.Text := '0';
end;
FClearOnNextKey := false;
if (FResultEdit.Text = '0') or (FCurrentValue = 0) then
FResultEdit.Text := '';
FBackSpaceValid := True;
FResultEdit.Text := FResultEdit.Text + Btns[ButtonType].Caption;
FCurrentValue := StrToFloat(FResultEdit.Text);
if (FStatusLabel <> nil) then
FStatusLabel.Caption:=FStatusLabel.Caption+OpToChar(ButtonType);
end;
btDecimal:
begin
if FClearOnNextKey then Reset;
If not FDecimalEntered then begin
FCurrentValue := StrToFloat(FResultEdit.Text);
FResultEdit.Text := FResultEdit.Text + Btns[ButtonType].Caption;
if (FStatusLabel <> nil) then
FStatusLabel.Caption:=FStatusLabel.Caption+OpToChar(ButtonType);
FDecimalEntered:=true;
end
else Beep;
end;
btPlusMinus:
begin
FCurrentValue := StrToFloat(FResultEdit.Text) * -1;
FResultEdit.Text := FloatToStr(FCurrentValue);
if FStatusLabel <> nil then begin
if FClearOnNextKey then
FStatusLabel.Caption := '0'
else
FStatusLabel.Caption := FLastStatus+FResultEdit.Text;
end;
end;
btClearAll:
begin
Reset;
end;
btClear:
begin
FCurrentValue := 0;
FResultEdit.Text := '0';
if FStatusLabel <> nil then begin
if FClearOnNextKey then
FStatusLabel.Caption := '0'
else
FStatusLabel.Caption := FLastStatus;
end;
end;
btPercent,btAdd,btSubtract,btDivide,btMultiply,btEquals:
begin
TempValue := StrToFloat(FResultEdit.Text);
if (Length(FResultEdit.Text) > 0) and (ButtonType <> btEquals) then begin
if (FLastOperatorEquals) then begin
FLastOperand := TempValue;
FLastOp := ButtonType;
FLastOperatorEquals := False;
if FStatusLabel <> nil then
FStatusLabel.Caption:=FLastStatus+OpToChar(ButtonType);
FLastStatus := FStatusLabel.Caption;
FCurrentValue := 0;
end
else if not(FLastOp in [btMultiply,btDivide,btAdd,btSubtract,btPercent]) then
begin
FLastOperand := TempValue;
FLastOp := ButtonType;
FResultValue:=tempvalue;
if FStatusLabel <> nil then
FStatusLabel.Caption:=FStatusLabel.Caption+OpToChar(ButtonType);
FLastStatus := FStatusLabel.Caption;
FCurrentValue := 0;
end
else if (FLastOp in [btMultiply,btDivide,btAdd,btSubtract]) then begin
if ButtonType = btPercent then begin
fResultValue := (fResultValue * TempValue)/100.0;
fLastOperand := TempValue;
end
else begin
case FLastOp of
btMultiply: fResultValue := fResultValue * TempValue;
btDivide: fResultValue := fResultValue / TempValue;
btAdd: fResultValue := fResultValue + TempValue;
btSubtract: fResultValue := fResultValue - TempValue;
end;
FLastOperand := TempValue;
end;
if (FStatusLabel <> nil) then begin
If ((ButtonType = btMultiply) or (ButtonType=btDivide))
and ((FLastOp = btAdd) or (FLastOp=btSubtract)) then
FStatusLabel.Caption:='('+FStatusLabel.Caption+')';
FStatusLabel.Caption := FStatusLabel.Caption+OpToChar(ButtonType);
FLastStatus := FStatusLabel.Caption;
if ButtonType = btPercent then begin
FLastStatus := FStatusLabel.Caption;
FStatusLabel.Caption := FlastStatus+OpToChar(btEquals) + FloatToStr(FResultValue);
end;
end;
FCurrentValue := 0;
end;
FLastOp:=ButtonType;
FResultEdit.Text := FloatToStr(FResultValue);
FCurrentValue := 0;
end
else if (Length(FResultEdit.Text) > 0)then begin
if FLastOperatorEquals or (FLastOp = btPercent) then TempValue := FLastOperand
else FLastOperand := TempValue;
case FLastOp of
btPercent: fResultValue := (fResultValue * FLastOperand)/100.0;
btMultiply: fResultValue := fResultValue * TempValue;
btDivide: fResultValue := fResultValue / TempValue;
btAdd: fResultValue := fResultValue + TempValue;
btSubtract: fResultValue := fResultValue - TempValue;
end;
if StatusLabel <> nil then begin
if FLastOperatorEquals then begin
FStatusLabel.Caption := FLastStatus;
FStatusLabel.Caption := FStatusLabel.Caption + OpToChar(FLastOp) + FloatToStr(FLastOperand);
end
else if FLastOp = btPercent then begin
FStatusLabel.Caption := FLastStatus;
FStatusLabel.Caption:='('+FStatusLabel.Caption+')'+FloatToStr(FLastOperand)+Optochar(btPercent);
end;
FStatusLabel.Caption:='('+FStatusLabel.Caption+')';
FLastStatus := FStatusLabel.Caption;
FStatusLabel.Caption := FStatusLabel.Caption+OpToChar(ButtonType);
FStatusLabel.Caption := FStatusLabel.Caption + FloatToStr(FResultValue);
end;
FLastOperatorEquals := True;
FResultEdit.Text := FloatToStr(FResultValue);
FClearOnNextKey := True;
FCurrentValue := 0;
end
else Beep;
end;
btBackSpace:
if FBackSpaceValid then begin
Temp := FResultEdit.Text;
if Temp[Length(Temp)] in ['0'..'9'] then
begin
Delete(Temp, Length(Temp),1);
if Temp = '' then begin
FBackSpaceValid := False;
Temp := '0';
end;
FCurrentValue := StrToFloat(Temp);
FResultEdit.Text := FloatToStr(FCurrentValue);
if FStatusLabel <> nil then begin
Temp := FStatusLabel.Caption;
Delete(Temp, Length(Temp),1);
FStatusLabel.Caption := Temp;
end;
end
else begin
if FStatusLabel <> nil then begin
Temp := FStatusLabel.Caption;
if Temp[Length(Temp)] = DecimalSeparator then begin
FDecimalEntered := False;
Delete(Temp, Length(Temp),1);
FStatusLabel.Caption := Temp;
end
else FBackSpaceValid := False;
end;
end;
end
else Beep;
btInverse:
begin
FCurrentValue := 1 / StrToFloat(FResultEdit.Text);
FResultEdit.Text := FloatToStr(FCurrentValue);
if FStatusLabel <> nil then begin
if FClearOnNextKey then
FStatusLabel.Caption := '0'
else
FStatusLabel.Caption := FLastStatus+FResultEdit.Text;
end;
end;
btSqrt:
begin
FCurrentValue := Sqrt(StrToFloat(FResultEdit.Text));
FResultEdit.Text := FloatToStr(FCurrentValue);
if FStatusLabel <> nil then begin
if FClearOnNextKey then
FStatusLabel.Caption := '0'
else
FStatusLabel.Caption := FLastStatus+FResultEdit.Text;
end;
end;
btMStore:
begin
FMemoryValue := StrToFloat(FResultEdit.Text);
FMemoryValue := FMemoryValue * 1;
FCurrentValue := 0;
end;
btMAdd:
begin
TempValue := FMemoryValue;
FMemoryValue := StrToFloat(FResultEdit.Text);
FMemoryValue := (FMemoryValue * 1) + TempValue;
end;
btMRecall:
begin
FResultEdit.Text := FloatToStr(FMemoryValue);
FCurrentValue := 0;
end;
btMClear:
begin
FMemoryValue := 0;
end;
end; // case ButtonType of...
except
on E: Exception do begin
FResultEdit.Text := E.Message;
Reset;
end;
end;
FMemoryStatus.Caption := MemoryStoreMap[FMemoryValue = 0];
end;
function TwwCalculator.IsBinaryOperator(ButtonType: TwwCalcButtonType):boolean;
begin
result := False;
case ButtonType of
btMultiply,btDivide,btAdd,btSubtract,btEquals: result := True;
end;
end;
procedure TwwCalculator.SetBackgroundBitmapDrawStyle(Value: TwwCalcBitmapDrawStyle);
begin
if Value<>FBackgroundBitmapDrawStyle then
begin
FBackgroundBitmapDrawStyle:= Value;
FullRepaint;
end
end;
procedure TwwCalculator.SetBackgroundBitmap(Value: TPicture);
var i:integer;
begin
if (Value = nil) or (Value.Graphic=nil) or Value.Graphic.Empty then begin
for i:= 0 to ControlCount-1 do begin
if COntrols[i] is TwwCalcButton then
TwwCalcButton(controls[i]).FTransparent := False;
end;
end
else begin
for i:= 0 to ControlCount-1 do begin
if COntrols[i] is TwwCalcButton then
TwwCalcButton(controls[i]).FTransparent := True;
end;
end;
FBackGroundBitmap.Assign(Value);
FPaintBitmap.FreeImage;
InitBitmapsFlag := True;
FullRepaint;
end;
function TwwCalculator.GetText: string;
begin
Result := FResultEdit.Text;
end;
procedure TwwCalculator.SetBorder3D(const Value: Boolean);
begin
if F3D <> Value then begin
F3D := Value;
invalidate;
end;
end;
procedure TwwCalculator.SetText(const Value: string);
begin
if FResultEdit <> nil then
FResultEdit.Text := Value;
end;
procedure TwwCalculator.SetOptions(Value: TwwCalcOptions);
var ChangedOptions: TwwCalcOptions;
begin
if FOptions <> Value then
begin
ChangedOptions := (FOptions - Value) + (Value - FOptions);
FOptions := Value;
if ChangedOptions <> [] then begin
{ for i:= 0 to ControlCount-1 do begin
if COntrols[i] is TwwCalcButton then begin
TwwCalcButton(controls[i]).FOptions := Value;
if (FBackGroundBitmap <> nil) and (FBackGroundBitmap.Graphic <> nil) and not FBackgroundbitmap.Graphic.empty then
TwwCalcButton(controls[i]).FTransparent := True
else TwwCalcButton(controls[i]).FTransparent := False;
end;
end;}
CalcButtons;
FullRepaint;
end;
end;
end;
procedure TwwCalculator.RefreshSummary;
begin
if StatusLabel <> nil then begin
FStatusLabel.Caption := 'Calculator';
end;
end;
procedure TwwCalculator.FullRepaint;
var i:integer;
begin
InitBitmapsFlag:= True;
invalidate;
for i:= 0 to ControlCount-1 do begin
controls[i].invalidate;
end;
end;
procedure TwwCalculator.SetMargin(Value: Integer);
begin
if FMargin <> Value then
begin
FMargin := Value;
CalcButtons;
end;
end;
procedure TwwCalculator.SetPanelColor(Value: TColor);
var i:integer;
begin
if FPanelColor <> Value then
begin
FPanelColor := Value;
Color := Value;
if FMemoryStatus<>nil then
FMemoryStatus.Color := Value;
if FMemoryBevel <> nil then
FMemoryBevel.FColor := Value;
for i:= 0 to ControlCount-1 do begin
if COntrols[i] is TwwCalcButton then
TwwCalcButton(controls[i]).ButtonColor := Value;
end;
FullRepaint;
end;
end;
procedure Register;
begin
RegisterComponents('Samples', [TwwCalculator]);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -