孤飛飄瀟 發表於 2005-11-20 3:59:00
|
我在以前搞過dephi嵌入win32asm,所以對編程環境還是很熟悉。可惜沒有機會編些簡單的程序,看看下面快速上手的資料感覺和VB的難度差不多吧。不過“誤信一切速成,最後都不成”!任何東西深入了都是廣闊的空間,而我就在那個空間外徘徊........
delphi 過渡知識參考資料:
essential pascal http://www.marcocantu.com/epascal/chinese/default.htm
object pascal 和vcl 參考nicrosoft的delphi教程還可以 http://www.sunistudio.com/nicrosoft/book/DesignInDelphi(Official).zip
因爲BCB會用,當個工具在上面畫界面還是很易用的,所以RAD的優勢就在這,掌握了語法規則 有了需求分析和設計,就讓我們達積木吧。
以下是快速摘要
註釋 {this is a comment} (* this is another comment *) // this is a comment up to the end of the line
大小寫不區分 語句結束時不能忘了加分號,更確切地說,必須記着把語句和緊接它的語句分開。語句分行唯一的限制是字符串不能跨行。
關鍵字 absolute 指令 (變量) abstract 指令 (方法) and 運算符 (布爾) array 類型 as 運算符 (RTTI) asm 語句 assembler 向後兼容 (彙編) at 語句 (異常處理) automated 訪問類別符 (類) begin 塊標記 case 語句 cdecl 函數調用協定 class 類型 const 聲明或指令(參數) constructor 特殊方法 contains 運算符 (集合) default 指令 (屬性) destructor 特殊方法 dispid dispinterface 界面類別符 dispinterface 類型 div 運算符 do 語句 downto 語句 (for) dynamic 指令 (方法) else 語句 (if 或 case) end 塊標記 except 語句 (異常處理) export 向後兼容 (類) exports 聲明 external 指令 (函數) far 向後兼容 (類) file 類型 finalization 單元結構 finally 語句 (異常處理) for 語句 forward 函數指令 function 聲明 goto 語句 if 語句 implementation 單元結構 implements 指令 (屬性) in 運算符 (集合) - 工程結構 index 指令 (dipinterface界面) inherited 語句 initialization 單元結構 inline 向後兼容 (見 asm) interface 類型 is 運算符 (RTTI) label 聲明 library 程序結構 message 指令 (方法) mod 運算符 (數學) name 指令 (函數) near 向後兼容 (類) nil 數值 nodefault 指令 (屬性) not 運算符 (布爾) object 向後兼容 (類) of 語句 (case) on 語句 (異常處理) or 運算符 (布爾) out 指令 (參數) overload 函數指令 override 函數指令 package 程序結構 (控件包) packed 指令 (記錄) pascal 函數調用協定 private 訪問類別符 (class) procedure 聲明 program 程序結構 property 聲明 protected 訪問類別符 (類) public 訪問類別符 (類) published 訪問類別符 (類) raise 語句 (異常處理) read 屬性類別符 readonly dispatch 界面類別符 record 類型 register 函數調用協定 reintroduce 函數指令 repeat 語句 requires 程序結構 (控件包) resident 指令 (函數) resourcestring 類型 safecall 函數調用協定 set 類型 shl 運算符 (數學) shr 運算符 (數學) stdcall 函數調用協定 stored 指令 (屬性) string 類型 then 語句 (if) threadvar 聲明 to 語句 (for) try 語句 (異常處理) type 聲明 unit 單元結構 until 語句 uses 單元結構 var 聲明 virtual 指令 (方法) while 語句 with 語句 write 屬性類別符 writeonly dispatch 界面類別符 xor 運算符 (布爾)
優先法則 單目運算符 (最高優先級) @ 取變量或函數的地址(返回一個指針) not 邏輯取反或按位取反 乘除及按位運算符 * 相乘或集合交集 / 浮點相除 div 整數相除 mod 取模 (整數相除的餘數) as 程序運行階段類型轉換 (RTTI運算符) and 邏輯或按位求和 shl 按位左移 shr 按位右移 加減運算符 + 相加、集合並集、字符串連接或指針增加一個偏移量 - 相減、集合差集或指針減少一個偏移量 or 邏輯或按位或運算 xor 邏輯或按位異或運算 關係及比較運算符(最低優先級) = 判斷是否相等 <> 判斷是否不相等 < 判斷是否小於 > 判斷是否大於 <= 判斷是否小於或等於,或是否是一個集合的子集 >= 判斷是否大於或等於,或是否是一個集合的父集 in 判斷是否是集合成員 is 判斷對象是否類型兼容 (又一個RTTI運算符)
Boolean (ByteBool WordBool LongBool) Char Integer ShortInt -128~127 Byte 0~255 SmallInt -32768~32767 Word 0~65535 LongInt LongWord Int64 Interger Cardinal AnsiChar WidChar #=Chr(),把數字轉化爲字符,Ord爲相反,把字符轉化爲數字 #9 Tab鍵 #10 換行 #13 回車
類型轉換 Chr 將一個有序數據轉換爲一個ANSI字符 Ord 將一個有序類型值轉換爲它的序號 Round 轉換一個實型值爲四捨五入後的整型值 Trunc 轉換一個實型值爲小數截斷後的整型值 Int 返回浮點數的整數部分 IntToStr 將數值轉換爲字符串 IntToHex 將數值轉換爲十六進制數字符串 StrToInt 將字符串轉換爲一個整型數,如字符串不是一個合法的整型將引發異常 StrToIntDef 將字符串轉換爲一個整數,如字符串不合法返回一個缺省值 Val 將字符串轉換爲一個數字(傳統Turbo Pascal例程用於向後兼容) Str 將數字轉換爲格式化字符串(傳統Turbo Pascal例程用於向後兼容) StrPas 將零終止字符串轉換爲Pascal類型字符串,在32位Delphi中這種類型轉換是自動進行的 StrPCopy 拷貝一個Pascal類型字符串到一個零終止字符串, 在32位Delphi中這種類型轉換是自動進行的 StrPLCopy 拷貝Pascal類型字符串的一部分到一個零終止字符串 FloatToDecimal 將一個浮點數轉換爲包含指數、數字及符號的十進制浮點記錄類型 FloatToStr 將浮點值轉換爲缺省格式的字符串 FloatToStrF 將浮點值轉換爲特定格式的字符串 FloatToText 使用特定格式,將一個浮點值拷貝到一個字符串緩衝區 FloatToTextFmt 同上面例程,使用特定格式,將一個浮點值拷貝到一個字符串緩衝區 StrToFloat 將一個Pascal字符串轉換爲浮點數 TextToFloat 將一個零終止字符串轉換爲浮點數
Dec 將例程中的參數值遞減1或一個特定的值,其中特定值可在第二個可選參數中定義 Inc 將例程中的參數值增加1或一個特定的值 Odd 如果參數爲奇數返回真 Pred 根據參數在其數據類型定義中的序列,返回參數值的前驅值 Succ 返回參數值的後繼值 Ord 返回參數值在其數據類型值集合中的序號 Low 返回參數對應的有序數據類型的最小取值 High 返回參數對應的有序數據類型的最大取值
實數: Single Double Extended Real {$REALCOMPATIBILITY ON}據說不標準,要兼容舊的格式使用這個宏 Comp Currency
TDateTime = type Double
爲了後續使用或直接用於變量,需要給自定義類型命名。如果自定義一個命名的類型,你必須將代碼放在特定的type區,如下所示: 最多的如定義類等。 type Uppercase = 'A'..'Z'; Temperatures = array [1..24] of Integer; Date = record Month: Byte; Day: Byte; Year: Integer; end; Colors = (Red, Yellow, Green, Cyan, Blue, Violet); Letters = set of Char; end;
子界類型 a...b a~b爲有序類型
枚舉類型 type Colors = (Red, Yellow, Green);//園括弧括起來的,在屬性列表中最多出現。 end; 只能有一個值 CPP = enum
集合類型 type Letters = set of Char; var Letters1,Letters2:Letters; begin Letters1 := ['A'.'B'.'C']; Letters2 := ['K']; end; 是什麼什麼的集合,可以有多個屬性。 CPP = 多字段與或
Font.Style := [fsBold, fsItalic]; // two styles Font.Style :=OldStyle + [fsBold, fsItalic];
數組類型 type MonthTemps = array [1..24, 1..31] of Integer; YearTemps = array [Jan..Dec] of Integer;
記錄類型 type Date = record Year: Integer; Month: Byte; Day: Byte; end; 類似 structure
指針 type PointerToInt = ^Integer; var P:^Integerl X:Integer; begin P := @X; p = &x; X :=10; x = 10; P^ :=20; *p = 20; end; 除了表示已分配內存的地址外,指針還能通過New 例程在堆中動態分配內存,不過當你不需要這個指針時,你也必須調用Dispose 例程釋放你動態分配的內存。 P:=nil;
Pointer 類型,如void *; 實際上,Delphi 中必須使用指針的情況很少,這是Delphi開發環境一個誘人的優點。
文件類型 type IntFile = file of Integer; 能打開一個與這個結構相應的物理文件、向文件中寫入整數、或者從文件中讀取當前的值
簡單語句: 單行:X:= Y+Z; 調用一個過程 Randomize; 複合語句begin end;
賦值 := =表示關係運算符,用於判斷是否相等用 c: ==
條件 if 用and、 or 、 not等布爾操作符聯接起來的條件 then statement; else statement; end;
Case語句 case Number of 1: Text:='One'; 2: Text:='Two'; else //default; Text:='Unknow'; end;
循環 循環條件爲順序數 for I:=1 to 10 do statement; for I := 10 downto 1 do 遞減 statement;
while conditions do statement; repeat statement; until conditions;
With語句 begin with BirthDay do begin Year:=1999; Month :=2; Day :=14; end; end; 減少BirthDay.xxx 的代碼工作量,但現在的自動完成比他效率高 可以with多個,同樣屬性的取最後一個的,容易出錯。
函數和過程 procedure Hello; begin ShowMessage ('Hello world!'); end;
調用:Hello;
function Double (Value: Integer) : Integer; begin Double := Value * 2; //函數名做返回值 end;
function Double2 (Value: Integer) : Integer; begin Result := Value * 2; //Result作返回值 end;
引用參數 同C++中的(&xx) 使用var關鍵字表示 procedure Produ(var Value:Integer); 既做傳遞參數,又把值返回給調用程序。
procedure Produ(out Value:Interger); 用作返回。
常量參數 使用const
開放數組參數 function Sum (const A: array of Integer): Integer; var I: Integer; begin Result := 0; for I := Low(A) to High(A) do Result := Result + A[I]; end; 可以傳遞大小不定的數組
類型變化的開放數組參數 可以接收多種類型的數組參數 function SumAll (const Args: array of const): Extended;
調用協定 fastcall 只要有可能,傳遞到CPU寄存器多達3個,操作更快 delphi3+ 缺省標記
win32 API是stdcall,win16 API是原始的Pascal調用和cdecl調用混合體
方法: 是一種特殊的函數或過程 他與類這一數據類型相對應,每個事件都定義一個方法,通常是過程。
forward 申明 欲聲明一個過程或函數,而且只給出它的名字和參數,不列出其實現代碼,需要在句尾加forward 關鍵字: procedure Hello; forward; 可用來寫遞歸調用 當你在一個單元(關於單元的更多內容見下一章)的interface 部分聲明一個過程或一個函數時,它被認爲是一個forward聲明,即使沒有forward關鍵字也一樣。
過程類型 type IntProc = procedure (var Num: Integer);
IP: IntProc; begin IP := otherProcedure;
函數重載 overload關鍵字 function Min (A,B: Integer): Integer; overload; function Min (A,B: Int64): Int64; overload; function Min (A,B: Single): Single; overload; function Min (A,B: Double): Double; overload; function Min (A,B: Extended): Extended; overload;
確省參數 procedure MessBox (Msg: string; Caption: string = 'Warning'; Flags: LongInt = mb_OK or mb_IconHand);
字符串 ShortString AnsiString WideSting 如果只簡單地用String定義字符串,那麼該字符串可能是短字符串也可能是ANSI長字符串,這取決於$H 編譯指令的值,$H+(確省)代表長字符串(ANSIString 類型)。長字符串是Delphi 庫中控件使用的字符串。 SetLength (String1, 200);設置長度分配內存 類型轉換,PChar(String) SetWindowText (Handle, PChar (Label1.Caption)); GetWindowText (Handle, PChar (S1), Length (S1));
格式化字符串 Format ('First %d, Second %d', [n1, n2]);//集合 d(decimal) 將整型值轉換爲十進制數字字符串 x (hexadecimal) 將整型值轉換爲十六進制數字字符串 p (pointer) 將指針值轉換爲十六進制數字字符串 s (string) 拷貝字符串、字符、或字符指針值到一個輸出字符串 e (exponential) 將浮點值轉換爲指數表示的字符串 f (floating point) 將浮點值轉換爲浮點表示的字符串 g (general) 使用浮點或指數將浮點值轉換爲最短的十進制字符串 n (number) 將浮點值轉換爲帶千位分隔符的浮點值 m (money) 將浮點值轉換爲現金數量表示的字符串,轉換結果取決於地域設置,詳見Delphi幫助文件的Currency and date/time formatting variables主題
內存 動態數組 Array1: array of Integer; SetLength(Array1, 100); 下標從0開始
普通數組下標可以隨便寫 動態數組不行,Length,Hight,Low函數了解動態數組狀況 for I := Low (Array1) to High (Array1) do Array1 [I] := I;
windows相關 type THandle = LongWord;
外部聲明 // forward declaration function LineTo (DC: HDC; X, Y: Integer): BOOL; stdcall; // external declaration (instead of actual code)取代真實代碼 function LineTo; external 'gdi32.dll' name 'LineTo'; 這段聲明表示函數LineTo 的代碼同名保存在GDI32.DLL 動態鏈接庫中(最重要的Windows 系統庫之一)。實際應用時,外部聲明中的函數名與DLL中的函數名可以不同。 與WindowsAPI對應的聲明: BOOL EnumWindows( WNDENUMPROC lpEnumFunc,LPARAM lParam ); =》 function EnumWindows (lpEnumFunc: TFNWndEnumProc,lParam: LPARAM): BOOL; stdcall;
回調函數 BOOL CALLBACK EnumWindowsProc ( HWND hwnd, // handle of parent window LPARAM lParam // application-defined value ); => type EnumWindowsProc = function (Hwnd: THandle, Param: Pointer): Boolean; stdcall; 直接使用方法
Variant變量沒有類型 運算效率低
單元Unit unit unitName; interface // other units we need to refer to uses A, B, C; // exported type definition type newType = TypeDefinition; // exported constants const Zero = 0; // global variables var Total: Integer; // list of exported functions and procedures procedure MyProc; implementation uses D, E; // hidden global variable var PartialTotal: Integer; // all the exported functions must be coded procedure MyProc; begin // ... code of procedure MyProc end; initialization // optional initialization part finalization // optional clean-up code end.
單元用作命名空間
Delphi 應用程序源代碼文件可分成兩類,它們是一個或多個單元文件及一個程序文件,單元文件可以看成是次一級的文件,它被應用程序的主體——程序文件——引用。 program Project1;
uses Forms, Unit1 in Unit1.PAS?{Form1DateForm};
begin Application.Initialize; Application.CreateForm (TForm1, Form1); Application.Run; end.
面向對象 type TData = class A:Integer; B:Char; function F(V:Integer):Boolean; var C:Integer; begin A :=1; C :=A; end; procedure P(V:Char); begin B:='b'; end; end.
public private protected
self變量 publish
繼承 type MyClass = class(BaseClass); 多繼承 type MyClass = class();
構造器 constructor 代替procedure;自動分配內存 對象方法重載 同函數
多態 type BaseClass = class public function F:String;virtual; end;
type MyClass = class(BaseClass) public function F:String;override; end;
類型兼容性 向上兼容 之類兼容父類
類型信息 if MyClass is BaseClass then
繼承 inherited; 使用父輩被繼承的方法
類引用 MyClassClass = class of MyClass;
Create() Free() type裏面類聲明 implementation 實現
initialization //初始化段 Init(); finalization //反初始化段 UnInit();
constructor Create(); destructor Destroy();
procedure fly(); virtual; abstract;抽象
接口 interface type Icanfly = interface ['{10000000-0000-0000-0000-0000000000}'] function Fly:String; end;
type TAirplane = class(TomerfaceObject,IcanFly) function Fly:String virtual; end;
Flyer1 :=TAirplance.Create as Icanfly;
QueryInterface
異常 錯誤處理 raise Exceptoion.Create('a error');
try A(); except ... finally ... end;
繼承異常類 p54
VCL TObject TPresistent TComponent TControl可視化控件 Windowsed Controls 基於window的控件(TWinControl) Nonwindowsed Controls( TGraphicsControl) Nonvisual Components非可視控件
可視化組件 Window-based controls 有GDI句柄,Windows管理,擁有Windows對象的屬性 Graphical controls 無GDI句柄,自行繪圖管理,如大量的Laber(比較Static控件) speedbutton,節省GDI資源,但響應事件麻煩。 novisiual compoenets 一些管理組件
可融入編輯環境 published private/public procedure p:string; property month:Integer read FMonth write SetMonth; 讀寫爲變量或者過程。 read GetMonth write SetMonth;
function GetMonth:Integer;讀 procedure SetMonth(const value:Integer);寫
只讀屬性的話就不帶read 或 write
對象拷貝 Assigned()
事件 private FOnClick : TNotifyEvent; …… published: property OnClick : TNotifyEvent Read FOnClick Write FOnClick; ……
|