1
using
System;
2 using System.Drawing;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using System.Data;
7
8 namespace DMenu
9 {
10 /**//// <summary>
11 /// 动态显示上下文菜单
12 /// </summary>
13 public class DMenuForm : System.Windows.Forms.Form
14 {
15 private System.Windows.Forms.CheckBox checkBox1;
16 private System.Windows.Forms.RadioButton radioButton1;
17 private System.Windows.Forms.ContextMenu contextMenu1;
18 private System.Windows.Forms.Label label1;
19 /**//// <summary>
20 /// 必需的设计器变量。
21 /// </summary>
22 private System.ComponentModel.Container components = null;
23
24 public DMenuForm()
25 {
26 //
27 // Windows 窗体设计器支持所必需的
28 //
29 InitializeComponent();
30
31 //
32 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
33 //
34 }
35
36 /**//// <summary>
37 /// 清理所有正在使用的资源。
38 /// </summary>
39 protected override void Dispose( bool disposing )
40 {
41 if( disposing )
42 {
43 if (components != null)
44 {
45 components.Dispose();
46 }
47 }
48 base.Dispose( disposing );
49 }
50
51 Windows Form Designer generated code#region Windows Form Designer generated code
52 /**//// <summary>
53 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
54 /// 此方法的内容。
55 /// </summary>
56 private void InitializeComponent()
57 {
58 this.checkBox1 = new System.Windows.Forms.CheckBox();
59 this.radioButton1 = new System.Windows.Forms.RadioButton();
60 this.contextMenu1 = new System.Windows.Forms.ContextMenu();
61 this.label1 = new System.Windows.Forms.Label();
62 this.SuspendLayout();
63 //
64 // checkBox1
65 //
66 this.checkBox1.ContextMenu = this.contextMenu1;
67 this.checkBox1.Location = new System.Drawing.Point(72, 88);
68 this.checkBox1.Name = "checkBox1";
69 this.checkBox1.TabIndex = 0;
70 this.checkBox1.Text = "checkBox1";
71 this.checkBox1.ThreeState = true;
72 //
73 // radioButton1
74 //
75 this.radioButton1.ContextMenu = this.contextMenu1;
76 this.radioButton1.Location = new System.Drawing.Point(72, 144);
77 this.radioButton1.Name = "radioButton1";
78 this.radioButton1.TabIndex = 1;
79 this.radioButton1.Text = "radioButton1";
80 //
81 // contextMenu1
82 //
83 this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);
84 //
85 // label1
86 //
87 this.label1.ContextMenu = this.contextMenu1;
88 this.label1.ForeColor = System.Drawing.SystemColors.WindowFrame;
89 this.label1.Location = new System.Drawing.Point(72, 32);
90 this.label1.Name = "label1";
91 this.label1.Size = new System.Drawing.Size(120, 23);
92 this.label1.TabIndex = 2;
93 this.label1.Text = "显示变化的字体";
94 //
95 // DMenuForm
96 //
97 this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
98 this.ClientSize = new System.Drawing.Size(568, 360);
99 this.Controls.AddRange(new System.Windows.Forms.Control[] {
100 this.label1,
101 this.radioButton1,
102 this.checkBox1});
103 this.MaximizeBox = false;
104 this.Name = "DMenuForm";
105 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
106 this.Text = "动态上下文菜单";
107 this.ResumeLayout(false);
108
109 }
110 #endregion
111
112 /**//// <summary>
113 /// 应用程序的主入口点。
114 /// </summary>
115 [STAThread]
116 static void Main()
117 {
118 Application.Run(new DMenuForm());
119 }
120
121 protected void RadioButton_Checked(System.Object sender, System.EventArgs e)
122 {
123 // 处理单选框被选中的菜单命令
124 if (contextMenu1.SourceControl == radioButton1)
125 radioButton1.Checked = true;
126 }
127 protected void RadioButton_Unchecked(System.Object sender, System.EventArgs e)
128 {
129 // 处理单选框不被选中的菜单命令
130 if (contextMenu1.SourceControl == radioButton1)
131 radioButton1.Checked = false;
132 }
133 protected void CheckBox_Checked(System.Object sender, System.EventArgs e)
134 {
135 // 处理复选框被选中的菜单命令
136 if (contextMenu1.SourceControl == checkBox1)
137 checkBox1.Checked = true;
138 }
139 protected void CheckBox_Unchecked(System.Object sender, System.EventArgs e)
140 {
141 // 处理复选框不被选中的菜单命令
142 if (contextMenu1.SourceControl == checkBox1)
143 checkBox1.Checked = false;
144 }
145 protected void CheckBox_Indeterminate(System.Object sender, System.EventArgs e)
146 {
147 // 处理复选框选择不确定的菜单命令
148 if (contextMenu1.SourceControl == checkBox1)
149 checkBox1.CheckState = System.Windows.Forms.CheckState.Indeterminate;
150 }
151 protected void Label_Bold(System.Object sender, System.EventArgs e)
152 {
153 // 处理标签选择粗体的菜单命令
154 if (contextMenu1.SourceControl == label1)
155 {
156 // 生成新的字体类型
157 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
158 label1.Font.Size, System.Drawing.FontStyle.Bold);
159 // 标签采用新的字体类型进行显示
160 label1.Font = newFont;
161 }
162 }
163 protected void Label_Italic(System.Object sender, System.EventArgs e)
164 {
165 // 处理标签选择斜体的菜单命令
166 if (contextMenu1.SourceControl == label1)
167 {
168 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
169 label1.Font.Size, System.Drawing.FontStyle.Italic);
170 label1.Font = newFont;
171 }
172 }
173 protected void Label_Regular(System.Object sender, System.EventArgs e)
174 {
175 // 处理标签选择正常字体的菜单命令
176 if (contextMenu1.SourceControl == label1)
177 {
178 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
179 label1.Font.Size, System.Drawing.FontStyle.Regular);
180 label1.Font = newFont;
181 }
182 }
183 protected void Label_Strikeout(System.Object sender, System.EventArgs e)
184 {
185 // 处理标签选择中间有直线通过的字体的菜单命令
186 if (contextMenu1.SourceControl == label1)
187 {
188 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
189 label1.Font.Size, System.Drawing.FontStyle.Strikeout);
190 label1.Font = newFont;
191 }
192 }
193 protected void Label_Underline(System.Object sender, System.EventArgs e)
194 {
195 // 处理标签选择带下划线的字体的菜单命令
196 if (contextMenu1.SourceControl == label1)
197 {
198 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
199 label1.Font.Size, System.Drawing.FontStyle.Underline);
200 label1.Font = newFont;
201 }
202 }
203 private void contextMenu1_Popup(object sender, System.EventArgs e)
204 {
205 // 清空菜单项。
206 contextMenu1.MenuItems.Clear();
207
208 // 根据当前选择的组件动态的生成上下文菜单。
209 if (contextMenu1.SourceControl == checkBox1)
210 {
211 // 添加复选框“选中”菜单项。
212 contextMenu1.MenuItems.Add("选中",new System.EventHandler(this.CheckBox_Checked));
213 // 添加复选框“不选中”菜单项。
214 contextMenu1.MenuItems.Add("不选中",new System.EventHandler(this.CheckBox_Unchecked));
215 // 添加复选框“不确定”菜单项。
216 contextMenu1.MenuItems.Add("不确定", new System.EventHandler(this.CheckBox_Indeterminate));
217 }
218 else
219 {
220 if (contextMenu1.SourceControl == radioButton1)
221 {
222 // 添加单选框“选中”菜单项。
223 contextMenu1.MenuItems.Add("选中",new System.EventHandler(this.RadioButton_Checked));
224 // 添加单选框“不选中”菜单项。
225 contextMenu1.MenuItems.Add("不选中",new System.EventHandler(this.RadioButton_Unchecked));
226 }
227 else
228 {
229 // 添加标签“粗体字”菜单项。
230 contextMenu1.MenuItems.Add("粗体字",new System.EventHandler(this.Label_Bold));
231 // 添加标签“斜体字”菜单项。
232 contextMenu1.MenuItems.Add("斜体字",new System.EventHandler(this.Label_Italic));
233 // 添加标签“普通字”菜单项。
234 contextMenu1.MenuItems.Add("普通字",new System.EventHandler(this.Label_Regular));
235 // 添加标签“中间有直线通过的字”菜单项。
236 contextMenu1.MenuItems.Add("中间有直线通过的字",new System.EventHandler(this.Label_Strikeout));
237 // 添加标签“带下划线的字”菜单项。
238 contextMenu1.MenuItems.Add("带下划线的字",new System.EventHandler(this.Label_Underline));
239 }
240 }
241 }
242 }
243}
244
2 using System.Drawing;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using System.Data;
7
8 namespace DMenu
9 {
10 /**//// <summary>
11 /// 动态显示上下文菜单
12 /// </summary>
13 public class DMenuForm : System.Windows.Forms.Form
14 {
15 private System.Windows.Forms.CheckBox checkBox1;
16 private System.Windows.Forms.RadioButton radioButton1;
17 private System.Windows.Forms.ContextMenu contextMenu1;
18 private System.Windows.Forms.Label label1;
19 /**//// <summary>
20 /// 必需的设计器变量。
21 /// </summary>
22 private System.ComponentModel.Container components = null;
23
24 public DMenuForm()
25 {
26 //
27 // Windows 窗体设计器支持所必需的
28 //
29 InitializeComponent();
30
31 //
32 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
33 //
34 }
35
36 /**//// <summary>
37 /// 清理所有正在使用的资源。
38 /// </summary>
39 protected override void Dispose( bool disposing )
40 {
41 if( disposing )
42 {
43 if (components != null)
44 {
45 components.Dispose();
46 }
47 }
48 base.Dispose( disposing );
49 }
50
51 Windows Form Designer generated code#region Windows Form Designer generated code
52 /**//// <summary>
53 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
54 /// 此方法的内容。
55 /// </summary>
56 private void InitializeComponent()
57 {
58 this.checkBox1 = new System.Windows.Forms.CheckBox();
59 this.radioButton1 = new System.Windows.Forms.RadioButton();
60 this.contextMenu1 = new System.Windows.Forms.ContextMenu();
61 this.label1 = new System.Windows.Forms.Label();
62 this.SuspendLayout();
63 //
64 // checkBox1
65 //
66 this.checkBox1.ContextMenu = this.contextMenu1;
67 this.checkBox1.Location = new System.Drawing.Point(72, 88);
68 this.checkBox1.Name = "checkBox1";
69 this.checkBox1.TabIndex = 0;
70 this.checkBox1.Text = "checkBox1";
71 this.checkBox1.ThreeState = true;
72 //
73 // radioButton1
74 //
75 this.radioButton1.ContextMenu = this.contextMenu1;
76 this.radioButton1.Location = new System.Drawing.Point(72, 144);
77 this.radioButton1.Name = "radioButton1";
78 this.radioButton1.TabIndex = 1;
79 this.radioButton1.Text = "radioButton1";
80 //
81 // contextMenu1
82 //
83 this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);
84 //
85 // label1
86 //
87 this.label1.ContextMenu = this.contextMenu1;
88 this.label1.ForeColor = System.Drawing.SystemColors.WindowFrame;
89 this.label1.Location = new System.Drawing.Point(72, 32);
90 this.label1.Name = "label1";
91 this.label1.Size = new System.Drawing.Size(120, 23);
92 this.label1.TabIndex = 2;
93 this.label1.Text = "显示变化的字体";
94 //
95 // DMenuForm
96 //
97 this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
98 this.ClientSize = new System.Drawing.Size(568, 360);
99 this.Controls.AddRange(new System.Windows.Forms.Control[] {
100 this.label1,
101 this.radioButton1,
102 this.checkBox1});
103 this.MaximizeBox = false;
104 this.Name = "DMenuForm";
105 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
106 this.Text = "动态上下文菜单";
107 this.ResumeLayout(false);
108
109 }
110 #endregion
111
112 /**//// <summary>
113 /// 应用程序的主入口点。
114 /// </summary>
115 [STAThread]
116 static void Main()
117 {
118 Application.Run(new DMenuForm());
119 }
120
121 protected void RadioButton_Checked(System.Object sender, System.EventArgs e)
122 {
123 // 处理单选框被选中的菜单命令
124 if (contextMenu1.SourceControl == radioButton1)
125 radioButton1.Checked = true;
126 }
127 protected void RadioButton_Unchecked(System.Object sender, System.EventArgs e)
128 {
129 // 处理单选框不被选中的菜单命令
130 if (contextMenu1.SourceControl == radioButton1)
131 radioButton1.Checked = false;
132 }
133 protected void CheckBox_Checked(System.Object sender, System.EventArgs e)
134 {
135 // 处理复选框被选中的菜单命令
136 if (contextMenu1.SourceControl == checkBox1)
137 checkBox1.Checked = true;
138 }
139 protected void CheckBox_Unchecked(System.Object sender, System.EventArgs e)
140 {
141 // 处理复选框不被选中的菜单命令
142 if (contextMenu1.SourceControl == checkBox1)
143 checkBox1.Checked = false;
144 }
145 protected void CheckBox_Indeterminate(System.Object sender, System.EventArgs e)
146 {
147 // 处理复选框选择不确定的菜单命令
148 if (contextMenu1.SourceControl == checkBox1)
149 checkBox1.CheckState = System.Windows.Forms.CheckState.Indeterminate;
150 }
151 protected void Label_Bold(System.Object sender, System.EventArgs e)
152 {
153 // 处理标签选择粗体的菜单命令
154 if (contextMenu1.SourceControl == label1)
155 {
156 // 生成新的字体类型
157 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
158 label1.Font.Size, System.Drawing.FontStyle.Bold);
159 // 标签采用新的字体类型进行显示
160 label1.Font = newFont;
161 }
162 }
163 protected void Label_Italic(System.Object sender, System.EventArgs e)
164 {
165 // 处理标签选择斜体的菜单命令
166 if (contextMenu1.SourceControl == label1)
167 {
168 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
169 label1.Font.Size, System.Drawing.FontStyle.Italic);
170 label1.Font = newFont;
171 }
172 }
173 protected void Label_Regular(System.Object sender, System.EventArgs e)
174 {
175 // 处理标签选择正常字体的菜单命令
176 if (contextMenu1.SourceControl == label1)
177 {
178 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
179 label1.Font.Size, System.Drawing.FontStyle.Regular);
180 label1.Font = newFont;
181 }
182 }
183 protected void Label_Strikeout(System.Object sender, System.EventArgs e)
184 {
185 // 处理标签选择中间有直线通过的字体的菜单命令
186 if (contextMenu1.SourceControl == label1)
187 {
188 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
189 label1.Font.Size, System.Drawing.FontStyle.Strikeout);
190 label1.Font = newFont;
191 }
192 }
193 protected void Label_Underline(System.Object sender, System.EventArgs e)
194 {
195 // 处理标签选择带下划线的字体的菜单命令
196 if (contextMenu1.SourceControl == label1)
197 {
198 Font newFont = new System.Drawing.Font(label1.Font.FontFamily,
199 label1.Font.Size, System.Drawing.FontStyle.Underline);
200 label1.Font = newFont;
201 }
202 }
203 private void contextMenu1_Popup(object sender, System.EventArgs e)
204 {
205 // 清空菜单项。
206 contextMenu1.MenuItems.Clear();
207
208 // 根据当前选择的组件动态的生成上下文菜单。
209 if (contextMenu1.SourceControl == checkBox1)
210 {
211 // 添加复选框“选中”菜单项。
212 contextMenu1.MenuItems.Add("选中",new System.EventHandler(this.CheckBox_Checked));
213 // 添加复选框“不选中”菜单项。
214 contextMenu1.MenuItems.Add("不选中",new System.EventHandler(this.CheckBox_Unchecked));
215 // 添加复选框“不确定”菜单项。
216 contextMenu1.MenuItems.Add("不确定", new System.EventHandler(this.CheckBox_Indeterminate));
217 }
218 else
219 {
220 if (contextMenu1.SourceControl == radioButton1)
221 {
222 // 添加单选框“选中”菜单项。
223 contextMenu1.MenuItems.Add("选中",new System.EventHandler(this.RadioButton_Checked));
224 // 添加单选框“不选中”菜单项。
225 contextMenu1.MenuItems.Add("不选中",new System.EventHandler(this.RadioButton_Unchecked));
226 }
227 else
228 {
229 // 添加标签“粗体字”菜单项。
230 contextMenu1.MenuItems.Add("粗体字",new System.EventHandler(this.Label_Bold));
231 // 添加标签“斜体字”菜单项。
232 contextMenu1.MenuItems.Add("斜体字",new System.EventHandler(this.Label_Italic));
233 // 添加标签“普通字”菜单项。
234 contextMenu1.MenuItems.Add("普通字",new System.EventHandler(this.Label_Regular));
235 // 添加标签“中间有直线通过的字”菜单项。
236 contextMenu1.MenuItems.Add("中间有直线通过的字",new System.EventHandler(this.Label_Strikeout));
237 // 添加标签“带下划线的字”菜单项。
238 contextMenu1.MenuItems.Add("带下划线的字",new System.EventHandler(this.Label_Underline));
239 }
240 }
241 }
242 }
243}
244