www.pudn.com > PASSCAL高级计算器.rar > CalcUnit.pas


unit CalcUnit; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, ExtCtrls, Buttons, Math, Registry; 
 
type 
  TCalcForm = class(TForm) 
    Memo1: TMemo; 
    FontDialog1: TFontDialog; 
    ColorDialog1: TColorDialog; 
    Timer1: TTimer; 
    Panel1: TPanel; 
    Panel2: TPanel; 
    procedure Memo1KeyPress(Sender: TObject; var Key: Char); 
    procedure FormShow(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure Memo1KeyDown(Sender: TObject; var Key: Word; 
      Shift: TShiftState); 
    procedure Memo1KeyUp(Sender: TObject; var Key: Word; 
      Shift: TShiftState); 
    procedure Timer1Timer(Sender: TObject); 
    procedure Memo1MouseMove(Sender: TObject; Shift: TShiftState; X, 
      Y: Integer); 
  private 
    theAllList: TStringList; 
    strResult: String; 
    strCancel: String; 
    function CalcNonoKh(theList: TStringList): Extended; 
    function MemoLinesToStringList(var theList: TStringList; iCurPosition: Integer; 
        bCalcOrCommond: Boolean = False): String;      // 返回值为被忽略的杂字 
//**   theList 为传递变量,iCurPosition 为光标当前位置, bCalcOrCommond 区别是用做计算还是用做命令 
    function CalcFormList(theList: TStringList): Extended; 
    function IsInOperatorsArray(theStr: String): Boolean; 
    function IsFloat(theStr: String): Boolean; 
    function DowToWeekStr(Dow: Word): String; 
    procedure WritePosition; 
    procedure RevertSize; 
  public 
    { Public declarations } 
  end; 
 
Function FontStyleToString(FontStyle : TFontStyles) : String; 
Function StringToFontStyle(strFontStyle : String) : TFontStyles; 
procedure showTaskbar; 
procedure hideTaskbar; //隐藏 
 
var 
  CalcForm: TCalcForm; 
 
const 
  TheOperators: array [0..36] of String = ('+','-','*','/','^','!','SQR','SQRT','%','ABS','INT','TRUNC','FRAC', 
        'ARCTAN','COS','LN','PI','SIN','TAN','ROUND','LOG2','LOG10','SETFONT','BKCOLOR','ARCSIN','ARCCOS', 
        'BACKCOLOR','CLEAR','CLOSE','QUIT','EXIT','(',')','FULLSCREEN','FULLSCR','REVERT','REV'); 
 
implementation 
 
{$R *.dfm} 
 
//**************************************************************************************** 
// 以下是自定义函数或过程 
//**************************************************************************************** 
 
procedure TCalcForm.WritePosition; 
var wRegistry: TRegistry; 
    bReg: Boolean; 
begin 
    wRegistry:=TRegistry.Create;  //KeyExists 
    wRegistry.RootKey:=HKEY_LOCAL_MACHINE; 
    bReg:=wRegistry.KeyExists('Software\WgqAdvanCalc'); 
    If bReg then 
    begin 
        wRegistry.OpenKey('Software\WgqAdvanCalc',False); 
        wRegistry.WriteInteger('iTop',Self.Top); 
        wRegistry.WriteInteger('iLeft',Self.Left); 
        wRegistry.WriteInteger('iHeight',Self.Height); 
        wRegistry.WriteInteger('iWidth',Self.Width); 
        wRegistry.CloseKey; 
    end; 
    wRegistry.Free; 
end; 
 
procedure TCalcForm.RevertSize; 
var wRegistry: TRegistry; 
    bReg: Boolean; 
begin 
    wRegistry:=TRegistry.Create;  //KeyExists 
    wRegistry.RootKey:=HKEY_LOCAL_MACHINE; 
    bReg:=wRegistry.KeyExists('Software\WgqAdvanCalc'); 
    If bReg then 
    begin 
        wRegistry.OpenKey('Software\WgqAdvanCalc',False); 
        Self.Top:=wRegistry.ReadInteger('iTop'); 
        Self.Left:=wRegistry.ReadInteger('iLeft'); 
        Self.Height:=wRegistry.ReadInteger('iHeight'); 
        Self.Width:=wRegistry.ReadInteger('iWidth'); 
        wRegistry.CloseKey; 
    end; 
    wRegistry.Free; 
end; 
 
procedure hideTaskbar; //隐藏 
var wndHandle : THandle; 
    wndClass : array[0..50] of Char; 
begin 
    StrPCopy(@wndClass[0], 'Shell_TrayWnd'); 
    wndHandle := FindWindow(@wndClass[0], nil); 
    ShowWindow(wndHandle, SW_HIDE); 
End; 
 
procedure showTaskbar; 
var wndHandle : THandle; 
    wndClass : array[0..50] of Char; 
begin 
    StrPCopy(@wndClass[0], 'Shell_TrayWnd'); 
    wndHandle := FindWindow(@wndClass[0], nil); 
    ShowWindow(wndHandle, SW_RESTORE); 
end;                                        
 
function TCalcForm.DowToWeekStr(Dow: Word): String; 
begin 
    case Dow of 
        1: Result:='星期日'; 
        2: Result:='星期一'; 
        3: Result:='星期二'; 
        4: Result:='星期三'; 
        5: Result:='星期四'; 
        6: Result:='星期五'; 
        7: Result:='星期六'; 
    end; 
end; 
 
function TCalcForm.IsFloat(theStr: String): Boolean; 
begin 
    try 
        StrToFloat(theStr); 
        Result:=True; 
    except 
        Result:=False; 
    end; 
end; 
 
Function FontStyleToString(FontStyle : TFontStyles) : String; 
var strFontStyles : String;    //(fsBold, fsItalic, fsUnderline, fsStrikeOut); 
begin 
	strFontStyles:='-'; 
	if fsBold in FontStyle then strFontStyles:=strFontStyles+'fsBold'+'-' 
	else strFontStyles:=strFontStyles+'aaaaaa'+'-'; 
	if fsItalic in FontStyle then strFontStyles:=strFontStyles+'fsItalic'+'-' 
	else strFontStyles:=strFontStyles+'aaaaaaaa'+'-'; 
	if fsUnderline in FontStyle then strFontStyles:=strFontStyles+'fsUnderline'+'-' 
	else strFontStyles:=strFontStyles+'aaaaaaaaaaa'+'-'; 
	if fsStrikeOut in FontStyle then strFontStyles:=strFontStyles+'fsStrikeOut'+'-' 
	else strFontStyles:=strFontStyles+'aaaaaaaaaaa'+'-'; 
	Result:=strFontStyles; 
end; 
 
Function StringToFontStyle(strFontStyle : String) : TFontStyles; 
var FontStyle : TFontStyles; 
begin 
	FontStyle:=[]; 
	If Copy(strFontStyle,2,6)='fsBold' then  
		FontStyle:=FontStyle+[fsBold]; 
	If Copy(strFontStyle,9,8)='fsItalic' then  
		FontStyle:=FontStyle+[fsItalic];	 
	If Copy(strFontStyle,18,11)='fsUnderline' then  
		FontStyle:=FontStyle+[fsUnderline]; 
	If Copy(strFontStyle,30,11)='fsStrikeOut' then  
		FontStyle:=FontStyle+[fsStrikeOut]; 
	Result:=FontStyle; 
end; 
 
function TCalcForm.IsInOperatorsArray(theStr: String): Boolean; 
var i: Integer; 
begin 
    Result:=False; 
    For i:=Low(TheOperators) to High(TheOperators) do 
    begin 
        If theStr = TheOperators[i] then 
        begin 
            Result:=True; 
            Break; 
        end; 
    end; 
end; 
 
function TCalcForm.CalcFormList(theList: TStringList): Extended; 
var i,iBegin, iEnd: Integer; 
    dTemp: Extended; 
    theListIn: TStringList; 
begin 
        theListIn:=TStringList.Create; 
        theListIn.Clear; 
        iBegin:=-1; 
        iEnd:=-1; 
        while theList.Count>1 do 
        begin 
            For i:=0 to theList.Count-1 do If (theList.Strings[i]='(') then iBegin:=i; 
            If iBegin<>-1 then 
            begin 
                For i:=iBegin to theList.Count-1 do 
                begin 
                    If (theList.Strings[i]=')') then 
                    begin 
                        iEnd:=i; break; 
                    end; 
                end; 
            end; 
            If (iBegin = -1)and(iEnd = -1) then 
            begin 
                theListIn.Clear; 
                For i:=0 to theList.Count-1 do 
                    theListIn.Add(theList.Strings[i]); 
                If theListIn.Count<>0 then 
                    dTemp:=CalcNonoKh(theListIn); 
                theList.Clear; 
                theList.Add(FloatToStr(dTemp)); 
            end 
            else if (iBegin <> -1)and(iEnd <> -1) then 
            begin 
                theListIn.Clear; 
                For i:=iBegin+1 to iEnd-1 do 
                    theListIn.Add(theList.Strings[i]); 
                If theListIn.Count<>0 then 
                    dTemp:=CalcNonoKh(theListIn); 
                For i:=1 to iEnd-iBegin do 
                begin 
                    theList.Delete(iBegin); 
                end; 
                theList.Strings[iBegin]:=FloatToStr(dTemp); 
                iBegin:=-1; iEnd:=-1; 
            end 
            else begin 
                Memo1.Lines.Add('表达式错误!∴'); 
                Exit; 
            end; 
        end; 
        If UpperCase(theList.Strings[0])='PI' then 
            Result:=3.1415926535897932384626433832795 
        else 
            Result:=StrToFloat(theList.Strings[0]); 
        theListIn.Free; 
end; 
 
function TCalcForm.MemoLinesToStringList(var theList: TStringList; iCurPosition: Integer; 
    bCalcOrCommond: Boolean = False): String; 
var strTemp, str: String; 
    i, j, iBegin, iEnd, iTemp: Integer; 
    bbool, bBegin, bEnd: Boolean; 
begin 
    theList.Clear; 
    Result:=''; 
    bbool:=False;  bBegin:=False; bEnd:=False; 
    strTemp:=Memo1.Lines.Text; 
    For i:=iCurPosition to Length(strTemp) do 
    begin 
        If strTemp[i]=#13 then 
        begin 
            iEnd:=i; 
            bEnd:=True; 
            break; 
        end; 
    end; 
    For i:=iCurPosition downto 1 do 
    begin 
        If (strTemp[i-1]+strTemp[i])='∴' then 
        begin 
            iBegin:=i+1; 
            bBegin:=True; 
            break; 
        end; 
        If strTemp[i]=#13 then 
        begin 
            bbool:=True; 
            iTemp:=i; 
        end; 
        If bbool and (strTemp[i]='=') then 
        begin 
            iBegin:=iTemp+1; 
            bBegin:=True; 
            break; 
        end; 
    end; 
    If not bBegin then iBegin:=1; 
    If not bEnd then iEnd:=Length(strTemp); 
    j:=iBegin; 
    For i:=iBegin to iEnd do 
    begin 
        If (IsInOperatorsArray(strTemp[i]))or(strTemp[i]=')')or(strTemp[i]='(') 
            or(strTemp[i]=#13) then 
        begin 
            str:=Copy(strTemp,j,i-j); 
            theList.Add(str); 
            theList.Add(strTemp[i]); 
            j:=i+1; 
        end; 
    end; 
    str:=Copy(strTemp,j,i-j); 
    theList.Add(Trim(str)); 
    theList.Add(Trim(strTemp[i])); 
    If bCalcOrCommond then 
    begin 
        i:=0; 
        while i <= theList.Count-1 do 
        begin 
            If Trim(theList.Strings[i])='' then 
                theList.Delete(i) 
            else 
                i:=i+1; 
        end; 
    end; 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        theList.Strings[i]:=Trim(theList.Strings[i]); 
        If bCalcOrCommond then 
        begin 
            If theList.Count>1 then 
                theList.Delete(i) 
            else 
                i:=i+1; 
        end else 
        begin 
            If IsInOperatorsArray(Uppercase(Trim(theList.Strings[i]))) then 
            begin 
                If (Uppercase(Trim(theList.Strings[i]))='SETFONT')or 
                    (Uppercase(Trim(theList.Strings[i]))='BKCOLOR')or 
                    (Uppercase(Trim(theList.Strings[i]))='BACKCOLOR')or 
                    (Uppercase(Trim(theList.Strings[i]))='FULLSCREEN')OR 
                    (Uppercase(Trim(theList.Strings[i]))='FULLSCR')OR 
                    (Uppercase(Trim(theList.Strings[i]))='REV')OR 
                    (Uppercase(Trim(theList.Strings[i]))='REVERT') then 
                    theList.Delete(i) 
                else 
                    i:=i+1; 
            end else 
            begin 
                If (not IsFloat(Trim(theList.Strings[i]))) then 
                begin 
                    Result:=Result+theList.Strings[i]; 
                    theList.Delete(i); 
                end 
                else i:=i+1; 
            end; 
        end; 
    end; 
end; 
 
function TCalcForm.CalcNonoKh(theList: TStringList): Extended; 
var i,j: Integer; 
    dTemp: Extended; 
begin 
//****************************************************************************** 
    i:=0;  
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'PI') then 
        begin 
            dTemp:=3.1415926535897932384626433832795; 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end; 
        i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (theList.Strings[i] = '!') then 
        begin 
            dTemp:=1; 
            For j:=1 to Trunc(StrToFloat(theList.Strings[i-1])) do 
            begin 
                dTemp:=dTemp*j; 
            end; 
            theList.Delete(i-1); 
            theList.Strings[i-1]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (theList.Strings[i] = '^') then 
        begin 
            dTemp:=Power(StrToFloat(theList.Strings[i-1]),StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i-1); 
            theList.Delete(i); 
            theList.Strings[i-1]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'SQR') then 
        begin                                                                               
            dTemp:=Sqr(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'SQRT') then 
        begin 
            dTemp:=Sqrt(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (theList.Strings[i] = '%') then 
        begin 
            dTemp:=(Trunc(StrToFloat(theList.Strings[i-1])) mod Trunc(StrToFloat(theList.Strings[i+1]))); 
            theList.Delete(i-1); 
            theList.Delete(i); 
            theList.Strings[i-1]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'INT')OR(UpperCase(theList.Strings[i]) = 'TRUNC') then 
        begin 
            dTemp:=Trunc(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'ROUND') then 
        begin 
            dTemp:=Round(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'ABS') then 
        begin 
            dTemp:=Abs(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'FRAC') then 
        begin 
            dTemp:=Frac(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'ARCTAN') then 
        begin 
            dTemp:=Arctan(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'SIN') then 
        begin 
            dTemp:=Sin(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'COS') then 
        begin 
            dTemp:=Cos(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'TAN') then 
        begin 
            dTemp:=Tan(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'ARCSIN') then 
        begin 
            dTemp:=ArcSin(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'ARCCOS') then 
        begin 
            dTemp:=ArcCos(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'LN') then 
        begin 
            dTemp:=Ln(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'LOG2') then 
        begin 
            dTemp:=Log2(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (UpperCase(theList.Strings[i]) = 'LOG10') then 
        begin 
            dTemp:=Log10(StrToFloat(theList.Strings[i+1])); 
            theList.Delete(i); 
            theList.Strings[i]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (theList.Strings[i] = '*')or(theList.Strings[i] = '/') then 
        begin 
            If theList.Strings[i] = '*' then 
                dTemp:=StrToFloat(theList.Strings[i-1])*StrToFloat(theList.Strings[i+1]); 
            If theList.Strings[i] = '/' then 
                dTemp:=StrToFloat(theList.Strings[i-1])/StrToFloat(theList.Strings[i+1]); 
            theList.Delete(i-1); 
            theList.Delete(i); 
            theList.Strings[i-1]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
//****************************************************************************** 
    i:=0; 
    while i<=(theList.Count-1) do 
    begin 
        If (theList.Strings[i] = '+')or(theList.Strings[i] = '-') then 
        begin 
            If theList.Strings[i] = '+' then 
                dTemp:=StrToFloat(theList.Strings[i-1])+StrToFloat(theList.Strings[i+1]); 
            If theList.Strings[i] = '-' then 
                dTemp:=StrToFloat(theList.Strings[i-1])-StrToFloat(theList.Strings[i+1]); 
            theList.Delete(i-1); 
            theList.Delete(i); 
            theList.Strings[i-1]:=FloatToStr(dTemp); 
        end else 
            i:=i+1; 
    end; 
    If theList.Count<>1 then Memo1.Lines.Add('您的表达式有问题或系统不支持这种表达式!∴'); 
    Result:=StrToFloat(theList.Strings[0]); 
//****************************************************************************** 
end; 
 
//**************************************************************************************** 
// 以下是窗口事件 
//**************************************************************************************** 
 
procedure TCalcForm.Memo1KeyPress(Sender: TObject; var Key: Char); 
var dTemp: Extended; 
begin 
    try 
        If Key = '=' then 
        begin 
            strCancel:=MemoLinesToStringList(theAllList,Memo1.SelStart); 
            If theAllList.Count<>0 then 
            begin 
                dTemp:=CalcFormList(theAllList); 
                strResult:=FloatToStr(dTemp); 
            end; 
        end;  
    except 
        Memo1.Lines.Add('表达式错误!∴'); 
    end; 
end; 
 
procedure TCalcForm.FormShow(Sender: TObject); 
var wRegistry: TRegistry; 
    bReg: Boolean; 
begin 
    theAllList:=TStringList.Create; 
    strCancel:=''; 
//**************************************************************************** 
    wRegistry:=TRegistry.Create;  //KeyExists 
    wRegistry.RootKey:=HKEY_LOCAL_MACHINE; 
    bReg:=wRegistry.KeyExists('Software\WgqAdvanCalc'); 
    If not bReg then 
    begin 
        If not wRegistry.CreateKey('Software\WgqAdvanCalc') then 
        begin 
            Application.MessageBox('未知的原因,创建注册信息失败!','注册软件',MB_ICONWARNING+MB_OK+MB_TOPMOST); 
            Exit; 
        end; 
    end else 
    begin 
        try 
        wRegistry.OpenKey('Software\WgqAdvanCalc',False); 
        Memo1.Font.Name:=wRegistry.ReadString('FontName'); 
        Memo1.Font.Size:=wRegistry.ReadInteger('FontSize'); 
        Memo1.Font.Color:=wRegistry.ReadInteger('FontColor'); 
        Memo1.Font.Style:=StringToFontStyle(wRegistry.ReadString('FontStyle')); 
        Memo1.Color:=wRegistry.ReadInteger('BackColor'); 
        Panel2.Color:=wRegistry.ReadInteger('Panel2Color'); 
        wRegistry.CloseKey; 
        except 
        end; 
    end; 
    wRegistry.Free; 
    try 
        RevertSize; 
    except 
    end; 
end; 
 
procedure TCalcForm.FormClose(Sender: TObject; var Action: TCloseAction); 
var wRegistry: TRegistry; 
    bReg: Boolean; 
begin 
    theAllList.Free; 
//****************************************************************************** 
    wRegistry:=TRegistry.Create;  //KeyExists 
    wRegistry.RootKey:=HKEY_LOCAL_MACHINE; 
    bReg:=wRegistry.KeyExists('Software\WgqAdvanCalc'); 
    If bReg then 
    begin 
        wRegistry.OpenKey('Software\WgqAdvanCalc',False); 
        wRegistry.WriteString('FontName',Memo1.Font.Name); 
        wRegistry.WriteInteger('FontSize',Memo1.Font.Size); 
        wRegistry.WriteInteger('FontColor',Memo1.Font.Color); 
        wRegistry.WriteString('FontStyle',FontStyleToString(Memo1.Font.Style)); 
        wRegistry.WriteInteger('BackColor',Memo1.Color); 
        wRegistry.WriteInteger('Panel2Color',Panel2.Color); 
        wRegistry.CloseKey; 
    end; 
    wRegistry.Free; 
    showTaskbar; 
    If Self.BorderStyle <> bsNone then 
        WritePosition; 
end; 
 
procedure TCalcForm.Memo1KeyDown(Sender: TObject; var Key: Word; 
  Shift: TShiftState); 
var theList: TStringList; 
begin 
    If Key = 13 then 
    begin 
        theList:=TStringList.Create; 
        MemoLinesToStringList(theList,Memo1.SelStart,True); 
        If UpperCase(Trim(theList.Text))='SETFONT' then 
        begin 
            FontDialog1.Font:=Memo1.Font; 
            FontDialog1.Execute; 
            Memo1.Font:=FontDialog1.Font; 
        end; 
        If (UpperCase(Trim(theList.Text))='BACKCOLOR')or 
            (UpperCase(Trim(theList.Text))='BKCOLOR') then 
        begin 
            ColorDialog1.Color:=Memo1.Color; 
            ColorDialog1.Execute; 
            Memo1.Color:=ColorDialog1.Color; 
            Panel2.Color:=ColorDialog1.Color; 
        end; 
        theList.Free; 
    end; 
end; 
 
procedure TCalcForm.Memo1KeyUp(Sender: TObject; var Key: Word; 
  Shift: TShiftState); 
var theList: TStringList; 
    yearstr,monthstr,daystr,dowstr: Word; 
    iTemp: Integer; 
begin 
    If Memo1.Lines.Text = '' then Exit; 
    If (Memo1.Lines.Text[Memo1.SelStart]='=')and(Key=187) then 
    begin 
        Memo1.SelText:=strResult; 
        If Trim(strCancel)<>'' then 
        begin 
            Memo1.Lines.Add('“'+Trim(strCancel)+'”'+'是个非法的东东,被忽略!∴'); 
            strCancel:=''; 
        end; 
        Memo1.Lines.Add(''); 
        strResult:=''; 
    end; 
    If Key = 13 then 
    begin 
        theList:=TStringList.Create; 
        MemoLinesToStringList(theList,Memo1.SelStart,True); 
        If UpperCase(Trim(theList.Text))='CLEAR' then 
        begin 
            Memo1.Clear; 
        end 
        else If (UpperCase(Trim(theList.Text))='EXIT')or 
            (UpperCase(Trim(theList.Text))='QUIT')or 
            (UpperCase(Trim(theList.Text))='CLOSE') then 
        begin 
            Close; 
        end 
        else If (UpperCase(Trim(theList.Text))='FULLSCREEN')or 
            (UpperCase(Trim(theList.Text))='FULLSCR') then 
        begin 
            If Self.BorderStyle <> bsNone then 
            begin 
                WritePosition; 
                iTemp:=Memo1.SelStart; 
                Self.BorderStyle:=bsNone; 
                Self.WindowState:=wsMaximized; 
                hideTaskbar; 
                Memo1.SetFocus; 
                Memo1.SelStart:=iTemp; 
            end; 
        end 
        else If (UpperCase(Trim(theList.Text))='REVERT')or 
            (UpperCase(Trim(theList.Text))='REV') then 
        begin 
            showTaskbar; 
            iTemp:=Memo1.SelStart; 
            Self.BorderStyle:=bsSizeable; 
            Self.WindowState:=wsNormal; 
            Memo1.SetFocus; 
            Memo1.SelStart:=iTemp; 
            RevertSize; 
        end 
        else If (UpperCase(Trim(theList.Text))='HELP') then 
        begin 
            Memo1.Lines.Add('**************************************'); 
            Memo1.Lines.Add('1、用法:'); 
            Memo1.Lines.Add('输入数学表达式后,输入“=”,系统自动计算结果;'); 
            Memo1.Lines.Add('一个表达式可以一行输入,也可以多行输入;'); 
            Memo1.Lines.Add('表达式可以是加(+)、减(-)、乘(*)、除(/)、括号(()),以及系统支持的数学函数的任意组合。'); 
            Memo1.Lines.Add('输入系统命令后,按回车键即可执行系统命令。系统不区分大小写。'); 
            Memo1.Lines.Add('2、系统命令:'); 
            Memo1.Lines.Add('Clear:清除屏幕。'); 
            Memo1.Lines.Add('Quit/Exit/Close:退出系统。'); 
            Memo1.Lines.Add('Help:帮助系统。'); 
            Memo1.Lines.Add('FullScreen/FullScr:把窗口最大化。'); 
            Memo1.Lines.Add('Revert/Rev:把窗口还原为原来的大小。'); 
            Memo1.Lines.Add('DateTime/Now:返回当前日期时间,以及星期几。'); 
            Memo1.Lines.Add('About:关于本软件,有作者信息及电话。'); 
            Memo1.Lines.Add('SetFont:设置窗口的字体,系统自动记忆设置后的字体。'); 
            Memo1.Lines.Add('BackColor/BkColor:设置窗口的背景颜色,系统自动记忆设置后的颜色。'); 
            Memo1.Lines.Add('3、系统支持的数学函数:'); 
            Memo1.Lines.Add('PI ! ^ % SQR SQRT INT/TRUNC ROUND ABS FRAC SIN COS TAN ARCSIN ARCCOS ARCTAN LN LOG2 LOG10'); 
            Memo1.Lines.Add('PI:圆周率。'); 
            Memo1.Lines.Add('Exp!:计算Exp(表达式)的介乘。'); 
            Memo1.Lines.Add('Exp1^Exp2:计算Exp1的Exp2次方。'); 
            Memo1.Lines.Add('Exp1%Exp2:计算Exp1除以Exp2后的余数。'); 
            Memo1.Lines.Add('Sqr(Exp):计算Exp的平方。'); 
            Memo1.Lines.Add('Sqrt(Exp):计算Exp的平方根。'); 
            Memo1.Lines.Add('Int(Exp)/Trunc(Exp):把Exp截断取整。'); 
            Memo1.Lines.Add('Round(Exp):把Exp四舍五入。'); 
            Memo1.Lines.Add('Abs(Exp):取Exp的绝对值。'); 
            Memo1.Lines.Add('Frac(Exp):取Exp的小数部分。'); 
            Memo1.Lines.Add('Sin(Exp):求Exp的正玄。'); 
            Memo1.Lines.Add('Cos(Exp):求Exp的余玄。'); 
            Memo1.Lines.Add('Tan(Exp):求Exp的正切。'); 
            Memo1.Lines.Add('ArcSin(Exp):求Exp的反正玄。'); 
            Memo1.Lines.Add('ArcCos(Exp):求Exp的反余玄。'); 
            Memo1.Lines.Add('ArcTan(Exp):求Exp的反正切。'); 
            Memo1.Lines.Add('Ln(Exp):求Exp的自然对数。'); 
            Memo1.Lines.Add('Log2(Exp):求Exp的以2为低的对数。'); 
            Memo1.Lines.Add('Log10(Exp):求Exp的以10为低的对数。'); 
            Memo1.Lines.Add('**************************************∴'); 
        end 
        else If (UpperCase(Trim(theList.Text))='ABOUT') then 
        begin 
            Memo1.Lines.Add('高级计算器 WgqSoft_AdvCalc 1.0'); 
            Memo1.Lines.Add('作者:王功勤'); 
            Memo1.Lines.Add('电话:13675480121'); 
            Memo1.Lines.Add('完成时间:2003年8月21日 ∴'); 
        end 
        else If (UpperCase(Trim(theList.Text))='DATETIME')or 
            (UpperCase(Trim(theList.Text))='NOW') then 
        begin 
            DecodeDatefully(now(),yearstr,monthstr,daystr,dowstr); 
            Memo1.Lines.Add('当前日期时间:'); 
            Memo1.Lines.Add(IntToStr(yearstr)+'年'+IntToStr(monthstr)+'月'+IntToStr(dayStr)+'日 ' 
                +DowToWeekStr(dowstr)+' '+TimeToStr(Now)+' ∴'); 
        end 
        else begin 
            If (not IsInOperatorsArray(Trim(theList.Text)))and(not IsFloat(Trim(theList.Text))) then 
            begin 
                If (Trim(theList.Text)<>'')and(Uppercase(Trim(theList.Text))<>'SETFONT') 
                    and(Uppercase(Trim(theList.Text))<>'BKCOLOR')AND(Uppercase(Trim(theList.Text))<>'BACKCOLOR') then 
                begin 
                    Memo1.Lines.Add('“'+Trim(theList.Text)+'”'+'不是一个合法的操作命令!∴'); 
                end; 
            end; 
        end; 
        theList.Free; 
    end; 
end; 
 
procedure TCalcForm.Timer1Timer(Sender: TObject); 
begin 
    Memo1.Cursor:=crNone; 
end; 
 
procedure TCalcForm.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X, 
  Y: Integer); 
begin 
if timer1.Enabled then //判断定时器是否打开 
begin //如打开,则重新开始计时 
  timer1.enabled:=false; 
  timer1.enabled:=true; 
end; 
Memo1.cursor:=crIBeam; 
end; 
 
end.