首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Host+DLL 模式下快捷鍵衝突解決方案

     編輯:PHP 相關知識

Delphi/C++Builder 插件系統快捷鍵動態綁定技巧

在 Host+DLL 插件架構中,多個 DLL 模組共享主程式的快捷鍵表會導致衝突:只有最先載入的模組能響應熱鍵。解決方案是在 TabSheet 的 OnHide/OnShow 事件中動態註冊/註銷快捷鍵。

核心程式碼

// 基類 TBaseForm 定義快捷鍵動作
procedure TBaseForm.bidingshortcut; // 綁定快捷鍵
begin
  al_new.ShortCut      := TextToShortCut('F5');
  al_edit.ShortCut     := TextToShortCut('F6');
  al_delete.ShortCut   := TextToShortCut('F7');
  al_refdata.ShortCut  := TextToShortCut('F8');
  al_query.ShortCut    := TextToShortCut('F3');
  al_report.ShortCut   := TextToShortCut('F2');
  al_export.ShortCut   := TextToShortCut('F10');
  al_import.ShortCut   := TextToShortCut('F4');
  al_gridset.ShortCut  := TextToShortCut('F9');
end;

procedure TBaseForm.unbidingshortcut; // 解除綁定
begin
  al_new.ShortCut      := TextToShortCut('none');
  al_edit.ShortCut     := TextToShortCut('none');
  al_delete.ShortCut   := TextToShortCut('none');
  al_refdata.ShortCut  := TextToShortCut('none');
  al_query.ShortCut    := TextToShortCut('none');
  al_report.ShortCut   := TextToShortCut('none');
  al_export.ShortCut   := TextToShortCut('none');
  al_import.ShortCut   := TextToShortCut('none');
  al_gridset.ShortCut  := TextToShortCut('none');
end;

Host 程式頁籤切換事件處理

// TabSheet OnShow 事件
procedure TMainForm.TabSheetShow(Sender: TObject);
var
  BaseForm: TBaseForm;
begin
  BaseForm := TBaseForm(TTabSheet(Sender).Tag); // Tag 儲存表單實例
  if Assigned(BaseForm) then
    BaseForm.bidingshortcut;
end;

// TabSheet OnHide 事件
procedure TMainForm.TabSheetHide(Sender: TObject);
var
  BaseForm: TBaseForm;
begin
  BaseForm := TBaseForm(TTabSheet(Sender).Tag);
  if Assigned(BaseForm) then
    BaseForm.unbidingshortcut;
end;

原理說明

  • VCL 的 TAction.ShortCut 屬性為全域熱鍵,同一熱鍵只能由最後註冊的 Action 擁有。
  • 頁籤切換時,先將非當前頁的 Action 熱鍵設為 none,再為當前頁註冊熱鍵,確保唯一性。
  • 利用 TabSheet.Tag 儲存對應的 Form 實例,透過基類轉型呼叫統一方法。

適用場景

  • MDI 子視窗或頁籤式多文檔介面。
  • 插件化架構,各模組獨立定義快捷操作。
  • 需支援 F1-F12、Ctrl/Shift 組合鍵的專業桌面應用。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved