动态显示上下文菜单
1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7
8namespace 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
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
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7
8namespace 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
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