許多程式的「關於」對話框包含可點擊的超連結標籤:點擊啟動瀏覽器開啟網址、啟動郵件用戶端寫信、遠端登入、開啟新聞群組等。這些功能僅需 ShellAPI 單元的 ShellExecute 一行代碼即可實現,並非僅能執行 EXE 檔案。
function ShellExecute(
hwnd: HWND; // 父視窗控制代碼
lpOperation: PChar; // 動作:'open'、'print'、'explore' 等
lpFile: PChar; // 目標:URL、mailto:、檔案路徑、目錄
lpParameters: PChar; // 參數(針對可執行檔)
lpDirectory: PChar; // 預設工作目錄
nShowCmd: Integer // 顯示狀態:SW_SHOWNORMAL 等
): HINST; stdcall;
常用範例:
ShellExecute(Handle, 'open', 'https://www.example.com', nil, nil, SW_SHOWNORMAL); — 開啟網頁ShellExecute(Handle, 'open', 'mailto:user@example.com', nil, nil, SW_SHOWNORMAL); — 撰寫郵件ShellExecute(Handle, 'open', 'ftp://ftp.example.com', nil, nil, SW_SHOWNORMAL); — 開啟 FTPShellExecute(Handle, 'open', 'telnet://host', nil, nil, SW_SHOWNORMAL); — 遠端登入ShellExecute(Handle, 'open', 'news:comp.lang.pascal', nil, nil, SW_SHOWNORMAL); — 開啟新聞群組ShellExecute(Handle, 'open', 'C:\Windows', nil, nil, SW_SHOWNORMAL); — 開啟資料夾ShellExecute(Handle, 'print', 'Readme.txt', nil, nil, SW_SHOWNORMAL); — 列印文件將 TLabel 封裝為 THyperLinkLabel:
URL: String、HoverColor、VisitedColorOnClick 呼叫 ShellExecute、滑鼠進入/離開變更游標與顏色Font.Style = [fsUnderline]、Cursor = crHandPoint這樣即可在任何表單輕鬆放置功能完整的超連結標籤,提升應用程式專業度與使用者體驗。