c#之menustrip_toolstripmenuitem小記
//如下示例程式碼會顯示一個選單,共有4個小選單項,每個選單項各對應4個選單項
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication6
{
class Form5 : Form
{
public Form5()
{
// Size the form. to show three wide menu items.
this.Width = 500;
//標題欄
this.Text = "ToolStripContextMenuStrip: Image and Check Margins";
// Create a new MenuStrip control.
//選單容器物件
MenuStrip ms = new MenuStrip();
// Create the ToolStripMenuItems for the MenuStrip control.
//選單項物件
ToolStripMenuItem bothMargins = new ToolStripMenuItem("BothMargins");
ToolStripMenuItem imageMarginOnly = new ToolStripMenuItem("ImageMargin");
ToolStripMenuItem checkMarginOnly = new ToolStripMenuItem("CheckMargin");
ToolStripMenuItem noMargins = new ToolStripMenuItem("NoMargins");
// Customize the DropDowns menus.
// This ToolStripMenuItem has an image margin
// and a check margin.
//dropdown事件
bothMargins.DropDown = CreateCheckImageContextMenuStrip();
//contextmenustrip類的學習
((ContextMenuStrip)bothMargins.DropDown).ShowImageMargin = true;
((ContextMenuStrip)bothMargins.DropDown).ShowCheckMargin = true;
// This ToolStripMenuItem has only an image margin.
imageMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)imageMarginOnly.DropDown).ShowImageMargin = true;
((ContextMenuStrip)imageMarginOnly.DropDown).ShowCheckMargin = false;
// This ToolStripMenuItem has only a check margin.
checkMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)checkMarginOnly.DropDown).ShowImageMargin = false;
((ContextMenuStrip)checkMarginOnly.DropDown).ShowCheckMargin = true;
// This ToolStripMenuItem has no image and no check margin.
noMargins.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)noMargins.DropDown).ShowImageMargin = false;
((ContextMenuStrip)noMargins.DropDown).ShowCheckMargin = false;
// Populate the MenuStrip control with the ToolStripMenuItems.
//透過menustrip.items.add用選單窗器類新增子物件 子選單項 ToolStripMenuItem
ms.Items.Add(bothMargins);
ms.Items.Add(imageMarginOnly);
ms.Items.Add(checkMarginOnly);
ms.Items.Add(noMargins);
// Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top;
// Add the MenuStrip control to the controls collection last.
// This is important for correct placement in the z-order.
//把選單容器物件menustrip新增到窗體中 form.controls.add
this.Controls.Add(ms);
}
// This utility method creates a Bitmap for use in
// a ToolStripMenuItem's image margin.
internal Bitmap CreateSampleBitmap()
{
// The Bitmap is a smiley face.
Bitmap sampleBitmap = new Bitmap(32, 32);
Graphics g = Graphics.FromImage(sampleBitmap);
//using的用法在此何義
using (Pen p = new Pen(ProfessionalColors.ButtonPressedBorder))
{
// Set the Pen width.
p.Width = 4;
// Set up the mouth geometry.
Point[] curvePoints = new Point[]{
new Point(4,14),
new Point(16,24),
new Point(28,14)};
// Draw the mouth.
g.DrawCurve(p, curvePoints);
// Draw the eyes.
g.DrawEllipse(p, new Rectangle(new Point(7, 4), new Size(3, 3)));
g.DrawEllipse(p, new Rectangle(new Point(22, 4), new Size(3, 3)));
}
return sampleBitmap;
}
// This utility method creates a ContextMenuStrip control
// that has four ToolStripMenuItems showing the four
// possible combinations of image and check margins.
internal ContextMenuStrip CreateCheckImageContextMenuStrip()
{
// Create a new ContextMenuStrip control.
ContextMenuStrip checkImageContextMenuStrip = new ContextMenuStrip();
// Create a ToolStripMenuItem with a
// check margin and an image margin.
ToolStripMenuItem yesCheckYesImage =
new ToolStripMenuItem("Check, Image");
yesCheckYesImage.Checked = true;
yesCheckYesImage.Image = CreateSampleBitmap();
// Create a ToolStripMenuItem with no
// check margin and with an image margin.
ToolStripMenuItem noCheckYesImage =
new ToolStripMenuItem("No Check, Image");
noCheckYesImage.Checked = false;
noCheckYesImage.Image = CreateSampleBitmap();
// Create a ToolStripMenuItem with a
// check margin and without an image margin.
ToolStripMenuItem yesCheckNoImage =
new ToolStripMenuItem("Check, No Image");
yesCheckNoImage.Checked = true;
// Create a ToolStripMenuItem with no
// check margin and no image margin.
ToolStripMenuItem noCheckNoImage =
new ToolStripMenuItem("No Check, No Image");
noCheckNoImage.Checked = false;
// Add the ToolStripMenuItems to the ContextMenuStrip control.
checkImageContextMenuStrip.Items.Add(yesCheckYesImage);
checkImageContextMenuStrip.Items.Add(noCheckYesImage);
checkImageContextMenuStrip.Items.Add(yesCheckNoImage);
checkImageContextMenuStrip.Items.Add(noCheckNoImage);
return checkImageContextMenuStrip;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication6
{
class Form5 : Form
{
public Form5()
{
// Size the form. to show three wide menu items.
this.Width = 500;
//標題欄
this.Text = "ToolStripContextMenuStrip: Image and Check Margins";
// Create a new MenuStrip control.
//選單容器物件
MenuStrip ms = new MenuStrip();
// Create the ToolStripMenuItems for the MenuStrip control.
//選單項物件
ToolStripMenuItem bothMargins = new ToolStripMenuItem("BothMargins");
ToolStripMenuItem imageMarginOnly = new ToolStripMenuItem("ImageMargin");
ToolStripMenuItem checkMarginOnly = new ToolStripMenuItem("CheckMargin");
ToolStripMenuItem noMargins = new ToolStripMenuItem("NoMargins");
// Customize the DropDowns menus.
// This ToolStripMenuItem has an image margin
// and a check margin.
//dropdown事件
bothMargins.DropDown = CreateCheckImageContextMenuStrip();
//contextmenustrip類的學習
((ContextMenuStrip)bothMargins.DropDown).ShowImageMargin = true;
((ContextMenuStrip)bothMargins.DropDown).ShowCheckMargin = true;
// This ToolStripMenuItem has only an image margin.
imageMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)imageMarginOnly.DropDown).ShowImageMargin = true;
((ContextMenuStrip)imageMarginOnly.DropDown).ShowCheckMargin = false;
// This ToolStripMenuItem has only a check margin.
checkMarginOnly.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)checkMarginOnly.DropDown).ShowImageMargin = false;
((ContextMenuStrip)checkMarginOnly.DropDown).ShowCheckMargin = true;
// This ToolStripMenuItem has no image and no check margin.
noMargins.DropDown = CreateCheckImageContextMenuStrip();
((ContextMenuStrip)noMargins.DropDown).ShowImageMargin = false;
((ContextMenuStrip)noMargins.DropDown).ShowCheckMargin = false;
// Populate the MenuStrip control with the ToolStripMenuItems.
//透過menustrip.items.add用選單窗器類新增子物件 子選單項 ToolStripMenuItem
ms.Items.Add(bothMargins);
ms.Items.Add(imageMarginOnly);
ms.Items.Add(checkMarginOnly);
ms.Items.Add(noMargins);
// Dock the MenuStrip control to the top of the form.
ms.Dock = DockStyle.Top;
// Add the MenuStrip control to the controls collection last.
// This is important for correct placement in the z-order.
//把選單容器物件menustrip新增到窗體中 form.controls.add
this.Controls.Add(ms);
}
// This utility method creates a Bitmap for use in
// a ToolStripMenuItem's image margin.
internal Bitmap CreateSampleBitmap()
{
// The Bitmap is a smiley face.
Bitmap sampleBitmap = new Bitmap(32, 32);
Graphics g = Graphics.FromImage(sampleBitmap);
//using的用法在此何義
using (Pen p = new Pen(ProfessionalColors.ButtonPressedBorder))
{
// Set the Pen width.
p.Width = 4;
// Set up the mouth geometry.
Point[] curvePoints = new Point[]{
new Point(4,14),
new Point(16,24),
new Point(28,14)};
// Draw the mouth.
g.DrawCurve(p, curvePoints);
// Draw the eyes.
g.DrawEllipse(p, new Rectangle(new Point(7, 4), new Size(3, 3)));
g.DrawEllipse(p, new Rectangle(new Point(22, 4), new Size(3, 3)));
}
return sampleBitmap;
}
// This utility method creates a ContextMenuStrip control
// that has four ToolStripMenuItems showing the four
// possible combinations of image and check margins.
internal ContextMenuStrip CreateCheckImageContextMenuStrip()
{
// Create a new ContextMenuStrip control.
ContextMenuStrip checkImageContextMenuStrip = new ContextMenuStrip();
// Create a ToolStripMenuItem with a
// check margin and an image margin.
ToolStripMenuItem yesCheckYesImage =
new ToolStripMenuItem("Check, Image");
yesCheckYesImage.Checked = true;
yesCheckYesImage.Image = CreateSampleBitmap();
// Create a ToolStripMenuItem with no
// check margin and with an image margin.
ToolStripMenuItem noCheckYesImage =
new ToolStripMenuItem("No Check, Image");
noCheckYesImage.Checked = false;
noCheckYesImage.Image = CreateSampleBitmap();
// Create a ToolStripMenuItem with a
// check margin and without an image margin.
ToolStripMenuItem yesCheckNoImage =
new ToolStripMenuItem("Check, No Image");
yesCheckNoImage.Checked = true;
// Create a ToolStripMenuItem with no
// check margin and no image margin.
ToolStripMenuItem noCheckNoImage =
new ToolStripMenuItem("No Check, No Image");
noCheckNoImage.Checked = false;
// Add the ToolStripMenuItems to the ContextMenuStrip control.
checkImageContextMenuStrip.Items.Add(yesCheckYesImage);
checkImageContextMenuStrip.Items.Add(noCheckYesImage);
checkImageContextMenuStrip.Items.Add(yesCheckNoImage);
checkImageContextMenuStrip.Items.Add(noCheckNoImage);
return checkImageContextMenuStrip;
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-706041/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- c#之menustrip小記(1)C#
- c#之readonly小記C#
- c#之泛型小記C#泛型
- c#之word物件模型小記C#物件模型
- c#之tcbs struct(2)小記C#Struct
- c#之tcbs class的小記C#
- c#之引用office功能元件_小記C#元件
- c#之反射_Type_Fieldinfo[]小記C#反射
- c#之結構struct(2)_小記C#Struct
- c#之stream相關類小記C#
- c#之資料型別小測記C#資料型別
- c#之tcbs 建構函式小記C#函式
- c#之基類派生類base_小記C#
- c#之datagridview控制元件(1)_小記C#View控制元件
- c#之arraylist動態陣列小記(1)C#陣列
- C#小筆記C#筆記
- c#之基類派生類base_小記(2)C#
- c#之互操作性_(非)託管程式碼小記C#
- 【C#學習之辨析小總結】C#
- c#呼叫儲存過程小記C#儲存過程
- Dockerfile 小記之FROMDocker
- c#之tcbs靜態方法_返回值為類的型別_小記C#型別
- 小程式專案之填坑小記
- 工作小記之防火牆防火牆
- CSS 小結筆記之背景CSS筆記
- CSS 小結筆記之BFCCSS筆記
- CSS 小結筆記之emCSS筆記
- CSS 小結筆記之定位CSS筆記
- Dockerfile小記之ARG和ENVDocker
- 【記】《.net之美》之讀書筆記(二) C#中的泛型筆記C#泛型
- 【記】《.net之美》之讀書筆記(一) C#語言基礎筆記C#
- CSS 小結筆記之浮動CSS筆記
- oracle library cache之trace小記Oracle
- c#存取圖片小記(轉載)_from iDotNetSpaceC#
- CSS 小結筆記之盒子模型CSS筆記模型
- CSS 小結筆記之清除浮動CSS筆記
- CSS 小結筆記之選擇器CSS筆記
- 軟體除錯斷點之小記除錯斷點