在寫程式的時候,有時我們為了省力,或是為了別的目的,我們常常想藉用系統的對話框,那麼,如何才能呼叫系統對話框呢?一位朋友是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、UndoMinizeALL。我也只是學會了使用其中的幾個函數。詳細情況請大家查看MSDN中有關shellobject的內容。
我最想說的是,學windows下的程式一定要用MSDN。這個庫裡的資源讓我實在驚嘆不已,大家可以看一看,我想你也會這麼認為的。