大多數BCB程序員也許一輩子都不用關心Toolsapi,但如果象我一樣無聊的話,
不妨玩玩和討論一下:
看看下面的程序,來把IDE變個樣:
--->BCB5。0
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
USERES("nodebug.res");
USEPACKAGE("vcl50.bpi");
//---------------------------------------------------------------------------
#include <toolsapi.hpp>
#include <inifiles.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Package source.
//---------------------------------------------------------------------------
namespace Nodebug // Here the namespace should be same with this file name.
{
bool NeedLocalize = true;
bool NeedWriteToIni = false;
TIniFile *ini, *chsini;
AnsiString Section, Ident, Value;
//===[ Do something on MenuItems. ]======================================
void __fastcall DoMenuItem(TMenuItem* MenuItem)
{
//Display all MenuItems.
if(MenuItem->Visible==false) MenuItem->Visible = true;
if ( NeedWriteToIni && !MenuItem->Name.IsEmpty())
{
Ident = MenuItem->Name;
Value = MenuItem->Caption;
ini->WriteString ( Section, Ident, Value );
}
}
//===[ Do something on MenuItems. ]======================================
//===[ Enumerate all MenuItem's SubItems. ]==============================
void __fastcall EnumerateMenuItem(TMenuItem* MenuItem)
{
DoMenuItem(MenuItem);
for ( int i=0; i<MenuItem->Count; i++ )
{
EnumerateMenuItem(MenuItem->Items[i]);
}
}
//===[ Enumerate all MenuItem's SubItems. ]==============================
void __fastcall LocalizeAppMenu()
{
_di_INTAServices IDE;
HRESULT hr = BorlandIDEServices->QueryInterface(__uuidof(INTAServices), (void **) &IDE);
if (SUCCEEDED(hr))
{
//----------[AppBuilder macro start]--------------------------------------
#ifndef AppBuilder
#define AppBuilder IDE->MainMenu->Owner
#endif
//----------[AppBuilder macro end ]--------------------------------------
TStringList* SectionList = new TStringList;
TStringList* IdentList = new TStringList;
AnsiString Value , Default("NULL");
try
{
chsini->ReadSections(SectionList);
for (int i = 0; i < SectionList->Count; i++)
{
chsini->ReadSection( SectionList->Strings[i], IdentList);
for (int x = 0; x < IdentList->Count; x++)
{
Value = chsini->ReadString( SectionList->Strings[i],
IdentList->Strings[x],
"NULL");
if( Default != Value )
{
TMenuItem* MI = dynamic_cast<TMenuItem*>
(AppBuilder->FindComponent(IdentList->Strings[x]));
if(MI != NULL) MI->Caption = Value;
}
}
}
}
#undef AppBuilder
__finally
{
delete IdentList;
delete SectionList;
}
}
}
//===[ BPL's 'Main' function or Entry Function. ]========================
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;
HRESULT hr = BorlandIDEServices->QueryInterface(__uuidof(INTAServices), (void **) &IDE);
if (SUCCEEDED(hr))
{
//----------[AppBuilder macro start]--------------------------------------
#ifndef AppBuilder
#define AppBuilder IDE->MainMenu->Owner
#endif
//----------[AppBuilder macro end ]--------------------------------------
for ( int i = 0; i<AppBuilder->ComponentCount; i++ ) // ComponentCount = 409
{
if ( AppBuilder->Components[i]->ClassNameIs("TMainMenu") ) // MainMenu1
{
//----------[AppMainMenu macro start]--------------------------------------
#ifndef AppMainMenu
#define AppMainMenu dynamic_cast<TMainMenu*>( AppBuilder->Components[i])
#endif
//----------[AppMainMenu macro end ]--------------------------------------
for ( int x = 0; x < AppMainMenu->Items->Count; x++ ) //count 11
{
if(NeedWriteToIni) Section = AppMainMenu->Items->Items[x]->Name;
EnumerateMenuItem(AppMainMenu->Items->Items[x]);
}//for finished.
#undef AppMainMenu
}//if finished.
}//for finished
#undef AppBuilder
}
else ShowMessage("Error:I Can't Access IDE.\nYou'd better unload this module.");
delete ini;
delete chsini;
}
//===[ BPL's 'Main' function or Entry Function. ]========================
}//namespace finished.
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
return 1;
}
//---------------------------------------------------------------------------