TreeView implements the main menu function, and treeview implements the main menu

Source: Internet
Author: User

TreeView implements the main menu function, and treeview implements the main menu
TreeView implements the main menu Function


In the MenuItem menu, the software sets various functions and starts corresponding modules. To increase software flexibility, we can also transform the main menu project into a TreeView. By clicking TreeNode in the TreeView tree, we can open the corresponding module to achieve the main menu function.


1. Set global variables
Last_Node: TTreeNode; used to save the last node of treeview1. In this way, you only need to judge that TreeView1.Selected and Last_Node are consistent, resulting in secondary clicks on the same node to trigger the event.


2. MenuItemsToListView process:
Procedure TMDIFrm. MenuItemsToListView (mMenu: TMenuItem; nNode: TTreeNodes; p_node: TTreeNode );
Var I: Integer;
C_node: TTreeNode;
Begin
For I: = 0 to mMenu. Count-1 do // menu item Loop
Begin
If mMenu. Items [I]. Caption <> '-'Then
Begin
C_node: = nNode. AddChild (p_node, mMenu. Items [I]. Caption );
C_node.Data: = @ mMenu. Items [I]. OnClick; // Data pointer type, save menu events
End;
MenuItemsToListView (mMenu. Items [I], nNode, c_node); // Recursion
End;
End;


3. FormCreat event: reads menu data
Var parent_node: TTreeNode;
Begin
TreeView1.Items. Clear;
For I: = 0 to MainMenu1.Items. Count-1 do
Begin
Parent_node: = TreeView1.Items. Add (nil, MainMenu1.Items [I]. Caption );
MenuItemsToListView (MainMenu1.Items [I], TreeView1.Items, parent_node );
End;
End;


4. TreeView1.OnMouseUp mouse event
Procedure TMDIFrm. TreeView1MouseUp (Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer );
Var pro: tpolicyevent;
Begin
If TreeView1.Selected = Last_Node then
Begin
@ Pro: = TreeView1.Selected. Data; // read the event address
If Assigned (pro) then pro (nil); // execute the event
End;
Last_Node: = TreeView1.Selected;
End;


5. If you are on the TreeView1 node, press the button to send an event.
(1) cancel the TreeView1.OnMouseUp mouse event;
(2) Button event:
Procedure TMDIFrm. Button1Click (Sender: TObject );
Var pro: tpolicyevent;

Begin

If TreeView1.Selected = nil then exit;

@ Pro: = TreeView1.Selected. Data;
If Assigned (pro) then pro (nil );
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.