Powerfully create controls and process the addition event processing in C ++ Builder

zhaozj2021-02-08  215

Home: http://www.maxss.net email: maxchou@163.com

When developing a similar interpreter or an application such as an application or other type of application, you need to dynamically create control (objects), and also add the appropriate event handler for these controls, and will be introduced in C Builder. How to implement a basic implementation framework.

Dynamically create a general process of control:

TButton * btn = new tbutton (this);

BTN-> CAPTION = "Button1";

BTN-> Parent = this;

BTN-> show ();

Below the example of a dynamically generated menu to practice how to dynamically create controls and process the add event processing.

Write a function to insert menu items from an existing menu object:

Void __fastcall tform1 :: addContextMenuItem (TPopupmenu * Pmenu,

ANSISTRING CAPTION, UNSIGNED INT TAG, TNOTIFYEVENT NOTIFYEVENT

parameter:

PMENU: Exes already existing TPopupMenu object pointer

CAPTION: Title of the menu item

Tag: Tag value for menu items, used to identify different TMENUITEM objects

NotifyEvent: Event handler of this menu item

Function implementation:

TMenuItem * mnuitem = new tmenuitem (PMENU); // CREATE New Item

Pmenu-> items-> add (mnuitem); // add it to pmenu

Mnuitem-> Name = "cm_" INTOSTR (TAG);

Mnuitem-> Caption = CAPTION;

Mnuitem-> ImageIndex = imgindex;

Mnuitem-> tag = tag;

Mnuitem-> onclick = notifyevent; // Assign IT An Event Handler

Then write the function CreateContextMenu () called AddContextMenuItem (...), defined as follows:

Void __fastcall tform1 :: CreateContextMenu (void)

{

FTAGINC = 0;

FcontextMenu = New TPopupMenu (this);

FcontextMenu-> AutohotKeys = Mamanual

FcontextMenu-> ONPOPUP = UpdateContextMenuItem

AddContextMenuItem (FcontextMenu, "Menu Item 1", FTAGINC , MenuItemClick;

AddContextMenuItem (FcontextMenu, "Menu Item 2", FTAGINC , MenuItemClick;

AddContextMenuItem (FcontextMenu, "-", ftaginc , nullnotifyevent;

AddContextMenuItem (FcontextMenu, "Menu Item 3", FTAGINC , MenuItemClick;

}

FcontextMenu is a member variable of TFORM1. UpdateContextMenuItem () is an update menu item in the Popup event for FcontextMenu. MenuItemClick and NullNotifyEvent are all member functions in TForm1, which is the event handler of the controlled control, format:

Void __fastcall tform1 :: menuItemClick (Tobject * Sender)

Void __fastcall tform1 :: nullnotifyeverEvent (TOBJECT * SENDER)

The above is my own experience, I hope to continue to communicate with the university experience.

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

New Post(0)