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;