C ++ Builder dynamically established menu and menu event

zhaozj2021-02-17  59

C Builder Dynamic Establishment Menu and Menu Event 6/5/2001 8: 34: 13 · · 小勇 ·· Yesky first creates a new project file (file-> new application). Then build a popupMenu1 on Form1, a Button1, a Richedit1. In the contents of Unit1.h, the black body is self-added.

#ifndef unit1h # define unit1h # include

#include

#include

#include

#include

#include

#include

Class TFORM1: Public TForm

{

__published: // Ide-management Components

TButton * Button1;

TPopupmenu * PopupMenu1;

Trichedit * richedit1;

Void __fastcall button1click (TOBJECT * Sender);

Void __fastcall formcreate (TOBJECT * Sender);

PRIVATE: // user declarations

Point mousepos; // Save the position of the mouse

INT POPCOUNT; / / Save menu item

Void __fastcall myclick (TOBJECT * Sender); // Self-defined click event

Public: // user declarations

__fastcall tform1 (tComponent * Owner);

}

Extern package tform1 * form1;

#ENDIF

In Unit1.cpp, the black body has been added.

#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: // If PopupMenu1 has a menu item

PopupMenu1-> items-> delete (i); file: // is deleted to avoid repeated establishment

TsearchRec Sr; // Used to reverse the file name

TMenuItem * newItem;

GetCursorpos (& MousePos); // Save the current mouse position in MousePos

INT MENUNAMELENGTH; // Menu name length

IF (FindFirst ("C: // Program files // 3ltyping // Data /// *. TXT", 0, SR) == 0)

{

File: / / get the first eligible file name;

File: // TXT file location, you can set it yourself;

NewItem = new tmenuitem (PopupMenu1); File: // Establish a menu

MenunameLength = sr.name.length (); file: // File name length

Sr.Name.setlength; file: // removes ".txt"

NEWITEM-> CAPTION = sr.name; file: // New menu name

PopupMenu1-> items-> add (newItem); file: // Plus new menu item to popupMenu1

NewItem-> onclick = myclick; file: // Put your defined myclick function to the new menu file: // onclick Click Event

File: // Find the next meeting of the firm, until

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); // Diss

}

PopCount = PopupMenu1-> items-> count; // PopupMenu1 Save Number Save

PopupMenu1-> popup (mousepos.x, mousepos.y); // Pop up the PoupMenu1 menu in the current location

}

{

File: // The following code is mainly to display the selected file on Richedit1.

FORM1-> CAPTION = ((tMenuitem *) Sender) -> CAPTION;

Richedit1-> plaintext = true; // is set to true, indicating a plain text file

Richedit1-> lines-> loadingfromfile

"C: // program files // 3ltyping // data //"

((TMENUITEM *) Sender -> Caption ". TXT");

}

Void __fastcall tform1 :: formcreate (TOBJECT * SENDER)

{

PopCount = 0; // Initialization

}

After the above program is compiled, click Button1, PoupMenu1 will pop up, and the contents of the file will be displayed on the RicheDit1 by clicking the menu item below PoupMenu1. This procedure is simple, mainly to explain how to dynamically create menus, and how to respond to events from the new menu, can also be applied to other top, such as TMenumain, etc. . . . . This program passes the C Builder 4.0.

转载请注明原文地址:https://www.9cbs.com/read-30903.html

New Post(0)