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_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;
新闻热点
疑难解答
图片精选