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

📄 pascal.txt

📁 基本语法
💻 TXT
📖 第 1 页 / 共 3 页
字号:
      const c = 1;
      implementation
      const d = 2;
      end.

  7.4 单元间交互参考
      允许交互参考,但不能产生循环参考(Circular unit references),若有发生必
      须有一单元之uses 放在 implementation 下

      program prog;
        uses unit1;
        const a = b;
      begin
      end.

      unit unit1;
      interface
      uses unit2;
      const b = c;
      implementation
      end.


      unit unit2;
      interface
      const c = 1;
      implementation
      uses unit1;    {避开循环参考}
      const d = b;
      end.
8.程序与函式
  8.1 程序: procedure 程序名( 参数;参数..);
            label {标记宣告}
            type  {型态定义}
            var   {变数宣告}
            procedure
            function
            begin

            {exit;}

            end;
            例如: Procedure print( pStr : String );
                  begin
                    writeln( Pstr );
                  end;


  8.2 函数: function 程序名( 参数;参数..):传回资料型态;
            label {标记宣告}
            const {常数定义}
            type  {型态定义}
            var   {变数宣告}
            procedure
            function
            begin

            {exit;}

            end;

            例如 function add( n1 , n2 : Integer ) : Integer;
                 begin
                   add := n1 + n2;
                   { result := N1 + N2; }
                 end;
  8.3 传值呼叫与传址呼叫
      procedure Add( Var sum : Integer ; n1 , n2 : integer );
      begin          ---{传址识别字}
        sum := n1 + n2;
        n1 := 0;
        n2 := 0;
      end;

      procedure test;
      var
         sum , a , b : integer;
      begin
         a := 100;
         b := 200;
         add( sum , a , b ); {sum = 300 , a = 100 , b = 200 }
      end;
  8.3 forward (前置宣告)
      前置宣告不须使用在 interface 之公有宣告!,公有宣告形同forword 作用
      procedure add( Var sum : integer ;n1 , n2 : integer ); forward;

      procedure base;
      var sum : integer;
      begin
        add(sum,4,5);

      end;

      procedure add ( Var sum : integer ;n1 , n2 : integer );
      begin
        sum := n1 + n2;
      end;
  8.4 External (外部函数宣告)

      Function MessageBox( Hwnd : Integer ;
                           text , caption : Pchar;
                           Flags : Integer ) : Integer ; Stdcall;
                           external 'User32.dll' Name 'MessageBoxa';

  8.5 呼叫惯例
       编译器指令    次序       清除                      呼叫惯例
       register     由左至右    函数      使用暂存器      pascal
       pascal       由左至右    函数      使用堆叠        pascal or c
       cdecl        由右至左    呼叫者    使用堆叠        pascal or c
       stdcall      由右至左    函数      使用堆叠        Windows Api

9.例外处理
  9.1 raise( 引发 )
      注意-1 : raise 启动一个物件,而不是一个物件,通常呼叫例外类别的 Create
               来快速建立
      例:SysUtils Execption
      constructor Exception.Create(const Msg: string);
      begin
        FMessage := Msg;
      end;


      constructor Exception.CreateFmt(const Msg: string;
                                      const Args: array of const);
      begin
        FMessage := Format(Msg, Args);
      end;

      --------------------------------------------------
      Function StrToIntRange( Var s:string;Min,Max:Longint):Longint;
      begin
        Result := StrToInt(s);
        If ( Result < Min ) or
           ( result > Max ) then
             raise ERangeError.CreateFmt( %d is not within the valid range of %d..%d',
      end;                                    [result,Min,Max]);

      注意-2 :控制不会从一个raise 叙述中回传,但在例外程式区块中允许再度
              引发例外,

  9.2 Try ... Except
      语法
         try
           .
           .
         except
           on Errorclass1 do ..;
           on Errorclass2 do ..;
           on Errorclass3 do ..;
         else
           {othes handle...};
         end;

         没有on ... do 之语法
         try
           .
           .
         except
           {exception handle}
         end;
         例子-1
         try
            result := sum div num;
         except
            on EZeroDivide do result := 0;
            on EOverFlow do result := 0;
            on EMatherror do result := 0;
            {由上而下检查择一处理,注意各种错误类别间之继承顺序}
         else
            result := 0;
         end;

  9.3 Try ... Finally
      例:
      reset(F);
      try
        processFile(F);
      finally
        closeFile(F);
      end;

  9.4 exit , break , continue 与 try .. finally 之运作
      例:
      procedure TForm1.MButton1Click(Sender: TObject);
      var
        I : integer;
        s : string;
      begin
        I := 10;
        s := '正常结束!';
         try
           while I <> 0 do
              begin
                I := I - 1;
                if I = 5 then
                   begin
                   s := 'exit 结束';
                   exit;
                   end;
              end;
         finally
           Showmessage(s); {显示exit 结束}
         end;
      end;

10.DLL (动态库}
   10.1 DLL 特徵
     project 使用之单元是采静态连结(Statically Linked),而DLLs 采动态连结
             (Dynamically Linked),故Projectl 中并包含DDLs之拷贝

     DLLs 可用任何遵守windows DLL 之开发工具来开发或使用,适合多种开发工具
     同时开发环境

   10.2 DLL 使用
      (1) 藉由名称(循序寻找)
          Procedure ImportByName; External 'TestLib.dll';
      (2) 藉由重新名称
          Procedure ImportByNewName;
                    External 'TestLib.dll' name 'ImportByName';
      (3) 藉由序号(找到程式的最快方法)
          Procedure ImportByOrdName;
                    External 'TestLib.dll' Index 1;

      (4) Windows Api 之呼叫
          Function MessageBox( Hwnd: Integer ; Text,Caption:Pchr
                               Flags:Integer):Integer;Stdcall;
                               External 'User32.dll' Name 'MessageBox';


      (5)动态库之输入程式单元
         (例-1):Delphi 之开发环境中之 uses windows
         (例-2):
             unit DateTime;{动态库之输入程式单元}
             interface
             type
              TTimeRec = Record
                 ss : integer;
                 mi : Integer;
                 hh : Integer;
              end;
             type
              TDateRec = Record
                 yy:Integer;
                 mm:Integer;
                 dd:Integer;
              end;

             Procedure SetTime(Var Time:TTimeRec);
             Procedure GetTime(Var Time:TTimeRec);
             Procedure SetDate(Var Date:TDateRec);
             Procedure GetDate(Var Date:TDateRec);

             Implementation
             Procedure SetTime; External 'DATETIME' index 1;
             Procedure GetTime; External 'DATETIME' index 2;
             Procedure SetDate; External 'DATETIME' index 3;
             Procedure GetDate; External 'DATETIME' index 4;

             end;
             ------------------------------------------------------

             program ShowTime; {呼叫程式}
             uses WinCrt , DateTime;
             var
               Time : TtimeRec;
             begin
               GetTime(Time);
               With Time Do
                 WriteLn( 'Time is',hh,':',mi,':',ss);
             end;
             -----------------------------------------------------

      (6)DDLs 之动态使用

         program ShowTime;
         uses WinProcs , WinTypesWinCrt;

         type
          TTimeRec = Record
             ss : integer;
             mi : Integer;
             hh : Integer;
          end;
         TGETTime = Procedure( var Time : TTimeRec );
         Var
           Time : TTimeRec;
           Handle : THandle;
           GetTime : TGetTime;
         Begin
           Handle := LoadLibrary('DATETIME.DLL');
           if Handle >= 32 then
              Begin
              @GetTime := GetProcAddress( Handle , 'GETTIME' );
              If @GetTime <> nil Then {or If Assigned(GetTime) then }
                 Begin
                 GetTime(Time)
                 With Time do
                      WriteLn('Time is ',hh,':',mi,':',ss);
                 end;
              FreeLibrary(handle);
              end;
         end.

   10.3 DDLs 之撰写
      10.3.1 例子
         library minmax;

         function Min( X , Y : Integer ) : Integer;
                                           StdCall;
           {避免非Delphi之程式呼叫,没有支援 register 呼叫惯例(内定值)}
         Begin
           If X < Y Then Min := X
           else Min := Y;
         end;

         function Max( X , Y : Integer ) : Integer;StdCall;
         Begin
           If X > Y Then Max := X
           else Max := Y;
         end;

         exports
           Min index 1 name Min Resident;
           Max index 2 name Max Resident;
         Begin
         end.
 

⌨️ 快捷键说明

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