As a new Windows programming language, Delphi is increasingly favored by programmers and enthusiasts because of its many excellent features. The following ten tips cover a wide range of topics and I hope they will be of benefit to Delphi enthusiasts.
1. Similar to vb. doevents function in.
You may find that there is no equivalent to vb. in Delphi. doevents function, so sometimes we will not be able to make Windows respond to multiple simultaneous events. In fact, Delphi's application object includes a similar method: PRocessMessage. You can call application. ProcessMessage to complete like vb. The same functionality as doevents in .
2. Call NetscapeNavigator in Delphi.
With the popularity of the Internet, have you ever thought about starting the Netscape browser in your Delphi program and displaying the homepage of the WWW address you specify? The following program can accomplish this function.
programNetscape;
usesDDEMan;
procedureGo??toURL(sURL: string);
var
dde:TDDEClientConv;
begin
dde:=TDDEClientConv. Create(nil);
withddedo
begin
//specifythelocationofnetscape. exe
ServiceApplication:=′c: s32program etscape. exe′;
//activatetheNetscapeNavigator
SetLink('Netscape', 'WWW-Activate');
RequestData(′0xFFFFFFFF′);
//gotothespecifiedURL
SetLink('Netscape', 'WWW-OpenURL');
RequestData(sURL+′,,0xFFFFFFFF,0x3,,,′);
CloseLink;
end;
dde. Free;
end;
begin
GotoURL(′http://www.yahoo.com/′);
end.
3. Formatted integer output.
Relatively large numbers will appear difficult to read when output. It is quite simple to display numbers with section marks in Delphi, as follows: xxxxx. caption: ΚFormatFloat(′#′, 524667500).
4. Get hints at compile time.
In Delphi2.0, when compiling, you can ask the compiler to tell you some hints, such as which variables are declared but never used. We know that you can control whether you want Delphi to do this through the options in the menu, but what if due to some special needs, you only need Delphi to prompt you in a specified code segment? Please refer to the following procedures.
{$HINTON}
procedureTform1. Button1Click(Sender: TObject);
var
X: integer;
begin
end;
{$HINTOFF}
5. Change Windows95 wallpaper.
You can change the wallpaper easily in Delphi, please refer to the following procedure.
procedureChangeIt;
var
Reg:TregIniFile;
begin
Reg:ΚTRegIniFile. Create('ControlPanel')
;
Reg. WriteString('desktop', 'Wallpaper',
'c: pwin95forest. bmp′);
Reg. WriteString('desktop', 'TileWallpaper
′,′1′);
Reg. Free;
SystemParametersInfo(SPI-SETDESKWALLPAPER, 0
, nil, SPIF-SENDWININICHANGE);
end;
6. Get the date the file was last used.
There is a new function in Win95, which is to get the last date of accessing the file. The famous CleanSweapforWin95 software relies on this function as one of the basis for judging whether a file is frequently accessed. In Delphi, we can achieve this function through the following program.
functionGetFileLastaccessTime(sFileName:string):TDate??Time;
var
ffd:TWin32FindData;
dft:DWord;
lft:TFileTime;
h: THandle;
begin
//getfileinformation
h:ΚWindows. FindFirstFile(PChar(sFileName), ffd);
if(INVALID―HANDLE―VALUEΙΛh)then
begin
//we'relookingforjustonefile,socloSEOur"find"
Windows. FindClose(h);
//converttheFILETIMEtolocalFILETIME
FileTimeToLocalFileTime(ffd.ftLastAccessTime, lft);
//convertFILETIMEtoDOStime
FileTimeToDosDateTime(lft,LongRec(dft).Hi,LongRec(dft).Lo);
//finally, convertDOStimetoTDateTimeforuseinDelphi'snativedate/timefunctions
Result: ΚFileDateToDateTime(dft);
end;
end;
GetFileLastAccessTime() will return the last access date of the file you specify in Delphi's TdateTime format.
7. Colorful labels.
We are no longer satisfied with the simple labels provided by Delphi. Can we have different fonts and colors in the labels to enrich our performance capabilities. The answer is yes, and there is no need for controls provided by third parties. We just need to cleverly use TRichEdit provided by Delphi itself. First remove the border of the TRichEdit control: RichEd??it1. BorderStyle: ΚbsNone; also set the read-only attribute to true: RichEd??it1. ReadOnly: ΚTrue; Then, you use software such as write to create text in RichText format, which can be displayed through the following statement:
RichEdit1. PlainText:ΚFalse;
RichEdit1. Lines. LoadFromFile(′c: est.rtf′);
8. How to prevent Win95 from displaying critical errors.
No matter how you debug your program repeatedly, after handing it over to the user, unexpected errors may occur. How to prevent Win95 from displaying a white window and telling your users that an embarrassing unexpected error has occurred? We can do this:
var
wOldError??Mode: Word;
begin
//tellwin??dowstoignorecriticalerrorsandsavecur??renterrormode
wOldError??Mode: ΚSetEr??rorMode (SEM-FAILCRITI??CALERRORS);
try
//codethatmightgenerateacriticalerrorgoeshere. . .
finally
//gobacktopreviouserrormode
SetErrorMode(wOldErrorMode);
end;
end;
Mainly use SetErrorMode() to complete this function.
9. Which object was just clicked with the mouse.
In Win95, the right button of the mouse plays a big role. However, due to historical reasons, the use of the right button is not effective enough even in Delphi. The following program can tell you how to know the name of the object you just right-clicked. . First create a popmenu, and then the following code can tell you the name of the object you just right-clicked: Popup??Menu1. PopupComponent. ClassName.
10. Check whether the CD-ROM or other disks have been changed.
The simplest way to check whether a CD-ROM or disk has been changed is to check its volume number. You can simply use the following function to return the disk's volume series number GetDiskVolSerialID ('E'). The function code is as follows:
functionGetDiskVolSerialID(cDriveName: char): DWord;
var
dwTemp1, dwTemp2:DWord;
begin
GetVolumeInformation(PChar(cDriveName+′:′), Nil, 0, ΝResult, dwTemp2, dwTemp2, Nil, 0);
end;