Dynamic modification technology of menus and toolbars in the VC ++ environment

zhaozj2021-02-11  216

Dynamic Modification of Menu and Toolbars in the VC Environment Visual C is a visualized programming tool, its built-in wizard appWizard can automatically generate the basic framework of the program, reducing a large number of program development basic work. For example, AppWizard can automatically generate a menu and toolbar for the application, it is very convenient. If you are writing a general application, you can modify, add or remove menu items on the menu and toolbar they provide. But in actual use, if you encounter some complicated programs, only the menu and toolbar provided by the system is far less than enough. Applications should be able to display different menu items and toolbars according to the different status of users, and make dynamic modifications to the menu and toolbar, load new menus and toolbars. Dynamic changes to the menu and toolbar are basically two ways during programming. First, in the process of running the CMenu class, the INSERTMENU of the CMenu class is inserted into the new menu item on the menu, but if this method is only used in dynamically change a few menu items, its programming work is still received, but If the part needs to be changed, the programming workload is very large, and the biggest problem is if the menu item inserted in this method, the Class Wizard cannot generate the corresponding message response function, and the programmer must write a message response loop. Therefore, a second dynamic modification method is proposed. The key to this method is not to modify the menu and toolbar, but by loading different menus and toolbars implement dynamic modifications to the menu and toolbar. The method is that it is not necessary to write a large number of menu items inserted into the command, simply define different menus and toolbars in advance according to the actual situation, and then load different menus and toolbar resources in the program to implement the menu and Dynamic update of the toolbar. And this method is suitable for class Wizard, which can produce the message response and other related functions of each menu item in the Class Wizard, greatly reduces the development workload of the program. The specific steps are as follows: First analyze the various changes in the process of operation, and create different menu resources and toolbar resources in resource management according to different menu status. Then use the loadtoolbar function and the LoadMenu function to transfer the corresponding menu resources and toolbar resources in places where you need to modify the menu and toolbar. Void cmainframe :: OnSwitch) {// Turn into toolbar resources represented by idR_NewToolbar; m_wndtoolbar.loadtoolbar (idR_NewToolbar); // After tune into the new toolbar, call the InvaliDate function to ensure the use of the new bitmap image. Toolbar; m_wndtoolbar.invalidate (); cMenu cmenu; // call new menu resources represented by idR_newmenu; cMenu.loadMenu (iDR_NewMenu); // Set the cMenu to the current menu; SetMenu (& CMenu); // Release the menu handle CMenu.Detach (); // Heavy painting menu strip; DrawMenubar (); // Re-draw window area; recalcLayout (true);} When switching menus, be careful to call the setach function immediately after calling the setMenu function to set the current window menu. Release the menu handle from the CMenu object, which prevents CMenu from being destroyed when the scope is removed, and the current function is terminated back. Finally, to call the RecalClayout Recalculation window size to re-draw the window. If you only replace a toolbar, you can use the function if the new toolbar is different from the original toolbar size or to call multiple toolbars, you must use this function to recalculate the size of the customer window to redraw the customer window. Also add some, if only the toolbar is modified, there is a more feasible way to create all the toolbar objects in OnCreate, then use the showWindow as needed to determine its display or not.

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

New Post(0)