1. 如果想你的程序能夠正確處理異常情況的話(huà),請引用SysUtils.pas單元,否則即使程序使用了try...except...也不能正確捕獲異常。
2. 定義常量字符串的一種方式
resourcestring
aa=‘a(chǎn)aaa‘;
raise Exception.CreateRes(@aa);
3. 字符串常量數組的初始化
const constarray:array [0..2] of string=(‘first’,’second’,’third’);
4. 結構體初始化
type Tstructinit=record
A1:integer;
A2:array [0..2] of integer;
End;
Const m_structinit:Tstructinit=(A1:0;A2:(0,1,2));
5. 多維數組的長(cháng)度
var array2:array of array of integer;
setlength(array2,2,2);
6. 使用Create和New開(kāi)辟的空間都存在于堆中,不能自動(dòng)釋放,建議使用FreeAndNil釋放, 參數以及局部變量存在于棧中,自動(dòng)釋放。
7. SizeOf不適合于對象,返回的總是4;對于固定類(lèi)型可以正確返回.
8. Create(nil)需要手工釋放,Creat(self)會(huì )隨著(zhù)擁有者的釋放而釋放.
9. 動(dòng)態(tài)改變已定義常量的值
procedure ChangeConst(const Const;var Value;Size:Integer);
begin
Move((@Value)^,(@Constant)^,Size);
End;
10. 進(jìn)行刪除操作的時(shí)候循環(huán)使用DownTo,會(huì )避免錯誤.
11. 漢字的Ascii碼>128,可以用它來(lái)判別是否為漢字
12. dll編寫(xiě)中,需要使用Sharemem單元來(lái)引用BORLANDMM.DLL內存管理.
13. PostMessage只將消息放到消息隊列中,需要排隊等待處理。
SendMessage繞過(guò)消息隊列直接發(fā)送到窗口過(guò)程,等到消息處理返回值才返回.
14. 鼠標移入移出消息:CM_MOUSEENTER,CM_MOUSELEAVE
15. 1. 產(chǎn)生隨機密碼(應該比較有用):office" /> function CreatePass:String; const MAX_LEN=10; var i: integer; s: string; begin Randomize; s:=‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘+‘a(chǎn)bcdefghijklmnopqrstuvwxyz‘+ ‘0123456789‘; Result := ‘; for i := 0 to MAX_LEN-1 do begin Result := Result + s[Random(Length(s)-1)+1]; end; end; 2. 十進(jìn)制數字轉換成羅馬數字 function DecToRoman(iDecimal: longint): string; const aRomans: array[1..13] of string = (‘I‘, ‘IV‘, ‘V‘, ‘IX‘, ‘X‘, ‘XL‘, ‘L‘, ‘XC‘, ‘C‘, ‘CD‘, ‘D‘, ‘CM‘, ‘M‘); aArabics: array[1..13] of integer = (1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000); var i: integer; begin result := ‘; for i := 13 downto 1 do while (iDecimal >= aArabics[i]) do begin iDecimal := iDecimal - aArabics[i]; result := result + aRomans[i]; end; end; procedure TForm1.Button1Click(Sender: TObject); begin showmessage(DecToRoman(5)); end; 3. 格式化整數顯示 使用FormatFloat函數可以解決你很多問(wèn)題。例如把1200000格式化為1,200,000輸出 procedure TForm1.Button1Click(Sender: TObject); var i:integer; s:string; begin i := 1200000; s := FormatFloat(‘#,0‘, i); showmessage(s); end; 4. 判斷串口是否收到了數據可以使用ClearCommError函數,TcomStat結構體中cbInQue,cbOutQue可以幫助實(shí)現判斷。 5. RGB顏色轉換為TColor類(lèi) function RGBToColor(R,G,B:Byte): TColor; begin Result:=B Shl 16 Or G Shl 8 Or R; end; 6. 把TColor轉換為RGB值 procedure TForm1.Button1Click(Sender: TObject); var Color: TColor; R, G, B: Integer; begin Color := clBlack; R := Color and $FF; G := (Color and $FF00) shr 8; B := (Color and $FF0000) shr 16; showmessage(inttostr(R)); showmessage(inttostr(G)); showmessage(inttostr(B)); end; 7. 瀏覽計算機對話(huà)框 uses ShlObj; function BrowseForComputer(const winhandle : THANDLE; const title : string) : string; var BrowseInfo: TBrowseInfo; IDRoot: PItemIDList; Path: array[0..MAX_PATH] of Char; begin SHGetSpecialFolderLocation(winHandle, CSIDL_NETWORK, IDRoot); ZeroMemory(@BrowseInfo, SizeOf(TBrowseInfo)); ZeroMemory(@path, MAX_PATH); BrowseInfo.hwndOwner := winhandle; BrowseInfo.pidlRoot := IDRoot; BrowseInfo.lpszTitle := PChar(title); BrowseInfo.pszDisplayName := @path; BrowseInfo.ulFlags:=BIF_BROWSEFORCOMPUTER; SHBrowseForFolder(BrowseInfo); end;
聯(lián)系客服