delphi一句话帮助 (转)[@more@]
1. 如果想你的能够正确处理异常情况的话,请引用SysUtils.pas单元,否则即使程序使用了try。。。except。。。也不能正确捕获异常。:namespace prefix = o ns = "urn:schemas--com::office" />
2. 定义常量字符串的一种方式
restring
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;
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);
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 ;
function ShowFileProperties(FileName: String; Wnd: HWND):Boolean;
i: 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;
Result := ShellExecuteEX(@sfi);
procedure TForm1.Button1Click(Sender: T);
begin
ShowFileProperties('c:AA.txt', Handle);
18. 更改时间
uses ,Dialogs,Forms;
var MyTime:TsystemTime;
begin
FillChar(MyTime,sizeof(MyTime),#0);
MyTime.wYear:=;
MyTime.wMonth:=06;
MyTime.wDay:=01;
If not SetSystem(MyTime) then
Showmessage(‘Failure’);
19. 复制文件夹Xcopy
. procedure Xcopy(SourceDir,DestinationDir:String);
Search : TSearchRec;
Rec : ;
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);
Begin
CopyFile(PChar(SourceDir + '' + Search.Name),PChar(DestinationDir + '' + Search.Name), True);
FileSetAttr(DestinationDir + '' + Search.Name, FileGetAttr(SourceDir + '' + Search.Name));
Application.ProcessMessages;
Rec := FindNext(Search);
FindClose(Search);
20. 绘制透明位图
procedure DrawTrans(DestCanvas: TCanvas; X,Y: smallint; SrcBitmap: TBitmap; AColor, BackColor: TColor);
var ANitmap, ORBitmap: TBitmap;
CM: TCopyMode;
Src: TRect;
begin
ANDBitmap:= NIL;
ORBitmap:= NIL;
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);
with ANDBitmap do begin
Width:= SrcBitmap.Width;
Height:= SrcBitmap.Height;
Canvas.Brush.Color := BackColor;
Canvas.CopyMode := cmSrcInvert;
Canvas.BrushCopy(Src, SrcBitmap, Src, AColor);
with DestCanvas do begin
CM := CopyMode;
CopyMode := cmSrcAnd;
Draw(X,Y, ANDBitmap);
CopyMode := cmSrcPaint;
Draw(X,Y, ORBitmap);
CopyMode := CM;
finally
ANDBitmap.Free;
ORBitmap.Free;
procedure TForm1.Button4Click(Sender: TObject);
begin
DrawTrans(Image1.Canvas, 0,0, Image2.Picture.Bitmap, clBlack, clSilver);
21. 获取速度
function GetCpuSpeed: Extended;
t, mhi, mlo, nhi, nlo: dword;
shr32 : comp;
begin
shr32 := 65536;
shr32 := shr32 * 65536;
t := GetTickCount;
while t = GetTickCount do ;
DB 0FH,031H // rdtsc
mov mhi,edx
mov mlo,eax
while GetTickCount < (t + 1000) do ;
DB 0FH,031H // rdtsc
mov nhi,edx
mov nlo,eax
Result := ((nhi * shr32 + nlo) - (mhi * shr32 + mlo)) / 1E6;
procedure TForm1.Button4Click(Sender: TObject);
begin
label1.Caption := FloatToStr(GetCpuSpeed) + 'mhz';
暂时只是写了这么多,以后会逐步添加
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员