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_MOUSELEAVE
15. 關機消息WM_QUERYENDSESSION
16. 可以利用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_NOCLOSEPROCESS or SEE_MASK_INVOKEIDLIST or SEE_MASK_FLAG_NO_UI;
lpVerb := PAnsiChar('propertIEs');
lpIDList := nil;
lpDirectory := nil;
nShow := 0;
hInstApp := 0;
lpParameters := nil;
dwHotKey := 0;
hIcon := 0;
hkeyClass := 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) then
Showmessage(‘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: TRect;
begin
ANDBitmap:= NIL;
ORBitmap:= NIL;
try
ANDBitmap:= TBitmap.Create;
ORBitmap:= TBitmap.Create;
Src := Bounds(0,0, SrcBitmap.Width, SrcBitmap.Height);
with ORBitmap do begin
Width:= SrcBitmap.Width;
Height:= SrcBitmap.Height;
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.Brush.Color := BackColor;
Canvas.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, ORBitmap);
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,edx
mov mlo,eax
end;
while GetTickCount < (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;