TPopupMenu 與 PDF 檢視器關聯,讓使用者在頁面上按右鍵時出現內容選單property PopupMenu: TPopupMenu; // published, inherited from TControl
設定 PopupMenu 可將檢視器連接到放置在同一表單上的 TPopupMenu 元件。每當使用者在檢視器內按右鍵(或按下鍵盤的功能表按鍵)時,VCL/LCL 會自動在游標位置顯示功能表 — 應用程式不需要自行處理 WM_CONTEXTMENU
典型的 PDF 閱讀器內容功能表會提供 Copy Selected Text、Zoom In、Zoom Out、Fit Width、Rotate、Go to Page…、Find… 與 Save As… 等項目。這些功能表項目通常會讀寫 CurrentCharIndex、Zoom、FitMode、Rotation 與 PageNumber 等公開屬性,以對文件執行動作
由於彈出視窗是透過 OnContextPopup 機制顯示,您可以藉由處理 OnContextPopup 並設定 Handled := True 來動態抑制或取代它 — 當右鍵點擊發生在需要專屬功能表的表單欄位或註解上時特別有用
TPopupMenu 實體的連結會以元件參照的形式儲存在表單檔案中AllowUserTextSelection 為 False,它仍會彈出TPopupMenu.OnPopup 並檢查 CurrentCharIndex 或 Pdf.Active 等檢視器狀態
// Attach a context menu offering Copy / Fit Width / Go to Page
procedure TForm1.FormCreate(Sender: TObject);
begin
PdfView1.PopupMenu := pmReader;
end;
procedure TForm1.miFitWidthClick(Sender: TObject);
begin
PdfView1.FitMode := pfmFitToWidth;
end;
procedure TForm1.miGoToPageClick(Sender: TObject);
var S: string;
begin
S := IntToStr(PdfView1.PageNumber);
if InputQuery('Go to page', 'Page number:', S) then
PdfView1.PageNumber := StrToIntDef(S, PdfView1.PageNumber);
end;