18.C#编程学习——右键菜单

132 阅读1分钟

18.C#编程学习——右键菜单

源码

usingSystem;

usingSystem.Drawing;

usingSystem.Windows.Forms;

 

classContextMenuDemo : Form

{

    MenuItem miColor;

 

    publicstaticvoid Main()

    {

        Application.Run(newContextMenuDemo());

    }

    public ContextMenuDemo()

    {

        Text = "Context Menu Demo";

 

        EventHandler eh = newEventHandler(MenuColorOnClick);

 

        MenuItem[] ami = { newMenuItem("Black",   eh),

                             newMenuItem("Blue",    eh),

                             newMenuItem("Green",   eh),

                             newMenuItem("Cyan",    eh),

                             newMenuItem("Red",     eh),

                             newMenuItem("Magenta", eh),

                             newMenuItem("Yellow", eh),

                             newMenuItem("White",   eh) };

 

        foreach (MenuItemmi in ami)

            mi.RadioCheck = true;

 

        miColor = ami[3];

        miColor.Checked = true;

        BackColor = Color.FromName(miColor.Text);

 

        ContextMenu = newContextMenu(ami);

    }

    void MenuColorOnClick(objectobj, EventArgsea)

    {

        miColor.Checked = false;

        miColor = (MenuItem)obj;

        miColor.Checked = true;

 

        BackColor = Color.FromName(miColor.Text);

    }

}