在写程序的时候,有时我们为了省力,或者为了别的目的,我们常常想借用系统的对话框,那么,如何才能调用系统对话框呢?一位朋友是VB中是这样调用“打开方式”对话框的:
winexec(PChar('rundll32shell32,OpenAs_RunDLL'+FilePath),SW_SHOWDEFAULT);
这句代码是运行rundll32.exe,让它调用shell32.dll中的资源来实现的。方法可行,但是有许多像我一样的菜鸟并不明白如何调用shell32.dll中的宝贵资源,我们应该如何去做呢?
下面说说我是如何调用的:
一、调用系统“About”对话框:
首先在uses中加入SHellApi,
然后写下如下代码:
PRocedureTForm1.Button1Click(Sender:TObject);
var
shellapp:variant;
begin
ShellAboutW(0,'Timerv1.03','kedy版权所有',1);
end;
其他步骤我不详述。运行后点击button就会弹出标准的WINDOWS关于对话框。对话框标题为"关于Timerv1.03"。大家可以看到,在程序中我使用了ShellAboutW这个函数。在MSDN2003中这个函数是这样描述的:
ShellAboutFunction
DisplaysaShellAboutdialogbox.
Syntax
intShellAbout(HWNDhWnd,
LPCTSTRszApp,
LPCTSTRszOtherStuff,
HICONhIcon
);
Parameters
hWnd
[in]Windowhandletoaparentwindow.ThisparametercanbeNULL.
szApp
[in]Pointertoanull-terminatedstringcontainingtextthatwillbedisplayedinthe
titlebaroftheShellAboutdialogboxandonthefirstlineofthedialogboxafterthe
text"Microsoft".Ifthetextcontainsaseparator(#)dividingitintotwoparts,the
functiondisplaysthefirstpartinthetitlebarandthesecondpartonthefirstline
afterthetext"Microsoft".
szOtherStuff
[in]Pointertoanull-terminatedstringcontainingtextthatwillbedisplayedinthe
dialogboxaftertheversionandcopyrightinformation.
hIcon
[in]Iconthatthefunctiondisplaysinthedialogbox.IfthisparameterisNULL,the
functiondisplaystheMicrosoft®Windows®orMicrosoftWindowsNT®icon.
什么意思我想不用我来翻译了吧,这些东西自己去看最好。
二、调用关机对话框
我们只要把begin部分代码改为
begin
shellapp:=CreateOleObject('Shell.application');
shellapp.ShutDownWindows;
end;
其他部分不变。运行点击button我们就可以看到标准的系统关机对话框了。
其实这还是调用的WindowsAPI函数shutdownwindows.
这个部分使用的是windows的shellapplication的method中的两个函数。method其他的函数还有:
BrowseForFolder、CascadeWindows、ControlPanelItem、EjectPC、Explore、FileRun、FindComputer、FindFiles、Help、MinimizeAll、NameSpace、Open、RefreshMenu、SetTime、TileHorizontally、TileVertically、TrayProperties、UndoMinimizeALL。我也只是学会了使用其中的几个函数。详细情况请大家查看MSDN中有关shellobject的内容。
我最想说的是,学windows下的程序一定要用MSDN。这个库里的资源让我实在惊叹不已,大家可以看一看,我想你也会这样认为的。