在 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;
// 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;
TAction.ShortCut 屬性為全域熱鍵,同一熱鍵只能由最後註冊的 Action 擁有。none,再為當前頁註冊熱鍵,確保唯一性。TabSheet.Tag 儲存對應的 Form 實例,透過基類轉型呼叫統一方法。