?? 用delphi創建internet快捷方式 (2000年11月22日).txt
字號:
用Delphi創建Internet快捷方式 (2000年11月22日)
本站更新 分類: 作者:srw 推薦: 閱讀次數:458
(http://www.codesky.net)
--------------------------------------------------------------------------------
在Windows中,為了方便文件或者文件夾的訪問,我們常常為某些文件或者文件夾建立快捷方式
(shortcut)。同樣,在Internet Explorer中,我們也可以為自己喜愛的網址建立Internet快捷方式
(Internet Shortcut)。只要點擊快捷方式,Internet Explorer就會啟動,并聯接到相應的網站。事實
上,除了Internet Explorer可以創建Internet快捷方式之外,在我們的應用程序中也同樣可以實現此功
能。在本文中,我將要向讀者介紹如何在Delphi程序中為網址建立Internet快捷方式。
Internet快捷方式的格式
為了分析Internet快捷方式的文件格式,我們可以先用Internet Explorer為某個網址建立一個快捷方式,
然后在DOS窗口中用DIR命令找到Internet快捷方式所對應的文件,并且用edit查看文件的內容。不難發現,
Internet快捷方式的文件格式與INI文件的格式是一樣的,只不過它的擴展名必須是 .URL罷了。 Internet
快捷方式的文件格式如下:
[DEFAULT]
BASEURL=
[InternetShortcut]
URL=
WorkingDirectory=
ShowCommand=
IconIndex=
IconFile=
Modified=
HotKey=
其中BASEURL、URL和WorkingDirectory這3項的含義是不言而明的。ShowCommand規定Internet Explorer
啟動后窗口的初始狀態:7表示最小化,3表示最大化;如果沒有ShowCommand這一項的話則表示正常大小。
IconFile和IconIndex用來為Internet快捷方式指定圖標;如果你不想指定圖標,Windows會使用缺省的
Internet快捷方式圖標。HotKey指定一個整數值;HotKey的值及其含義如下:
833 - Ctrl + Shift + A
834 - Ctrl + Shift + B
835 - Ctrl + Shift + C
836 - Ctrl + Shift + D
837 - Ctrl + Shift + E
838 - Ctrl + Shift + F
…
1601 - Ctrl + Alt + A
1602 - Ctrl + Alt + B
1603 - Ctrl + Alt + C
1604 - Ctrl + Alt + D
1605 - Ctrl + Alt + E
1606 - Ctrl + Alt + F
...
一個簡單的Internet快捷方式只需要在
[InternetShortcut]節中包含URL項就可以了,例:
[InternetShortcut]
URL=http://www.yahoo.com
快捷方式的創建
接下來,我們來看一個非常簡單的Delphi例子。此程序將在Windows的桌面上建一個一個名為“計算機世
界”的快捷方式,它指向《計算機世界》的首頁;快捷方式的圖標使用Windows 98/95中System目錄(或者
Windows NT中的System32目錄)下的shell32.dll動態聯接庫中的第41號圖標。
由于Internet快捷方式的文件格式與INI文件的是相同的,我們可以使用Delphi的TiniFile類來讀寫.URL文
件的內容。而要使快捷方式出現在桌面上,我們只要把.URL文件保存在Windows桌面所對應的那個目錄之下即
可。當前用戶的桌面目錄可以從注冊表中獲取,此信息保存在Windows注冊表HKEY_CURRENT_USER 根鍵下的
Software\Microsoft\Windows
\CurrentVersion\Explorer\Shell Folders主鍵的Desktop項中。
示例程序的單元文件的代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,7
StdCtrls, Registry, IniFiles;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
ini:TIniFile;
Reg:TRegistry;
DesktopPath, FileName, S:String;
Buf:array[0..max_path]of char;
begin
//獲取當前用戶Desktop文件夾的路徑
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_CURRENT_USER;
try
Reg.OpenKey('Software\Microsoft\Windows
\CurrentVersion\Explorer\Shell Folders',False);
if Reg.ValueExists('Desktop') then
DesktopPath:=Reg.ReadString('Desktop');
finally
Reg.Free;
end;
if (DesktopPath< >'')and(DesktopPath
[Length(DesktopPath)]< >'\')
then DesktopPath:=DesktopPath+'\';
//將Buf清零
FillChar(Buf,SizeOf(Buf),0);
//獲取Win98/95中的System
文件夾或者NT中的System32文件夾的路徑
GetSystemDirectory(Buf,SizeOf(Buf));
S:=Buf;
if (S< >'')and(S[Length(S)]< >'\') then S:=S+'\';
S:=S+'shell32.dll';
//Internet快捷方式的文件名(擴展名必須是URL)
FileName:=DesktopPath+'計算機世界.url';
ini:=TIniFile.Create(FileName);
//指定URL
ini.WriteString('InternetShortcut','URL',
'http://www.computerworld.com.cn');
//指定圖標文件
ini.WriteString('InternetShortcut','IconFile',S);
ini.WriteString('InternetShortcut','IconIndex','41');
ini.Free;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -