- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <stdlib.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::ReadMenuFile(AnsiString fname)
- {
- TFileStream *sm;
- int ii,nn;
- TMenuItem *mitem,*curitem;
- AnsiString name,value;
- //清除菜单
- nn=MainMenu1->Items->Count;
- for(ii=0;ii<nn;ii++)
- delete MainMenu1->Items->Items[ii];
- //读取菜单文件
- try
- {
- sm=new TFileStream(fname,fmOpenRead); //以只读方式打开文本菜单文件
- mitem=MainMenu1->Items;
- curitem=mitem;
- for(;;) //循环读取文件内容
- {
- name=ReadName(sm); //读取Name=Value格式的Name
- value=ReadValue(sm); //读取Name=Value格式的Value
- if((name=="")||(name=="End"))break; //如果读到了结束代码,跳出循环
- name=name.UpperCase(); //将Name全变为大写字符
- if(name=="MENU") //菜单代码
- {
- value=value.UpperCase();
- if(value=="MAIN") //是主菜单吗
- {
- curitem=MainMenu1->Items;
- }
- else if(value=="SUBMENU") //是子菜单吗
- {
- curitem=mitem;
- }
- else if(value=="EXITSUB") //该退出子菜单吗
- {
- curitem=curitem->Parent;
- }
- else{}
- }
- else if(name=="NAME") //是菜单名称吗
- {
- mitem=new TMenuItem(this); //新建一个菜单项
- mitem->Caption=value;
- curitem->Add(mitem); //加入当前菜单项中
- }
- else if(name=="KEY") //是键值吗
- {
- mitem->Tag=atoi(value.c_str()); //用菜单项的Tag属性保存
- mitem->OnClick=MItemClick; //让每一菜单项的Click事件指向同一操作
- }
- else if(name=="ALIAS") //菜单项的别名
- {
- mitem->Caption=value;
- }
- else if(name=="VISIBLE") //菜单项可见吗
- {
- mitem->Visible=False;
- }
- else if(name=="ENABLED") //菜单项激活吗
- {
- mitem->Enabled=False;
- }
- else{}
- }
- }
- __except(EXCEPTION_EXECUTE_HANDLER)//异常处理
- {
- Application->MessageBox("不能打开菜单文件!","提示",MB_OK);
- }
- delete sm; //删除已分配的sm流
- }
- //---------------------------------------------------------------------------
- AnsiString ReadName(TStream *sm)
- {
- int nn;
- AnsiString ass;
- char ss[2];
- ass="";
- ss[1]=0;
- for(;;)
- {
- nn=sm->Read(ss,1);
- if(nn!=1)break;
- if((ss[0]=='=')||(ss[0]==0x0d)||(ss[0]==0x0a))
- {
- if(ass!="")break;
- }
- else
- {
- ass=ass+ss;
- }
- }
- return ass;
- }
- //---------------------------------------------------------------------------
- AnsiString ReadValue(TStream *sm)
- {
- int nn;
- AnsiString ass;
- char ss[2];
- ass="";
- ss[1]=0;
- for(;;)
- {
- nn=sm->Read(ss,1);
- if(nn!=1)break;
- if(ss[0]==0x0a)
- {
- break;
- }
- else
- {
- if(ss[0]!=0x0d)
- ass=ass+ss;
- }
- }
- return ass;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MItemClick(TObject *Sender)
- {
- TMenuItem *mitem;
- int key;
- mitem=(TMenuItem *)Sender;
- key=mitem->Tag;
- ShowMessage(IntToStr(key));
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- ReadMenuFile("TestMenu.Txt");
- }
- //---------------------------------------------------------------------------