Delphi programming highlights

Source: Internet
Author: User
◇ [Delphi] copying files by network neighbors
Uses shellapi;
Copyfile(pchar('newfile.txt '), pchar (' // computername/ction/targer.txt '), false );

◇ [Delphi] results in mouse dragging
Implemented through the mousemove event, dragover event, and enddrag event, such as the label on the panel:
VaR xpanel, ypanel, xlabel, ylabel: integer;
Panel mousemove event: xpanel: = x; ypanel: = y;
Dragover event of panel: xpanel: = x; ypanel: = y;
Label mousemove event: xlabel: = x; ylabel: = y;
Label enddrag event: Label. Left: = xpanel-xlabel; label. Top: = ypanel-ylabel;

◇ [Delphi] obtain the Windows directory
Uses shellapi;
VaR WINDIR: array [0 .. 255] of char;
Getwindowsdirectory (WINDIR, sizeof (WINDIR ));
Or read from the Registry, Location:
HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion
Systemroot key, for example, C: \ WINDOWS

◇ [Delphi] draw lines on form or other containers
VaR X, Y: array [0 .. 50] of integer;
Canvas. Pen. Color: = clred;
Canvas. Pen. Style: = psdash;
Form1.canvas. moveTo (trunc (X [I]), trunc (Y [I]);
Form1.canvas. lineto (trunc (X [J]), trunc (Y [J]);

◇ [Delphi] string list usage
VaR tips: tstringlist;
TIPS: = tstringlist. Create;
Tips.loadfromfile('filename.txt ');
Edit1.text: = tips [0];
Tips. Add ('Last line addition string ');
Tips. insert (1, 'insert string at No 2 line ');
Tips.savetofile('newfile.txt ');
Tips. Free;

◇ [Delphi] Simple clipboard operations

◇ [Delphi] file and directory operations
Chdir ('C: \ abcdir'); go to the directory
Mkdir ('dirname'); create a directory
Rmdir ('dirname'); Delete the Directory
Getcurrentdir; // get the name of the current directory, '\'
Getdir (0, S); // get the working directory name s: = 'C: \ abcdir ';
Deletfile('abc.txt '); // delete an object
Renamefile('old.txt', 'new.txt '); // rename the file
Extractfilename (filelistbox1.filename); // obtain the file name
Extractfileext (filelistbox1.filename); // obtain the file suffix

◇ [Delphi] processing file attributes
ATTR: = filegetattr (filelistbox1.filename );
If (ATTR and fareadonly) = fareadonly then... // read-only
If (ATTR and fasysfile) = fasysfile then... // System
If (ATTR and faarchive) = faarchive then... // Archive
If (ATTR and fahidden) = fahidden then... // hide

◇ [Delphi] executionProgramExternal files
Winexec // call the executable file
Winexec ('COMMAND. com/C copy *. * c: \ ', sw_normal );
Winexec ('start abc.txt ');
ShellExecute or shellexecuteex // start the File Association Program
Function executefile (const filename, Params, defaultdir: string; showcmd: integer): thandle;
Executefile ('C: \ ABC \ a.txt ', 'x. abc', 'c: \ ABC \', 0 );
Executefile ('HTTP: // ', '','', 0 );
Executefile ('mailto: ', '','', 0 );

◇ [Delphi] obtain the system running process name
VaR hcurrentwindow: hwnd; sztext: array [0 .. 254] of char;
Hcurrentwindow: = getwindow (handle, gw_hwndfrist );
While hcurrentwindow <> 0 do
If getwindowtext (hcurrnetwindow, @ sztext, 255)> 0 then listbox1.items. Add (strpas (@ sztext ));
Hcurrentwindow: = getwindow (hcurrentwindow, gw_hwndnext );

◇ [Delphi] Assembly embedding
ASM end;
You can modify eax, ECx, and EDX at will. You cannot modify ESI, EDI, ESP, EBP, and EBX.

◇ [Delphi] About type conversion functions
Floattostr // float to string
Floattostrf // formatted floating point to string
Inttohex // convert integer to hexadecimal
Fmtstr // output string in specified format
Formatdatetime ('yyyy-MM-DD, HH-mm-ss', date );

◇ [Delphi] string process and Function
Insert (OBJ, target, POS); // the position where the target string is inserted in the POS. If the insert result is greater than the maximum length of target, the extra characters will be truncated. If POS is not 255, an error occurs. For example, if St: = 'Brian ', insert (' OK ', st, 2) will change st to 'brokian '.
Delete (St, POs, num); // Delete the substring whose number is num (integer) characters from the POs (integer) position in the St string. For example, if St: = 'Brian ', delete (St, 3, 2) is changed to BRN.
STR (value, St); // converts a value (integer or real) into a string and places it in the St. For example, if a = 2.5e4, STR (A: 10, St) will set the value of St to '123 '.
Val (St, VAR, Code); // converts the string expression st to an integer or a real value, which is stored in var. St must be a string that represents a value and complies with the numerical constant rules. If no error is detected during conversion, the variable code is set to 0; otherwise, it is set to the first error character. For example, if St: = 25.4e3 and X is a real variable, Val (St, X, Code) will set X to 25400 and code to 0.
Copy (St. Pos. Num); // returns a substring that starts at the position pos (integer) in the St string and contains num (integer) characters. If the POs value is longer than the length of the St string, an empty string is returned. If the POs value is not greater than 255, a running error occurs. For example, if St: = 'Brian ', copy (st, 2, 2) returns 'ri '.
Concat (ST1, st2, st3 ......, STN); // connect the strings represented by all independent variables in the given order and return the connected values. If the result length is 255, a running error occurs. For example, if ST1: = 'Brian ', st2: = '', st3: = 'wilfred', Concat (ST1, st2, st3) returns 'Brian Wilfred '.
Length (ST); // returns the length of the string expression St. For example, if St: = 'Brian ', the return value of length (ST) is 5.
POs (OBJ, target); // returns the position of the string OBJ In the first appearance of the target string. If the target does not have a matched string, the return value of the POs function is 0. For example, if target: = 'Brian Wilfred ', the return value of pos ('wil', target) is 7, and the return value of pos ('hurbet ', target) is 0.

◇ [Delphi] About processing the Registry
Uses registry;
VaR REG: Tregistry;
Reg: = Tregistry. Create;
Reg. rootkey: = 'HKEY _ CURRENT_USER ';
Reg. openkey ('control Panel \ Desktop ', false );
Reg. writestring ('title wallpaper ', '0 ');
Reg. writestring ('wallpaper ', filelistbox1.filename );
Reg. closereg;
Reg. Free;

◇ [Delphi] keyboard constant name
F1--F12: (112) -- B (123)
A-Z :( 65) -- A (90)
0-9 :( 48) -- (57)
◇ [Delphi] preliminary judgment of the program's mother tongue
DOS prompt of Delphi software: This program must be run under win32.
DOS prompt of VC ++: This program cannot be run in DOS mode.

◇ [Delphi] cookie operations
Response. Cookies ("name"). Domain: = 'HTTP: // ';
With response. Cookies. Add do
Name: = 'username ';
Value: = 'username ';

◇ [Delphi] add to Document menu connection
Uses shellapi, shlobj;
Shaddtorecentdocs (shard_path, pchar (filepath); // Add a connection
Shaddtorecentdocs (shard_path, nil); // clear

◇ [Miscellaneous] intelligent ABC input dictionary for backup
WINDOWS \ SYSTEM \ tmmr. Rem

◇ [Delphi] Determining the mouse buttons
If getasynckeystate (vk_lbutton) <> 0 then... // left click
If getasynckeystate (vk_mbutton) <> 0 then... // middle
If getasynckeystate (vk_rbutton) <> 0 then... // right-click

◇ [Delphi] set the maximum display of the form
Onformcreate event
Self. Width: = screen. width;
Self. Height: = screen. height;

◇ [Delphi] press the button to receive messages
Oncreate event processing: application. onmessage: = myonmessage;
Procedure tform1.myonmessage (var msg: tmsg; var handle: Boolean );
If MSG. Message = 256 then... // any key
If MSG. Message = 112 then... // F1
If MSG. Message = 113 then... // F2

◇ [Miscellaneous] Hide Shared Folders
Shared effect: accessible, but invisible (in resource management and network neighbors)
Share Name: Direction $
Access: // computer/dirction/

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: 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.