main.cpp

来自「C++Builder程序员编程手记《配书光盘》」· C++ 代码 · 共 93 行

CPP
93
字号
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AddButtonClick(TObject *Sender)
{
  TMenuItem *newadditem = new TMenuItem(this);

  if(menuitemcount==0)
    MenuFunction->InsertNewLineAfter(Exitcmd);
  if(menuitemcount<addlimit)
  {
    newadditem->Caption=newitemtext+IntToStr(menuitemcount);
    MenuFunction->Add(newadditem);
    menuitemcount++;
  }
  else
    ShowMessage("你已经创建了"+IntToStr(addlimit)+"项菜单!");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 if(Edit1->Text.ToInt()<addlimit&&MenuFunction->Count>5)
  {
    for(int i=0;i<(MenuFunction->Count-Edit1->Text.ToInt());i++)
    {
      MenuFunction->Delete(5);
      menuitemcount--;
    }
  }
  addlimit=Edit1->Text.ToInt();
  Button3->Enabled=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Edit1Change(TObject *Sender)
{
  int Tmp;

  try
  {
    Tmp=Edit1->Text.ToInt();
  }
  catch(const EConvertError &e)
  {
    ;
  }
  if(Tmp<=10&&Tmp>=1)
    Button3->Enabled=true;
  else if(Tmp>10)
  {
    ShowMessage("你最多可增加10个菜单项");
    Edit1->Text="10";
    Edit1->Update();
    Button3->OnClick(Sender);
  }
  else
  {
    ShowMessage("你最少要设置1个菜单项");
    Edit1->Text="1";
    Edit1->Update();
    Button3->OnClick(Sender);
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DeleteButtonClick(TObject *Sender)
{
  if(MenuFunction->Count>5)
  {
    MenuFunction->Delete(5);
    menuitemcount--;
  }
  else
    ShowMessage("你已经删除了全部动态菜单!");

}
//---------------------------------------------------------------------------


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?