Delphi one-sentence help

Source: Internet
Author: User

1. If you want your Program If you can correctly handle exceptions, reference the sysutils. Pas unit. Otherwise, even if the program uses try... Else t... And cannot catch exceptions correctly.
2. A way to define a constant string
Resourcestring
AA = 'aaa ';
Raise exception. createres (@ aa );
3. String constant array Initialization
Const constarray: array [0 .. 2] of string = ('first', 'second', 'third ');
4. struct Initialization
Type tstructinit = record
A1: integer;
A2: array [0 .. 2] of integer;
End;
Const m_structinit: tstructinit = (A1: 0; a2 :( 0, 1, 2 ));
5. Length of multi-dimensional array
VaR array2: array of integer;
Setlength (array2, 2, 2 );
6. The space opened using create and new exists in the heap and cannot be automatically released. We recommend that you use freeandnil to release the space. Parameters and local variables exist in the stack and be automatically released.
7. sizeof is not suitable for objects, and always 4 is returned. It can be returned correctly for fixed types.
8. Create (NiL) needs to be manually released, and creat (Self) will be released with the release of the owner.
9. dynamically change the value of a defined constant
Procedure changeconst (const; VaR value; Size: integer );
Begin
Move (@ value) ^, (@ constant) ^, size );
End;
10. Use downto repeatedly during the delete operation to avoid errors.
11. ASCII code of Chinese characters> 128, which can be used to determine whether it is a Chinese character
12. When writing DLL, use the sharemem unit to reference borlandmm. dll Memory Management.
13. postmessage only stores messages in the message queue and needs to be queued for processing.
Sendmessage is directly sent to the window without passing through the message queue. It is returned only when the message is returned.
14. Move the mouse in and out of the message: cm_mouseenter, cm_mouseleave
15. shutdown message wm_queryendsession
16. You can use thintwindow and activatehint to create a floating form.
17. File Properties dialog box
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 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 ('failed ');
End;
19. Copy the folder 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. 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: = 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. Obtain CPU speed
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, 031 H // rdtsc
MoV MHI, EDX
MoV MLO, eax
End;
While gettickcount <(t + 1000) do;
ASM
DB 0fh, 031 H // 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.