完成图示程序:通过字体菜单选择字体,通过颜色菜单选择颜色,在窗体中画图并输出文字说明。画图菜单包括两个菜单项:画线、画圆。
具体如图所示:

具体代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Color pencolor ; Font fontSelect; private void 画圆ToolStripMenuItem_Click(object sender, EventArgs e) { using (Graphics g = this.CreateGraphics()) { g.Clear(this.BackColor); Pen p = new Pen(pencolor, 3); g.DrawEllipse(p, 90, 70, 100, 100); g.DrawString("圆", fontSelect, new SolidBrush(pencolor), 110, 200); } } private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog ColorDialog1 = new ColorDialog(); if (ColorDialog1.ShowDialog() == DialogResult.OK) { pencolor = ColorDialog1.Color; } } private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) { FontDialog fontdialog1 = new FontDialog(); if(fontdialog1.ShowDialog() == DialogResult.OK) { fontSelect = fontdialog1.Font; } } private void 画线ToolStripMenuItem_Click(object sender, EventArgs e) { using (Graphics g = this.CreateGraphics()) { g.Clear(this.BackColor); Pen p = new Pen(pencolor, 2); g.DrawLine(p, 20, 60, 250, 60); g.DrawString("线", fontSelect, new SolidBrush(pencolor), 110, 200); } } } } |
测试结果:

