Delphi helps

zhaozj2021-02-16  46

1. If you want your program to handle exceptions correctly, please refer to the sysutils.pas unit, otherwise even if the program uses TRY. . . Except. . . You cannot capture an exception correctly.

2. A way to define a constant string

ResourceString

AA = 'aaaa';

Raise Exception.createres (@AA);

3. Initialization of string constant array

Const constarray: array [0..2] of string = ('first', 'second', 'third');

4. Structural initialization

TYPE TSTRUCTINIT = Record

A1: Integer;

A2: array [0..2] of integer;

END;

Const m_structinit: tstructinit = (A1: 0; A2: (0, 1, 2));

5. The length of the multi-dimensional array

Var Array2: Array of Array of Integer;

SETLENGTH (Array2, 2, 2);

6. The space opened with Create and New exists in the heap, and cannot be automatically released. It is recommended to use freeandnil release, parameters, and local variables existing in the stack, automatically release.

7. SIZEOF is not suitable for objects, returns to always 4; the fixed type can be returned correctly.

8. Create (NIL) requires manual release, Creat (Self) will be released as the owner's release.

9. Dynamic changes the value of the defined constant

Procedure ChangeConst (const const; var value; size: integer);

Begin

Move ((@ value) ^, (@ constant) ^, size);

END;

10. Cyclic use of DOWNTO when deleting operations, will avoid errors.

11. Chinese characters ASCII code> 128, can use it to judge whether it is Chinese characters

12. DLL write, you need to use the ShareMem unit to reference BorlandMM.DLL memory management.

13. PostMessage only places the message in the message queue and needs to wait in line.

SendMessage Winding the message queue directly to the window process, waiting to return the message processing return value.

14. Mouse move into the transfer message: cm_mouseeenter, cm_mouselave

15. Shutdown message WM_QUERYENDSESSION

16. You can create a floating form using ThintWindow and class method ActivateHint.

17. Tune the file properties dialog

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. Change the system time

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. Copy the folder XCOPY

Procedure Xcopy (SourceDir, DestinationDir: String);

VAR

Search: tsearchrec;

Re: 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 (SECH);

END;

FindClose (Search);

END;

20. Draw a transparent bitmap

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: = CMSRCCPY;

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. Get CPU speed

Function getcpuspeed: extended;

VAR

T, 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;

Temporarily just wrote so much, will gradually update the addition

转载请注明原文地址:https://www.9cbs.com/read-24951.html

New Post(0)