mfc 多文档中怎样动态添加菜单项?

mfc 多文档中怎样动态添加菜单项?,第1张

pDocTemplate = new CSingleDocTemplate(

IDR_MAINFRAME,

RUNTIME_CLASS(CMyScrollViewDoc),

RUNTIME_CLASS(CMainFrame), // 主 SDI 框架窗口

RUNTIME_CLASS(CMyScrollViewView))这个函数将文档类视类和框架类结合在一起,这个是单文档的组织形式,所以在单文档中框架类始终没有改变,所以可以动态加载菜单

pDocTemplate = new CMultiDocTemplate(IDR_questionTYPE,

RUNTIME_CLASS(CquestionDoc),

RUNTIME_CLASS(CChildFrame), // 自定义 MDI 子框架

RUNTIME_CLASS(CquestionView))而这个是多文档的组织形式,可以看到第三个参数变成了CChildFrame,而不是CMainFrame,第一个参数也有所改变看,所以每新建一个多文档就会对应一个CChildFrame,所以动态添加的菜单不会显示在CMainFrame上,多文档不共用框架类

这个貌似不难,右键菜单用代码生成就行了。

你是要用ContextMenuStrip控件,生成的菜单可以用代码控制。如

// This code example demonstrates how to handle the Opening event.

// It also demonstrates dynamic item addition and dynamic

// SourceControl determination with reuse.

class Form3 : Form

{

// Declare the ContextMenuStrip control.

private ContextMenuStrip fruitContextMenuStrip

public Form3()

{

// Create a new ContextMenuStrip control.

fruitContextMenuStrip = new ContextMenuStrip()

// Attach an event handler for the

// ContextMenuStrip control's Opening event.

fruitContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening)

// Create a new ToolStrip control.

ToolStrip ts = new ToolStrip()

// Create a ToolStripDropDownButton control and add it

// to the ToolStrip control's Items collections.

ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit")

ts.Items.Add(fruitToolStripDropDownButton)

// Dock the ToolStrip control to the top of the form.

ts.Dock = DockStyle.Top

// Assign the ContextMenuStrip control as the

// ToolStripDropDownButton control's DropDown menu.

fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip

// Create a new MenuStrip control and add a ToolStripMenuItem.

MenuStrip ms = new MenuStrip()

ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit")

ms.Items.Add(fruitToolStripMenuItem)

// Dock the MenuStrip control to the top of the form.

ms.Dock = DockStyle.Top

// Assign the MenuStrip control as the

// ToolStripMenuItem's DropDown menu.

fruitToolStripMenuItem.DropDown = fruitContextMenuStrip

// Assign the ContextMenuStrip to the form's

// ContextMenuStrip property.

this.ContextMenuStrip = fruitContextMenuStrip

// Add the ToolStrip control to the Controls collection.

this.Controls.Add(ts)

//Add a button to the form and assign its ContextMenuStrip.

Button b = new Button()

b.Location = new System.Drawing.Point(60, 60)

this.Controls.Add(b)

b.ContextMenuStrip = fruitContextMenuStrip

// Add the MenuStrip control last.

// This is important for correct placement in the z-order.

this.Controls.Add(ms)

}

// This event handler is invoked when the ContextMenuStrip

// control's Opening event is raised. It demonstrates

// dynamic item addition and dynamic SourceControl

// determination with reuse.

void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)

{

// Acquire references to the owning control and item.

Control c = fruitContextMenuStrip.SourceControl as Control

ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem

// Clear the ContextMenuStrip control's Items collection.

fruitContextMenuStrip.Items.Clear()

// Check the source control first.

if (c != null)

{

// Add custom item (Form)

fruitContextMenuStrip.Items.Add("Source: " + c.GetType().ToString())

}

else if (tsi != null)

{

// Add custom item (ToolStripDropDownButton or ToolStripMenuItem)

fruitContextMenuStrip.Items.Add("Source: " + tsi.GetType().ToString())

}

// Populate the ContextMenuStrip control with its default items.

fruitContextMenuStrip.Items.Add("-")

fruitContextMenuStrip.Items.Add("Apples")

fruitContextMenuStrip.Items.Add("Oranges")

fruitContextMenuStrip.Items.Add("Pears")

// Set Cancel to false.

// It is optimized to true based on empty entry.

e.Cancel = false

}

}

就是一个基础框架的例子。

首先根据ID获取当前权限的菜单xml文件,一般将xml存储到数据库里面。然后通过xml文件去编辑菜单。

再将菜单添加到form界面上面,给你个简单例子。

    public partial class TreeMenu : RadTreeView

    {

        #region Field

        private XDocument xDoc = null

        #endregion

 

        #region Property

 

        public XDocument XDoc

        {

            set 

            {

                this.xDoc = value

 

                var items = xDoc.Element("Menu").Elements("MenuItem")

                foreach (var item in items.ToList())

                {

                    this.Nodes.Add(this.CreateTreeNode(item))

                }

            }

        }

        #endregion

 

        public TreeMenu()

        {

            InitializeComponent()

        }

 

        public TreeMenu(IContainer container)

        {

            container.Add(this)

            InitializeComponent()

 

            this.NodeCheckedChanged += TreeMenu_NodeCheckedChanged

        }

 

        #region Method

        /// <summary>

        /// 点击节点更换

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void TreeMenu_NodeCheckedChanged(object sender, RadTreeViewEventArgs e)

        {

            //throw new NotImplementedException()

            RadTreeNode node = sender as RadTreeNode

 

        }

 

        private RadTreeNode CreateTreeNode(XElement xel)

        {

            //string treeName = xel.Attribute("Text").Value.ToString()

            RadTreeNode node = new RadTreeNode()

            node.Text = xel.Attribute("Text").Value.ToString()

            if (xel.Elements("MenuItem").Count() > 0)

            {

                foreach (var q in xel.Elements("MenuItem"))

                {

                    node.Nodes.Add(this.CreateTreeNode(q))

                }

            }

            node.Tag = xel

            

            return node

        }

        #endregion

    }

菜单事件部分肯定要用到反射,都是些基础的东西。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11641499.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存