国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C++ BUILDER 動(dòng)態(tài)建立菜單及菜單事件

2019-11-17 05:13:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  首先新建一個(gè)工程文件(File->New application)。
然后在Form1上面建立一個(gè)PopupMenu1,一個(gè)Button1,一個(gè)RichEdit1。


  在頭文Unit1.H的內(nèi)容,其中黑體是自已加的。

#ifndef Unit1H
#define Unit1H
#include
#include
#include
#include
#include
#include
#include
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TPopupMenu *PopupMenu1;
TRichEdit *RichEdit1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
PRivate: // User declarations
POINT MousePos;//保存鼠標(biāo)的位置
int PopCount;//保存菜單項(xiàng)個(gè)數(shù)
void __fastcall MyClick(TObject *Sender);//自已定義的單擊事件
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
extern PACKAGE TForm1 *Form1;
#endif




在Unit1.cpp中的內(nèi)容,黑體自已加。

#include
#pragma hdrstop

#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
for(int i=PopCount-1;i>=0;i--) file://假如PopupMenu1里面有菜單項(xiàng)
PopupMenu1->Items->Delete(i); file://就刪除,以免重復(fù)建立

TSearchRec Sr;//用來(lái)反回文件名
TMenuItem *NewItem;
GetCursorPos(&MousePos);//把當(dāng)前的鼠標(biāo)位置保存在MousePos里
int MenuNameLength;//菜單名的長(zhǎng)度

if(FindFirst("C://Program Files//3LTyping//data//*.txt",0,Sr)==0)

{
file://取得第一個(gè)符合條件的文件名;

file://TXT文件的位置,可自己設(shè)定;

NewItem=new TMenuItem(PopupMenu1); file://建立菜單
MenuNameLength=Sr.Name.Length(); file://文件名的長(zhǎng)度
Sr.Name.SetLength(MenuNameLength-4); file://去除“.TXT”
NewItem->Caption=Sr.Name; file://新建菜單的名字
PopupMenu1->Items->Add(NewItem); file://把新建菜單項(xiàng)加到PopupMenu1里
NewItem->OnClick=MyClick; file://把自己定義的MyClick函數(shù)復(fù)給新菜單的

file://OnClick單擊事件


file://查找下一個(gè)符合條件的文件名,直到完

while(FindNext(Sr)==0)

{
NewItem=new TMenuItem(PopupMenu1);
MenuNameLength=Sr.Name.Length();
Sr.Name.SetLength(MenuNameLength-4);
NewItem->Caption=Sr.Name;
PopupMenu1->Items->Add(NewItem);
NewItem->OnClick=MyClick;

}


FindClose(Sr);//結(jié)事查找

}


PopCount=PopupMenu1->Items->Count;//PopupMenu1下的菜單項(xiàng)個(gè)數(shù)保存
PopupMenu1->Popup(MousePos.x,MousePos.y);//在當(dāng)前位置彈出PoupMenu1菜單
}
{
file://下面的代碼主要是把選定的文件顯示在RichEdit1上

Form1->Caption=((TMenuItem*)Sender)->Caption;
RichEdit1->PlainText=true;//設(shè)為true,表示純文本文件
RichEdit1->Lines->LoadFromFile(
"C://Program Files//3LTyping//data//"+
((TMenuItem*)Sender)->Caption+".txt");
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
PopCount=0;//初始化
}



  上面的程序編譯之后,單擊Button1,就會(huì)彈出PoupMenu1,通過(guò)單擊PoupMenu1下面的菜單項(xiàng)就會(huì)在RichEdit1上面顯示文件的內(nèi)容。

  這個(gè)程序很簡(jiǎn)單,主要是說(shuō)明如何動(dòng)態(tài)建立菜單,及如何響應(yīng)新建菜單的事件,也可應(yīng)用到其它上面,如TMenuMain等。。。。。


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凌海市| 古交市| 高要市| 通河县| 辉南县| 滦平县| 石狮市| 大田县| 天水市| 呼玛县| 建宁县| 汝城县| 武威市| 海伦市| 乾安县| 金寨县| 中牟县| 陆丰市| 内乡县| 武汉市| 华宁县| 建水县| 上蔡县| 承德市| 平乐县| 泗洪县| 迭部县| 保德县| 武宣县| 沁源县| 日照市| 荥经县| 石柱| 正蓝旗| 南丰县| 普兰县| 衢州市| 右玉县| 石城县| 乡城县| 陈巴尔虎旗|