1. 如果想你的程式能夠正確處理異常情況的話,請引用SysUtils.pas單元,否則即使程式使用了try。 。 。 except。 。 。也不能正確捕獲異常。 2. 定義常數字串的一種方式resourcestring aa='aaaa';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. 多維數組的長度var array2:array of array of integer;setlength(array2,2,2);6 . 使用Create和New開闢的空間都存在於堆中,不能自動釋放,建議使用FreeAndNil釋放,參數以及局部變數存在於堆疊中,自動釋放。 7. SizeOf不適合對象,返回的總是4;對於固定類型可以正確返回.8. Create(nil)需要手工釋放,Creat(self)會隨著擁有者的釋放而釋放.9. 動態改變已定義常數的值PRocedure ChangeConst(const Const;var Value;Size:Integer);begin Move((@Value)^,(@Constant)^,Size);End;10.進行刪除操作的時候循環使用DownTo,會避免錯誤.11. 漢字的Ascii碼>128,可以用它來判別是否為漢字12. dll編寫中,需要使用Sharemem單元來引用BORLANDMM.DLL內存管理.13. PostMessage只將訊息放到訊息佇列中,需要排隊等待處理。 SendMessage繞過訊息佇列直接傳送到視窗過程,等到訊息處理回傳值才回傳.14.滑鼠移入移出訊息:CM_MOUSEENTER,CM_MOUSELEAVE15. 關機訊息WM_QUERYENDsession16. 可以利用ThintWindow和類別的方法ActivateHint來建立浮動視窗.17.調出檔案屬性對話框uses ShellAPI;function ShowFileProperties(FileName: String; Wnd: HWND):Boolean;var sfi: TSHELLEXECUTEINFO;begin with sfi do begin cbSize := SizeOf(sfi); lpFile := PAnsiChar(FileName); Wnd := Wnd; fMask. SEE_MASK_FLAG_NO_UI; lpVerb := PAnsiChar('properties'); lpIDList := nil; lpDirectory := nil; nShow := 0; hInstApp := 0; lpParameters := nil; 0; hProcess := 0; lpClass := nil; end; Result := ShellExecuteEX(@sfi);end;procedure TForm1.Button1Click(Sender: TObject);begin ShowFileProperties('c:/AA.txt', Handle);end;18. 更改系統時間uses Windows,Dialogs,Forms;var MyTime:TsystemTime;begin FillChar(MyTime,sizeof(MyTime),#0); MyTime.wYear:=2003; MyTime.wMonth:=06; MyTime.wDay:=01; If not SetSystem(MyTime) thenShowmessage('Failure'); End; 19 . 複製資料夾Xcopy. procedure Xcopy(SourceDir,DestinationDir:String);
var
Search : TSearchRec;
Rec : Word;
Begin
SourceDir := SourceDir + '/';
Rec := FindFirst(SourceDir + '*.*', faAnyFile, Search);
While Rec = 0 Do
Begin
If Search.Name[1] <> '.' Then
Begin
If (Search.Attr And faDirectory) = faDirectory Then
Begin
Windows.CreateDirectory(PChar(DestinationDir + '/' + Search.Name), nil);
FileSetAttr(DestinationDir + '/' + Search.Name, FileGetAttr(SourceDir + '/' + Search.Name));
X_Copy(SourceDir + '/' + Search.Name, DestinationDir + '/' + Search.Name);
end
Else
Begin
CopyFile(PChar(SourceDir + '/' + Search.Name),PChar(DestinationDir + '/' + Search.Name), True);
FileSetAttr(DestinationDir + '/' + Search.Name, FileGetAttr(SourceDir + '/' + Search.Name));
application.ProcessMessages;
end;
end;
Rec := FindNext(Search);
end;
FindClose(Search);
end;20. 繪製透明位圖procedure DrawTrans(DestCanvas: TCanvas; X,Y: smallint; SrcBitmap: TBitmap; AColor, BackColor: TColor);var ANDBitmap, ORBitmap: TBitmap; CM: TCopyMode; Src: TRbegingin, ORBitmap: TBitmap; CM: TCopyMode; Src: TRdbegingin, Nrc); ; ORBitmap:= NIL; try ANDBitmap:= TBitmap.Create; ORBitmap:= TBitmap.Create; Src := Bounds(0,0, SrcBitmap.Width, SrcBitmap.Height); with ORBitmap do begin Width:= SrcBitmap.Widk Heightmap. Canvas.Brush.Color := clBlack; Canvas.CopyMode := cmSrcCopy; Canvas.BrushCopy(Src, SrcBitmap, Src, AColor); end; with ANDBitmap do begin Width:= SrcBitmap.Width; Height:= SrcBitmap.Height; Canvas.Brushor 三; .CopyMode := cmSrcInvert; Canvas.BrushCopy(Src, SrcBitmap, Src, AColor); end; with DestCanvas do begin CM := CopyMode; CopyMode := cmSrcAnd; Draw(X,Y, ANDBitmap); CopyMode := cmSrcPaint; Draw(X,Y, ANDBitmap); CopyMode := cmSrcPaint; Draw(XPaint; ); CopyMode := CM; end; finally ANDBitmap.Free; ORBitmap.Free; end;end;procedure TForm1.Button4Click(Sender: TObject);begin DrawTrans(Image1.Canvas, 0,0, Image2.Picture.Bitmap, clBlack, clSilver);end;21. 取得CPU速度function GetCpuSpeed: Extended;var t, mhi, mlo, nhi, nlo: dword; shr32 : comp;begin shr32 := 65536; shr32 := shr32 * 65536; t := GetTickCount; while t = GetTickCount do ; asm DB 0FH,031H // rdtsc mov mhi,ed (povs mov mhit (xscv mov mhi), fal ov mov mhi, (xssc mov mhi)); t + 1000) do ; asm DB 0FH,031H // rdtsc mov nhi,edx mov nlo,eax end; Result := ((nhi * shr32 + nlo) - (mhi * shr32 + mlo)) / 1E6;end;procedure TForm1.Button4Click(Sender: TObject);begin label1.Caption := FloatToStr(GetCpuSpeed) + 'mhz';end; 暫時只是寫了這麼多,以後會逐步更新添加