📄 ch9_demoform001.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CH9.Properties;
namespace CH9
{
public partial class CH9_DemoForm001 : Form
{
public CH9_DemoForm001()
{
InitializeComponent();
}
private void CH5_DemoForm001_Load(object sender, EventArgs e)
{
// 设定状态列(StatusStrip)中名称为 tsbDateTime 之 ToolStripStatusLabel 控件的各个属性。
this.tsbDateTime.AutoToolTip = true;
this.tsbDateTime.Text = DateTime.Now.ToString();
this.tsbDateTime.Image = Resources.CLOCK02.ToBitmap();
this.tsbDateTime.TextAlign = ContentAlignment.MiddleRight;
this.tsbDateTime.ImageAlign = ContentAlignment.BottomRight;
this.tsbDateTime.TextImageRelation = TextImageRelation.TextBeforeImage;
this.tsbDateTime.Spring = true;
// 窗体预设的透明度是 100%,因此将该选项核取起来。
this.tsmiOnehundredPercent.Checked = true;
// 于菜单中建立额外的 ToolStripItem 控件。
CreateInitialMenus();
}
private void AddOption()
{
ToolStripMenuItem newOption = new ToolStripMenuItem();
// 将 Checked 属性设定成 False 以便使其不被核取。
newOption.Checked = false;
// 将 CheckOnClick 属性设定成 True,以便当使用者按一下此菜单项目时,它会自动在核取与不核取之间切换。
newOption.CheckOnClick = true;
// 设定新菜单项目的文字。
newOption.Text = "选项 " + (tsmiCheckedList.DropDownItems.Count - 2).ToString();
// 设定新菜单项目的图片。
newOption.Image = Resources.ACA16;
// 如果「核取清单」菜单的下拉菜单中只有「新增选项」、「移除选项」
// 与「分隔线」三个菜单项目,就将本次所新建立的菜单项目核取起来。
if (tsmiCheckedList.DropDownItems.Count == 3)
{
newOption.Checked = true;
}
// 设定当引发新建立之菜单项目的 Click、MouseEnter 与 MouseLeave 事件
// 时就执行特定的事件处理例程。
newOption.Click += new System.EventHandler(this.MenuOption_Click);
newOption.MouseEnter += new System.EventHandler(this.MenuItem_MouseEnter);
newOption.MouseLeave += new System.EventHandler(this.MenuItem_MouseLeave);
// 别忘了要将新建立的菜单项目加入「核取清单」菜单的下拉菜单中。
tsmiCheckedList.DropDownItems.Add(newOption);
}
private void CreateInitialMenus()
{
// 先加入三个菜单项目至「核取清单」菜单的下拉菜单中。
for (int i = 1; i <= 3; i++)
{
AddOption();
}
// 建立一个用来显示文字卷标的 ToolStripLabel 控件。
ToolStripLabel menuLabel = new ToolStripLabel();
menuLabel.Text = "请选取:";
// 将 ToolStripLabel 控件新增至 MenuStrip 控件的 Items 集合中,以便将它加至菜单中。
MenuStrip1.Items.Add(menuLabel);
// 建立一个用来显示下拉式清单方块的 ToolStripComboBox 控件。
ToolStripComboBox menuComboBox = new ToolStripComboBox();
menuComboBox.Items.Add("停靠上边界");
menuComboBox.Items.Add("停靠下边界");
menuComboBox.Items.Add("停靠左边界");
menuComboBox.Items.Add("停靠右边界");
menuComboBox.SelectedIndex = 0;
menuComboBox.ToolTipText = "选取菜单的停靠位置";
// 设定当选取 ToolStripComboBox 控件中的不同选项时就执行事件处理例程 ComboBox_SelectedIndexChanged。
menuComboBox.SelectedIndexChanged += new System.EventHandler(this.ComboBox_SelectedIndexChanged);
// 将 ToolStripComboBox 控件新增至 MenuStrip 控件的 Items 集合中,以便将它加至菜单中。
MenuStrip1.Items.Add(menuComboBox);
// 依序建立三个拥有按钮外观的 ToolStripButton 控件。
ToolStripButton button1 = new ToolStripButton();
ToolStripButton button2 = new ToolStripButton();
ToolStripButton button3 = new ToolStripButton();
// 依序设定这三个 ToolStripButton 控件的图片。
button1.Image = Resources.SampleImage1;
button2.Image = Resources.SampleImage2;
button3.Image = Resources.SampleImage3;
// 依序设定这三个 ToolStripButton 控件的工具提示文字。
button1.ToolTipText = "蓝色";
button2.ToolTipText = "红色";
button3.ToolTipText = "绿色";
// 依序设定这三个 ToolStripButton 控件的 Name 属性。
button1.Name = "Blue";
button2.Name = "Red";
button3.Name = "Green";
// 设定当引发这三个 ToolStripButton 控件的 Click 事件时就执行事件处理例程 ColorButton_Click。
button1.Click += new System.EventHandler(this.ColorButton_Click);
button2.Click += new System.EventHandler(this.ColorButton_Click);
button3.Click += new System.EventHandler(this.ColorButton_Click);
// 将这三个 ToolStripButton 控件新增至 MenuStrip 控件的 Items 集合中,以便将它加至菜单中。
MenuStrip1.Items.Add(button1);
MenuStrip1.Items.Add(button2);
MenuStrip1.Items.Add(button3);
// 建立一个结合按钮与下拉式清单的 ToolStripSplitButton 控件。
ToolStripSplitButton colorSplitButton = new ToolStripSplitButton();
// 设定 ToolStripSplitButton 控件的文字。
colorSplitButton.Text = "蓝色";
// 设定当按一下 ToolStripSplitButton 控件的标准按钮时就执行事件处理例程 SplitButton_Click。
colorSplitButton.ButtonClick += new System.EventHandler(this.SplitButton_Click);
// 建立三个 ToolStripMenuItem 对象。
ToolStripMenuItem tsmiColorOption1 = new ToolStripMenuItem();
ToolStripMenuItem tsmiColorOption2 = new ToolStripMenuItem();
ToolStripMenuItem tsmiColorOption3 = new ToolStripMenuItem();
// 设定这三个 ToolStripMenuItem 对象的文字。
tsmiColorOption1.Text = "蓝色";
tsmiColorOption2.Text = "红色";
tsmiColorOption3.Text = "绿色";
// 设定这三个 ToolStripMenuItem 对象的工具提示文字。
tsmiColorOption1.ToolTipText = "蓝色";
tsmiColorOption2.ToolTipText = "红色";
tsmiColorOption3.ToolTipText = "绿色";
// 设定这三个 ToolStripMenuItem 对象的名称。
tsmiColorOption1.Name = "Blue";
tsmiColorOption2.Name = "Red";
tsmiColorOption3.Name = "Green";
// 设定这三个 ToolStripMenuItem 对象的图片。
tsmiColorOption1.Image = Resources.SampleImage1;
tsmiColorOption2.Image = Resources.SampleImage2;
tsmiColorOption3.Image = Resources.SampleImage3;
// 设定当引发这三个 ToolStripMenuItem 对象的 Click 事件时就执行事件处理例程 DropDownColorItem_Click。
tsmiColorOption1.Click += new System.EventHandler(this.DropDownColorItem_Click);
tsmiColorOption2.Click += new System.EventHandler(this.DropDownColorItem_Click);
tsmiColorOption3.Click += new System.EventHandler(this.DropDownColorItem_Click);
// 将这三个 ToolStripMenuItem 对象新增至 ToolStripSplitButton 控件的 DropDownItems 集合中,以便使它们成为ToolStripSplitButton 控件的选项。
colorSplitButton.DropDownItems.Add(tsmiColorOption1);
colorSplitButton.DropDownItems.Add(tsmiColorOption2);
colorSplitButton.DropDownItems.Add(tsmiColorOption3);
// 将这三个 ToolStripSplitButton 控件新增至 MenuStrip 控件的 Items 集合中,以便将它加至菜单中。
MenuStrip1.Items.Add(colorSplitButton);
// 建立一个用来显示文字超级链接的 ToolStripLabel 控件。
ToolStripLabel myLinkLabel = new ToolStripLabel();
myLinkLabel.Text = "http://www.microsoft.com";
myLinkLabel.IsLink = true;
myLinkLabel.AutoToolTip = true;
// 设定当引发文字超级链接的 Click 事件时就执行事件处理例程 myLinkLabel_Click。
myLinkLabel.Click += new System.EventHandler(this.myLinkLabel_Click);
// 将文字超级链接新增至 MenuStrip 控件的 Items 集合中,以便将它加至菜单中。
MenuStrip1.Items.Add(myLinkLabel);
}
private void tsmiExit_Click(object sender, EventArgs e)
{
this.Close();
}
#region "事件处理函数"
private void MenuItem_MouseEnter(object sender, System.EventArgs e)
{
ToolStripMenuItem selectedItem = (ToolStripMenuItem)(sender);
tsbSelectedItem.Text = selectedItem.Text;
}
private void MenuItem_MouseLeave(object sender, System.EventArgs e)
{
tsbSelectedItem.Text = "";
}
private void tsmiNew_Click(object sender, EventArgs e)
{
this.BackColor = Color.White;
this.tsbDateTime.BackColor = System.Drawing.SystemColors.Control;
}
private void MenuOption_Click(object sender, EventArgs e)
{
foreach (object item in tsmiCheckedList.DropDownItems)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -