Csharp/C Sharp/GUI Windows Form/ToolStripMenuItem

Материал из .Net Framework эксперт
Версия от 11:32, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

ToolStripMenuItem in action

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem1;
    private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem formatToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem colorToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem blackToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem blueToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem redToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem greenToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem fontToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem timesToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem courierToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem comicToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem boldToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem italicToolStripMenuItem;
    private System.Windows.Forms.ToolStripSeparator dashToolStripMenuItem;
    private System.Windows.Forms.Label displayLabel;
  public Form1() {
        InitializeComponent();
  }
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      MessageBox.Show("First Line\nSecond Line",
         "About", MessageBoxButtons.OK, MessageBoxIcon.Information );
    }
   private void exitToolStripMenuItem_Click(object sender, EventArgs e)
   {
      Application.Exit();
   }
   private void ClearColor()
   {
      blackToolStripMenuItem.Checked = false;
      blueToolStripMenuItem.Checked = false;
      redToolStripMenuItem.Checked = false;
      greenToolStripMenuItem.Checked = false;
   } 
   private void blackToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();
      displayLabel.ForeColor = Color.Black;
      blackToolStripMenuItem.Checked = true;
   }
   private void blueToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();
      displayLabel.ForeColor = Color.Blue;
      blueToolStripMenuItem.Checked = true;
   }
   private void redToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();
      displayLabel.ForeColor = Color.Red;
      redToolStripMenuItem.Checked = true;
   }
   private void greenToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearColor();
      displayLabel.ForeColor = Color.Green;
      greenToolStripMenuItem.Checked = true;
   }
   private void ClearFont()
   {
      timesToolStripMenuItem.Checked = false;
      courierToolStripMenuItem.Checked = false;
      comicToolStripMenuItem.Checked = false;
   }
   private void timesToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearFont();
      timesToolStripMenuItem.Checked = true;
      displayLabel.Font = new Font( 
         "Times New Roman", 14, displayLabel.Font.Style );
   }
   private void courierToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearFont();
      courierToolStripMenuItem.Checked = true;
      displayLabel.Font = new Font(
         "Courier", 14, displayLabel.Font.Style );
   }
   private void comicToolStripMenuItem_Click(object sender, EventArgs e)
   {
      ClearFont();
      comicToolStripMenuItem.Checked = true;
      displayLabel.Font = new Font(
         "Comic Sans MS", 14, displayLabel.Font.Style );
   }
   private void boldToolStripMenuItem_Click(object sender, EventArgs e)
   {
      boldToolStripMenuItem.Checked = !boldToolStripMenuItem.Checked;
      displayLabel.Font = new Font(
         displayLabel.Font.FontFamily, 14,
         displayLabel.Font.Style ^ FontStyle.Bold );
   }
   private void italicToolStripMenuItem_Click(object sender, EventArgs e)
   {
      italicToolStripMenuItem.Checked = !italicToolStripMenuItem.Checked;
      displayLabel.Font = new Font(
         displayLabel.Font.FontFamily, 14,
         displayLabel.Font.Style ^ FontStyle.Italic );
   } 
   private void InitializeComponent()
   {
     this.menuStrip1 = new System.Windows.Forms.MenuStrip();
     this.fileToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
     this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.formatToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.colorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.blackToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.blueToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.redToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.greenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.fontToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.timesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.courierToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.ruicToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.dashToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator();
     this.boldToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.italicToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
     this.displayLabel = new System.Windows.Forms.Label();
     this.menuStrip1.SuspendLayout();
     this.SuspendLayout();
     // 
     // menuStrip1
     // 
     this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.fileToolStripMenuItem1,
        this.formatToolStripMenuItem});
     this.menuStrip1.Location = new System.Drawing.Point(0, 0);
     this.menuStrip1.Name = "menuStrip1";
     this.menuStrip1.Size = new System.Drawing.Size(326, 24);
     this.menuStrip1.TabIndex = 4;
     this.menuStrip1.Text = "menuStrip1";
     // 
     // fileToolStripMenuItem1
     // 
     this.fileToolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.aboutToolStripMenuItem,
        this.exitToolStripMenuItem});
     this.fileToolStripMenuItem1.Name = "fileToolStripMenuItem1";
     this.fileToolStripMenuItem1.Text = "File";
     // 
     // aboutToolStripMenuItem
     // 
     this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
     this.aboutToolStripMenuItem.Text = "About";
     this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
     // 
     // exitToolStripMenuItem
     // 
     this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
     this.exitToolStripMenuItem.Text = "Exit";
     this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
     // 
     // formatToolStripMenuItem
     // 
     this.formatToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.colorToolStripMenuItem,
        this.fontToolStripMenuItem});
     this.formatToolStripMenuItem.Name = "formatToolStripMenuItem";
     this.formatToolStripMenuItem.Text = "Format";
     // 
     // colorToolStripMenuItem
     // 
     this.colorToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.blackToolStripMenuItem,
        this.blueToolStripMenuItem,
        this.redToolStripMenuItem,
        this.greenToolStripMenuItem});
     this.colorToolStripMenuItem.Name = "colorToolStripMenuItem";
     this.colorToolStripMenuItem.Text = "Color";
     // 
     // blackToolStripMenuItem
     // 
     this.blackToolStripMenuItem.Name = "blackToolStripMenuItem";
     this.blackToolStripMenuItem.Text = "Black";
     this.blackToolStripMenuItem.Click += new System.EventHandler(this.blackToolStripMenuItem_Click);
     // 
     // blueToolStripMenuItem
     // 
     this.blueToolStripMenuItem.Name = "blueToolStripMenuItem";
     this.blueToolStripMenuItem.Text = "Blue";
     this.blueToolStripMenuItem.Click += new System.EventHandler(this.blueToolStripMenuItem_Click);
     // 
     // redToolStripMenuItem
     // 
     this.redToolStripMenuItem.Name = "redToolStripMenuItem";
     this.redToolStripMenuItem.Text = "Red";
     this.redToolStripMenuItem.Click += new System.EventHandler(this.redToolStripMenuItem_Click);
     // 
     // greenToolStripMenuItem
     // 
     this.greenToolStripMenuItem.Name = "greenToolStripMenuItem";
     this.greenToolStripMenuItem.Text = "Green";
     this.greenToolStripMenuItem.Click += new System.EventHandler(this.greenToolStripMenuItem_Click);
     // 
     // fontToolStripMenuItem
     // 
     this.fontToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.timesToolStripMenuItem,
        this.courierToolStripMenuItem,
        this.ruicToolStripMenuItem,
        this.dashToolStripMenuItem,
        this.boldToolStripMenuItem,
        this.italicToolStripMenuItem});
     this.fontToolStripMenuItem.Name = "fontToolStripMenuItem";
     this.fontToolStripMenuItem.Text = "Font";
     // 
     // timesToolStripMenuItem
     // 
     this.timesToolStripMenuItem.Name = "timesToolStripMenuItem";
     this.timesToolStripMenuItem.Text = "Times New Roman";
     this.timesToolStripMenuItem.Click += new System.EventHandler(this.timesToolStripMenuItem_Click);
     // 
     // courierToolStripMenuItem
     // 
     this.courierToolStripMenuItem.Name = "courierToolStripMenuItem";
     this.courierToolStripMenuItem.Text = "Courier";
     this.courierToolStripMenuItem.Click += new System.EventHandler(this.courierToolStripMenuItem_Click);
     // 
     // comicToolStripMenuItem
     // 
     this.ruicToolStripMenuItem.Name = "comicToolStripMenuItem";
     this.ruicToolStripMenuItem.Text = "Comic Sans";
     this.ruicToolStripMenuItem.Click += new System.EventHandler(this.ruicToolStripMenuItem_Click);
     // 
     // dashToolStripMenuItem
     // 
     this.dashToolStripMenuItem.Name = "dashToolStripMenuItem";
     // 
     // boldToolStripMenuItem
     // 
     this.boldToolStripMenuItem.Name = "boldToolStripMenuItem";
     this.boldToolStripMenuItem.Text = "Bold";
     this.boldToolStripMenuItem.Click += new System.EventHandler(this.boldToolStripMenuItem_Click);
     // 
     // italicToolStripMenuItem
     // 
     this.italicToolStripMenuItem.Name = "italicToolStripMenuItem";
     this.italicToolStripMenuItem.Text = "Italic";
     this.italicToolStripMenuItem.Click += new System.EventHandler(this.italicToolStripMenuItem_Click);
     // 
     // displayLabel
     // 
     this.displayLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
     this.displayLabel.Location = new System.Drawing.Point(12, 39);
     this.displayLabel.Name = "displayLabel";
     this.displayLabel.Size = new System.Drawing.Size(293, 89);
     this.displayLabel.TabIndex = 7;
     this.displayLabel.Text = "Text";
     // 
     // MenuTest
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(326, 169);
     this.Controls.Add(this.menuStrip1);
     this.Controls.Add(this.displayLabel);
     this.Name = "MenuTest";
     this.Text = "MenuTest";
     this.menuStrip1.ResumeLayout(false);
     this.ResumeLayout(false);
     this.PerformLayout();
  }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


Use ToolStripMenuItem to set font size

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
enum TextFontSize {
    FontSizeHuge = 30,
    FontSizeNormal = 20,
    FontSizeTiny = 8
}
public class MainWindow : Form {
    private TextFontSize currFontSize = TextFontSize.FontSizeNormal;
    private ToolStripMenuItem currentCheckedItem;
    public MainWindow() {
        InitializeComponent();
        currentCheckedItem = normalToolStripMenuItem;
        currentCheckedItem.Checked = true;
        this.toolStripTextBoxColor.LostFocus += new EventHandler(toolStripTextBoxColor_LostFocus);
    }
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
        Application.Exit();
    }
    void toolStripTextBoxColor_LostFocus(object sender, EventArgs e) {
        BackColor = Color.FromName(toolStripTextBoxColor.Text);
    }
    private void ContextMenuItemSelection_Clicked(object sender, EventArgs e) {
        currentCheckedItem.Checked = false;
        ToolStripMenuItem miClicked = miClicked = (ToolStripMenuItem)sender;
        if (miClicked.Name == "hugeToolStripMenuItem") {
            currFontSize = TextFontSize.FontSizeHuge;
            currentCheckedItem = hugeToolStripMenuItem;
        }
        if (miClicked.Name == "normalToolStripMenuItem") {
            currFontSize = TextFontSize.FontSizeNormal;
            currentCheckedItem = normalToolStripMenuItem;
        }
        if (miClicked.Name == "tinyToolStripMenuItem") {
            currFontSize = TextFontSize.FontSizeTiny;
            currentCheckedItem = tinyToolStripMenuItem;
        }
        currentCheckedItem.Checked = true;
        Invalidate();
    }
    private void MainWindow_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.DrawString("Right click on me...", new Font("Times New Roman", (float)currFontSize), new SolidBrush(Color.Black), 50, 50);
    }
    private void InitializeComponent() {
        this.mainMenuStrip = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.changeBackgroundColorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripTextBoxColor = new System.Windows.Forms.ToolStripTextBox();
        this.hugeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.normalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.tinyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.mainMenuStrip.SuspendLayout();
        this.fontSizeContextStrip.SuspendLayout();
        this.SuspendLayout();
        // 
        this.mainMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.changeBackgroundColorToolStripMenuItem});
        this.mainMenuStrip.Location = new System.Drawing.Point(0, 0);
        this.mainMenuStrip.Name = "mainMenuStrip";
        this.mainMenuStrip.Size = new System.Drawing.Size(300, 24);
        this.mainMenuStrip.TabIndex = 0;
        this.mainMenuStrip.Text = "menuStrip1";
        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.exitToolStripMenuItem});
        this.fileToolStripMenuItem.Text = "&File";
        this.exitToolStripMenuItem.Text = "E&xit";
        this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
        // 
        // changeBackgroundColorToolStripMenuItem
        // 
        this.changeBackgroundColorToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripTextBoxColor});
        this.changeBackgroundColorToolStripMenuItem.Name = "changeBackgroundColorToolStripMenuItem";
        this.changeBackgroundColorToolStripMenuItem.Text = "Change Background Color";
        // 
        // toolStripTextBoxColor
        // 
        this.toolStripTextBoxColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
        this.toolStripTextBoxColor.Name = "toolStripTextBoxColor";
        this.toolStripTextBoxColor.Size = new System.Drawing.Size(100, 21);
        this.fontSizeContextStrip.Enabled = true;
        this.fontSizeContextStrip.GripMargin = new System.Windows.Forms.Padding(2);
        this.fontSizeContextStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.hugeToolStripMenuItem,
            this.normalToolStripMenuItem,
            this.tinyToolStripMenuItem});
        this.fontSizeContextStrip.Location = new System.Drawing.Point(25, 90);
        this.fontSizeContextStrip.Name = "contextMenuStrip1";
        this.fontSizeContextStrip.RightToLeft = System.Windows.Forms.RightToLeft.No;
        this.fontSizeContextStrip.Size = new System.Drawing.Size(97, 70);
        this.hugeToolStripMenuItem.Text = "Huge";
        this.hugeToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);
        this.normalToolStripMenuItem.Text = "Normal";
        this.normalToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);
        this.tinyToolStripMenuItem.Text = "Tiny";
        this.tinyToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(300, 145);
        this.ContextMenuStrip = this.fontSizeContextStrip;
        this.Controls.Add(this.mainMenuStrip);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainWindow_Paint);
        this.mainMenuStrip.ResumeLayout(false);
        this.fontSizeContextStrip.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.MenuStrip mainMenuStrip;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem changeBackgroundColorToolStripMenuItem;
    private System.Windows.Forms.ToolStripTextBox toolStripTextBoxColor;
    private System.Windows.Forms.ContextMenuStrip fontSizeContextStrip;
    private System.Windows.Forms.ToolStripMenuItem hugeToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem normalToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem tinyToolStripMenuItem;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new MainWindow());
    }
}