首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 實現超連結標籤:ShellExecute 一鍵啟動瀏覽器、郵件與遠端登入

     編輯:Delphi

ShellExecute 多功能啟動器

許多程式的「關於」對話框包含可點擊的超連結標籤:點擊啟動瀏覽器開啟網址、啟動郵件用戶端寫信、遠端登入、開啟新聞群組等。這些功能僅需 ShellAPI 單元的 ShellExecute 一行代碼即可實現,並非僅能執行 EXE 檔案。

ShellExecute 參數與常用操作

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); — 開啟 FTP
  • ShellExecute(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、VisitedColor
  • 事件:OnClick 呼叫 ShellExecute、滑鼠進入/離開變更游標與顏色
  • 樣式:Font.Style = [fsUnderline]、Cursor = crHandPoint

這樣即可在任何表單輕鬆放置功能完整的超連結標籤,提升應用程式專業度與使用者體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved