为窗体添加快捷菜单示例

来源:互联网 发布:猫云seo seojsc 编辑:程序博客网 时间:2024/06/05 01:10
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 为窗体添加快捷菜单示例{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void toolStripMenuItem_Click(object sender, EventArgs e)        {            ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;            string text = tsmi.Text;            switch (text)            {                case "还原(&R)":                    this.WindowState = FormWindowState.Normal;                    break;                case "最小化(&N)":                    this.WindowState = FormWindowState.Minimized;                    break;                case "最大化(&M)":                    this.WindowState = FormWindowState.Maximized;                    break;                case "显示在顶端(&T)":                    this.TopMost = !this.TopMost;                    break;                case "关闭(&C)":                    this.Close();                    break;            }        }        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)        {            MenuItemRestore.Enabled = !(this.WindowState == FormWindowState.Normal);            MenuItemMax.Enabled = !(this.WindowState == FormWindowState.Maximized);            MenuItemTop.Checked = this.TopMost;        }        private void StyleToolMenuItem_Click(object sender, EventArgs e)        {            ToolStripMenuItem item = (ToolStripMenuItem)sender;            Font oldFont = this.textBox1.Font;            Font newFont = null;            switch (item.Text)            {                case "粗体(&B)":                    if (item.Checked)                        newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);                    else newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);                    break;                case "斜体(&I)":                    if (item.Checked)                        newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);                    else newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);                    break;                case "下划线(&U)":                    if (item.Checked)                        newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);                    else newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);                    break;            }            this.textBox1.Font = newFont;        }        private void alignToolStripMenuItem_Click(object sender, EventArgs e)        {            ToolStripMenuItem item = (ToolStripMenuItem)sender;            switch (item.Text)            {                case "左对齐(&L)":                    if (item.Checked) this.textBox1.TextAlign = HorizontalAlignment.Left;                    this.左对齐LToolStripMenuItem.Checked = true;                    this.右对齐RToolStripMenuItem.Checked = false;                    this.居中对齐CToolStripMenuItem.Checked = false;                    break;                case "右对齐(&R)":                    if (item.Checked) this.textBox1.TextAlign = HorizontalAlignment.Right;                    this.左对齐LToolStripMenuItem.Checked = false;                    this.右对齐RToolStripMenuItem.Checked = true;                    this.居中对齐CToolStripMenuItem.Checked = false;                    break;                case "居中对齐(&C)":                    if (item.Checked) this.textBox1.TextAlign = HorizontalAlignment.Center;                    this.左对齐LToolStripMenuItem.Checked = false;                    this.右对齐RToolStripMenuItem.Checked = false;                    this.居中对齐CToolStripMenuItem.Checked = true;                    break;            }        }    }}