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

玩轉 C++Builder IDE:使用 ToolsAPI 客製化選單與本地化介面

     編輯:VC 教學

前言

大多數 C++Builder 開發者一輩子不會接觸 ToolsAPI,但若你想擴充 IDE 功能(如自動本地化選單、加入自訂專案範本、整合外部工具),掌握 ToolsAPI 是關鍵。本文展示一個 BPL 套件專案,示範如何:

  • 列舉並顯示主選單所有項目(含隱藏項)
  • 將選單標題匯出至 INI 檔
  • 從中文 INI 讀取並套用本地化文字
  • 在 IDE 啟動/卸載時自動執行

專案結構

  • 專案類型:Runtime Package (BPL)
  • 需引用:vcl50.bpi(或對應版本)、toolsapi.hpp、inifiles.hpp
  • 進入點:Register() 函數(BPL 載入時自動呼叫)

核心代碼精要

1. 介面取得與主選單存取

_di_INTAServices IDE;
HRESULT hr = BorlandIDEServices->QueryInterface(__uuidof(INTAServices), (void**)&IDE);
if (SUCCEEDED(HR)) {
    #define AppBuilder IDE->MainMenu->Owner  // TApplication
    TMainMenu* MainMenu = dynamic_cast(AppBuilder->Components[i]); // 尋找主選單
}

2. 遞迴列舉選單項目並寫入 INI

void __fastcall EnumerateMenuItem(TMenuItem* Item) {
    DoMenuItem(Item);  // 處理單一項目
    for (int i = 0; i < Item->Count; ++i)
        EnumerateMenuItem(Item->Items[i]);  // 遞迴子項目
}

void __fastcall DoMenuItem(TMenuItem* Item) {
    if (!Item->Visible) Item->Visible = true;  // 強制顯示隱藏項
    if (NeedWriteToIni && !Item->Name.IsEmpty()) {
        Ini->WriteString(Section, Item->Name, Item->Caption);
    }
}

3. 從中文 INI 讀取並套用本地化

void __fastcall LocalizeAppMenu() {
    _di_INTAServices IDE;
    if (SUCCEEDED(BorlandIDEServices->QueryInterface(__uuidof(INTAServices), (void**)&IDE))) {
        #define AppBuilder IDE->MainMenu->Owner
        TStringList *Sections = new TStringList, *Idents = new TStringList;
        try {
            ChsIni->ReadSections(Sections);
            for (int s = 0; s < Sections->Count; ++s) {
                ChsIni->ReadSection(Sections->Strings[s], Idents);
                for (int i = 0; i < Idents->Count; ++i) {
                    AnsiString Val = ChsIni->ReadString(Sections->Strings[s], Idents->Strings[i], "NULL");
                    if (Val != "NULL") {
                        TMenuItem* MI = dynamic_cast(AppBuilder->FindComponent(Idents->Strings[i]));
                        if (MI) MI->Caption = Val;
                    }
                }
            }
        } __finally { delete Idents; delete Sections; }
        #undef AppBuilder
    }
}

4. 套件註冊入口

void __fastcall PACKAGE Register() {
    Ini = new TIniFile(ChangeFileExt(Application->ExeName, ".INI"));
    ChsIni = new TIniFile(ChangeFileExt(Application->ExeName, "CHS.INI"));
    if (NeedLocalize) LocalizeAppMenu();

    _di_INTAServices IDE;
    if (SUCCEEDED(BorlandIDEServices->QueryInterface(__uuidof(INTAServices), (void**)&IDE))) {
        #define AppBuilder IDE->MainMenu->Owner
        for (int i = 0; i < AppBuilder->ComponentCount; ++i) {
            if (AppBuilder->Components[i]->ClassNameIs("TMainMenu")) {
                #define AppMainMenu dynamic_cast(AppBuilder->Components[i])
                for (int x = 0; x < AppMainMenu->Items->Count; ++x) {
                    if (NeedWriteToIni) Section = AppMainMenu->Items->Items[x]->Name;
                    EnumerateMenuItem(AppMainMenu->Items->Items[x]);
                }
                #undef AppMainMenu
            }
        }
        #undef AppBuilder
    } else {
        ShowMessage("無法存取 IDE 服務,請卸載此模組。");
    }
    delete Ini; delete ChsIni;
}

部署與測試

  1. 編譯產生 Nodebug.bpl。
  2. 複製至 $(BCB)\Bin 目錄。
  3. 在 IDE 中:Component → Install Packages → Add 選擇該 BPL。
  4. 重啟 IDE,觀察選單是否變為中文(需預先準備對應的 CHS.INI)。

擴展方向

  • 自動產生程式碼範本、專案精靈。
  • 整合版本控制、靜態分析工具。
  • 自訂編輯器邊緣、語法高亮、鍵盤綁定。

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