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

📄 wwcalcedit.pas

📁 一套及时通讯的原码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{  This a editor prepared to edit numbers.
   Inplace edit calculations are supported.
   The following key operations are supported while editing.
   Operation      Valid Keys
   ---------      ----------
     %              %
     *              *
     +              +
     -              -
     .              . or ,
     /              /
     0-9            0-9
     1/x            r
     =              Enter
     Backspace      backspace
     C              Esc
     CE             Del
     ln             n
     log            l
     n!             !
     pi             p
     x^2            @
     x^3            #
     x^y            y

     MS             Ctrl+M
     M+             Ctrl+P
     MC             Ctrl+L
     MR             Ctrl+R

     inv            i
     sqrt           @

     +/-            F9
     (              (
     )              )
     A-F (HEX)      A-F
     And            &
     Ave            Ctrl+A
     bin            F8
     Byte           F4
     Cos            o or O
     Dat            Ins
     Dec            F6
     Degrees        F2
     dms            m
     Dword          F2
     Exp            x
     F-E            v
     Grads          F4
     Hex            F5
     Hyp            h
     Int            ;
     lsh            <
     M+             Ctrl+P
     MC             Ctrl+L
     Mod            %
     MR             Ctrl+R
     MS             Ctrl+M
     Not            ~
     oct            f7
     or             |
     qword          f12
     radians        f3
     Sin            s
     Sum            Ctrl+t
     Tan            t
     word           f3
     xor            ^













   Addition        +
   Subtraction     -
   Multiplication  *
   Division        /
   sqrt            s
   invert          r
   percentage      %
   C (Clear)       BackSpace and Left
   CE              Delete
   log             l
   ln              n
   sin             s or S
   cos             o
   pi              p
   tan             t
   exp             x
   x^Y             y
   n! factorial    !
   x^2  SQR        @
   OR              ^ |
   AND             &
   Memory          Ctrl-M
   Recall          Ctrl-R
   Memory Clear    Ctrl-L
   Memory Add      Ctrl-P
   Result          =
   Aso you can use the Backspace,
   Del key means CE command ( using 'l' char ),
   and the C command ( using 'c' char ).
}
unit wwCalcEdit;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Buttons, Forms, Dialogs,
  StdCtrls, wwdbedit, wwdbcomb, math, extctrls,wwcommon,wwCalculator;

type
  TwwCustomCalcEdit = class;

  TwwPopupCalcOptions = class(TPersistent)
  private
    FBackground:TPicture;
    FBackGroundStyle: TwwCalcBitmapDrawStyle;
    FMargin:integer;
    FOptions: TwwCalcOptions;
    FPanelColor:TColor;
    procedure SetBackgroundStyle(Value: TwwCalcBitmapDrawStyle);
    procedure SetOptions(Value: TwwCalcOptions);
    procedure SetMargin(Value: Integer);
    procedure SetPanelColor(Value: TColor);
  public
    constructor Create(AOwner: TComponent);
    destructor Destroy; override;
  protected
    procedure SetBackgroundBitmap(Value: TPicture); virtual;
  published
    property Background:TPicture read FBackground write SetBackGroundBitmap;
    property BackgroundStyle: TwwCalcBitmapDrawStyle read FBackgroundStyle write SetBackgroundStyle;
    property ButtonMargin: integer read FMargin write SetMargin default 3;
    property Options: TwwCalcOptions read FOptions write SetOptions default [];
    property PanelColor: TColor read FPanelColor write SetPanelColor default clBtnFace;
  end;

  TwwCalcPanel = class(TwwCalculator)
  private
    FAssociatedEdit:TwwCustomCalcEdit;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure CreateWnd; override;
    procedure Keypress(var Key: Char); override;
    procedure Compute(Sender: TObject); override;
//    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  public
    constructor Create(AOwner: TComponent); override;
    property AssociatedEdit:TwwCustomCalcEdit read FAssociatedEdit write FAssociatedEdit;
  end;

(*  TCalculatorWindow = class( TCustomControl )
  private
    FEdit: TCalculatorCombo;
    FButtonPressed,
    FButtonDown: TBotonPresionado;
    procedure DrawButton( aCanvas: TCanvas; B: TBotonPresionado;
                          Text: string );
    procedure ButtonRect( B: TBotonPresionado; var R: TRect );
    function ButtonToChar( B: TBotonPresionado ): char;
    function CharToButton( ch: char ): TBotonPresionado;
    procedure ButtonPushed( B: TBotonPresionado );
    procedure Draw( aCanvas: TCanvas );
  protected
    procedure Paint; override;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer ); override;
    function MouseToButton( X, Y: Integer ): TBotonPresionado;
    procedure PushButton( B: TBotonPresionado );
  public
//    procedure Repintar;
    property Edit: TCalculatorCombo read FEdit;
  end;
  *)
  TwwCustomCalcEdit = class(TwwDBCustomComboBox)
  private
    FDropDownCalc:TwwCalcPanel;
//    FCalculator: TCalculatorWindow;
    fResult: Double;
    FMemoryValue: Double;
    FLastOperator: Char;
    FSecondToLastOperator: Char;
    FLastOperand: Double;
    FClearOnNextKey: boolean;
    FLastOperatorEquals: Boolean;
    FDecimalEntered : Boolean;
    FCalcOptions : TwwPopupCalcOptions;
//    procedure ToggleSign;
    function GetValue: Double;
//    procedure SetCalcOptions(Value: TwwPopupCalcOptions);
    procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
    procedure CMCancelMode(var Message: TCMCancelMode); message CM_CANCELMODE;
  protected
    procedure ResetCalculator; virtual;
    function IsValidOperator(Key: Char): boolean; virtual;
    function IsUnaryOperator(Key:Char):boolean; virtual;
    function IsBinaryOperator(Key:Char):boolean; virtual;
    procedure CreateParams(var Params: TCreateParams); override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure KeyPress(var Key: Char); override;
    procedure Compute(OperatorKey:Char);
//    function GetPanel: TwwCalcPanel;
  public
    property Panel:TwwCalcPanel read fDropDownCalc write fDropDownCalc;
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
    procedure CreateWnd; override;
    //MAKE DoCloseUp and DoDropDown events.
    procedure CloseUp(Accept: Boolean); override;
    procedure DropDown; override;
    function IsDroppedDown: Boolean; override;
    property Value: Double read GetValue;// write fResult;
  published
    property CalcOptions: TwwPopupCalcOptions read FCalcOptions write FCalcOptions;
    property AutoSelect;
    property Color;
    property Ctl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property ImeMode;
    property ImeName;
    property MaxLength;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property TabOrder;
    property TabStop;
    property Visible;
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDropDown;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnStartDrag;
  end;

procedure Register;

implementation

var wwCalcHook: HHOOK;

function wwCalcHookProc(nCode: Integer; wParam: Integer; lParam: Integer): LResult; stdcall;
var r1, r2: TRect;
begin
  result := CallNextHookEx(wwCalcHook, nCode, wParam, lParam);
  with PMouseHookStruct(lParam)^ do
  begin
    if (wParam = WM_LBUTTONDOWN) or (wParam = WM_NCLBUTTONDOWN) then
    begin
      if (Screen.ActiveControl <> nil) and (Screen.ActiveControl is TwwCustomCalcEdit) then
        with (Screen.ActiveControl as TwwCustomCalcEdit) do
      begin
        { Auto-closeup if clicked outside of drop-down area }
        if (fDropDownCalc <> nil) and fDropDownCalc.Visible then begin
           GetWindowRect(fDropDownCalc.Handle, r1);
           GetWindowRect(Handle, r2);
           if (not PtInRect(r1, pt)) and (not PtInRect(r2, pt)) then
              PostMessage(Handle, CM_CANCELMODE, 0, 0);
        end
      end;
    end;
  end;
end;

function fcThisThat(const Clause: Boolean; TrueVal, FalseVal: Integer): Integer;
begin
  if Clause then result := TrueVal else Result := FalseVal;
end;

procedure TwwCalcPanel.Compute(Sender: TObject);
var
 ch: char;
 ButtonType: TwwCalcButtonType;
begin
  ButtonType := TwwCalcButtonType(TSpeedButton(Sender).Tag);
  case ButtonType of
    bt0..bt9:   ch := TSpeedButton(Sender).Caption[1];
    btDecimal:  ch := '.';
    btPlusMinus: ;
    btClearAll: ch := char(vk_escape);
    btClear:    ch := char(vk_back);
    btAdd:      ch:='+';
    btSubtract: ch:='-';
    btDivide:   ch:='/';
    btMultiply: ch:='*';
    btBackSpace:ch:=char(vk_back);
    btInverse:  ch:='r';
    btPercent:  ch:='%';
    btSqrt:     ch:='@';
    btMStore:   ch:='m';
    btMAdd:     ch:='p';
    btMRecall:  ch:='r';
    btMClear:   ch:='l';
    btEquals:   ch:='=';
    else
      ch:=' ';
    end;

    SendMessage(FAssociatedEdit.Handle, WM_CHAR, word(ch), 0);
end;

procedure TwwCalcPanel.Keypress(var Key: Char);
var
  combo: TwwCustomCalcEdit;
begin
  Combo:= owner as TwwCustomCalcEdit;
  inherited Keypress(Key);
end;

    constructor TwwPopupCalcOptions.Create(AOwner: TComponent);
    begin
      FBackground := TPicture.Create;
      FMargin := 3;
      FOptions := [];
      FPanelColor := clBtnFace;
    end;

    destructor TwwPopupCalcOptions.Destroy;
    begin
      if FBackGround <> nil then begin
         FBackground.Free;
         FBackground := nil;
      end;
      inherited;
    end;

    procedure TwwPopupCalcOptions.SetBackgroundBitmap(Value: TPicture);
    begin
      FBackground.assign(Value);
    end;

    procedure TwwPopupCalcOptions.SetBackgroundStyle(Value: TwwCalcBitmapDrawStyle);
    begin
      if Value<>FBackgroundStyle then begin
         FBackgroundStyle:= Value;
      end;
    end;

    procedure TwwPopupCalcOptions.SetOptions(Value: TwwCalcOptions);
    begin
      if Value<>FOptions then begin
         FOptions:= Value;
      end;
    end;

    procedure TwwPopupCalcOptions.SetPanelColor(Value: TColor);
    begin
      if Value<>FPanelColor then begin
         FPanelColor:= Value;
      end;
    end;

    procedure TwwPopupCalcOptions.SetMargin(Value: Integer);
    begin
      if Value<>FMargin then begin
         FMargin := Value;
      end;
    end;

procedure TwwCalcPanel.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  with Params do
  begin
    Style := Style or WS_CHILD or WS_CLIPSIBLINGS or WS_CLIPCHILDREN; // ak
    Style := Style or WS_BORDER;
    {$ifdef win32}
    ExStyle := WS_EX_TOOLWINDOW;

    ExStyle := ExStyle or WS_EX_CONTROLPARENT; //ak
    {$endif}
    WindowClass.Style := CS_SAVEBITS;
  end;
end;

constructor TwwCalcPanel.Create(AOwner: TComponent);
begin
   inherited Create(AOwner);
   FAssociatedEdit := TwwCustomCalcEdit(AOwner);
   ControlStyle := ControlStyle + [csReplicatable];
   ControlStyle := ControlStyle + [csNoDesignVisible];
   ControlStyle := ControlStyle + [csReflector];
   Color := clBtnFace;
//   Height := 175;
//   Width := 250;
end;

procedure TwwCalcPanel.CreateWnd;
begin
  inherited CreateWnd;
  Windows.SetParent(Handle, 0);
end;

(*procedure TwwCalcPanel.Draw( aCanvas: TCanvas );
var
  OldBrushColor: TColor;
  OldPenColor: TColor;
begin
  inherited;
  exit;
  OldBrushColor := aCanvas.Brush.Color;
  OldPenColor := aCanvas.Pen.Color;
  try
    aCanvas.Brush.Color := clRed;//FEdit.CalculatorBackColor;
    aCanvas.FillRect( ClientRect );
    aCanvas.Rectangle( ClientRect.left, ClientRect.top,
                      ClientRect.right, ClientRect.bottom );
    acanvas.TextOut(10,10,'Sum');
    // Pintar botones BACK, CE y C
//    aCanvas.Brush.Color := FEdit.CalculatorButtonsColor;
//    aCanvas.Font := Font;//FEdit.BackFont;
  //  DrawButton( aCanvas, bpBack, STR_BACK );
  finally
    aCanvas.Brush.Color := OldBrushColor;
    aCanvas.Pen.Color := OldPenColor;

⌨️ 快捷键说明

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