1.绘制带立体感的圆柱
public partial class Form1 : Form { public Form1() { InitializeComponent(); // ControlStyles 指定控件的样式和行为。 // SetStyle将指定的 ControlStyles 标志设置为 true 或 false。 this.SetStyle(ControlStyles.OptimizedDoubleBuffer |//OptimizedDoubleBuffer如果为 true,则该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁。 如果将此属性设置为 true,则还应当将 AllPaintingInWmPaint 设置为 true。 ControlStyles.ResizeRedraw |//ResizeRedraw如果为 true,则在调整控件大小时重绘控件。 ControlStyles.AllPaintingInWmPaint, true);//AllPaintingInWmPaint如果为 true,控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁。 仅当 UserPaint 位设置为 true 时,才应当应用该样式 } private void Form1_Load(object sender, EventArgs e) { } private void Form1_Paint(object sender, PaintEventArgs e) { DrawVertPipe(e.Graphics); //Graphics g = pictureBox1.CreateGraphics(); } //GraphicsContainer表示图形容器的内部数据。 当使用 BeginContainer 和 EndContainer 方法保存 Graphics 对象的状态时使用此类。 此类不能被继承。 //BeginContainer保存具有此 Graphics 的当前状态的图形容器,然后打开并使用新的图形容器。 //EndContainer关闭当前图形容器,并将此 Graphics 的状态还原到通过调用 BeginContainer 方法保存的状态。 private void DrawVertPipe(System.Drawing.Graphics g) { System.Drawing.Drawing2D.GraphicsContainer gCon; gCon = g.BeginContainer(); Rectangle bound = new Rectangle(150, 50, 250, 250); GraphicsPath gp = new GraphicsPath(); //指定是否将平滑处理(抗锯齿)应用于直线、曲线和已填充区域的边缘。AntiAlias指定抗锯齿的呈现。 g.SmoothingMode = SmoothingMode.AntiAlias; gp.AddRectangle(bound); g.FillPath(new SolidBrush(Color.Violet), gp); FillCylinderShadow(g, gp, 0.5f, Color.FromArgb(100, 0, 0, 0), Color.FromArgb(100, Color.White)); gp.Dispose(); g.EndContainer(gCon); } public void FillCylinderShadow(Graphics g, GraphicsPath gp, float focus, Color BeginColor, Color EndColor) { LinearGradientBrush linGrBrush = new LinearGradientBrush( gp.PathPoints[0], gp.PathPoints[1], BeginColor, EndColor ); //创建基于钟形曲线的渐变过渡过程。 linGrBrush.SetSigmaBellShape(focus);//介于 0 到 1 之间的一个值,它指定渐变中心(起始色和结束色均匀混合的点)。 g.FillPath(linGrBrush, gp); linGrBrush.Dispose(); } }
注:
Graphics.BeginContainer 方法与 Graphics.EndContainer 方法
图形容器保留图形状态(如变形、剪辑区域和呈现属性)。
BeginContainer 方法调用时的状态。
BeginContainer 方法调用之后放在该堆栈上的所有信息块也会被移除。
Save 方法调用也成对出现。
BeginContainer 方法)放在堆栈上的所有信息块都从堆栈中被移除。
BeginContainer 方法建立的图形状态包括默认图形状态的呈现质量;调用方法时存在的任何呈现质量状态更改都会重置为默认值。
2.窗体绘制网格
public partial class Form3 : Form { Bitmap bit; TextureBrush textureBrush; int x = 0; int y = 0; bool showRec = false; public Form3() { InitializeComponent(); bit = new Bitmap(10, 10); Graphics g = Graphics.FromImage(bit); g.Clear(this.BackColor); g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 10, 10)); g.Dispose(); textureBrush = new TextureBrush(bit);//使用TextureBrush可以有效减少窗体拉伸时的闪烁 } private void Form3_Paint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(textureBrush, this.ClientRectangle); if (showRec) { e.Graphics.DrawRectangle(Pens.White, new Rectangle(x * 10, y * 10, 10, 10)); } } private void Form3_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right)//鼠标右键 { x = e.X / 10; y = e.Y / 10; showRec = true; this.Invalidate(); MessageBox.Show("第" + x + "×" + y + "个矩形 " + "坐标为:(" + e.X + "," + e.Y + ")"); showRec = false; this.Invalidate(); } } }
3.把窗体转成图像文件
//Screen.AllScreens获取系统上所有显示器的数组。 //Screen.Bounds获取显示的边界。 Rectangle rect = Screen.AllScreens[0].Bounds; Image image = new Bitmap(rect.Width, rect.Height); Graphics g = Graphics.FromImage(image); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(rect.Width, rect.Height)); Rectangle rect2 = this.Bounds; Image image2 = new Bitmap(rect2.Width, rect2.Height); Graphics g2 = Graphics.FromImage(image2); g2.DrawImage(image, new Rectangle(0, 0, rect2.Width, rect2.Height), rect2, GraphicsUnit.Pixel); image2.Save(@"C:\form.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
4.绘制条形图
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = this.CreateGraphics(); //绘制X轴 g.DrawLine(new Pen(Color.Black), 50, 50, 50, 500); g.DrawString("(年份)", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(50, 30)); //绘制Y轴 g.DrawLine(new Pen(Color.Black), 50, 50, 700, 50); g.DrawString("(产量)", new Font("宋体", 10), new SolidBrush(Color.Black), new Point(10, 50)); //绘制年份 int startYear = 2002; int posNum = 100; for (int i = 0; i < 6; i++) { string strYear = startYear.ToString(); g.DrawString(strYear, new Font("宋体", 12), new SolidBrush(Color.Black), new Point(posNum * (i + 1), 30)); startYear = startYear + 1; } //绘制产量 float startNum = 1000; for (int i = 0; i < 4; i++) { string strNum = startNum.ToString(); g.DrawString(strNum, new Font("宋体", 12), new SolidBrush(Color.Black), new Point(10, posNum * (i + 1))); startNum = startNum + 1000; } //绘制矩形以表示产量 int drawPosNum = 80; for (int i = 0; i < 6; i++) { Random rd = new Random(); //绘制红色矩形 float flNumber1 = rd.Next(0, 400); g.DrawRectangle(new Pen(Color.Red), drawPosNum, 50, 20, flNumber1); SolidBrush sb1 = new SolidBrush(Color.Red); g.FillRectangle(sb1, drawPosNum, 50, 20, flNumber1); //绘制蓝色矩形 float flNumber2 = rd.Next(0, 400); g.DrawRectangle(new Pen(Color.Blue), (drawPosNum + 25), 50, 20, flNumber2); SolidBrush sb2 = new SolidBrush(Color.Blue); g.FillRectangle(sb2, (drawPosNum + 25), 50, 20, flNumber2); //绘制绿色矩形 float flNumber3 = rd.Next(0, 400); g.DrawRectangle(new Pen(Color.Pink), (drawPosNum + 50), 50, 20, flNumber3); SolidBrush sb3 = new SolidBrush(Color.Green); g.FillRectangle(sb3, (drawPosNum + 50), 50, 20, flNumber3); drawPosNum = drawPosNum + 100; } }
5.绘制饼形图
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = this.CreateGraphics(); int x = 100, y = 30, width = 200, height = 200, sum = 0 ; float sweepAngle = 0.0f,startAngle = 0.0f; Pen p = null; SolidBrush sb = null; int[,] strYearInfo = new int[,] { { 2006, 1500 }, { 2007, 2000 }, { 2008, 1200 } }; for (int i = 0; i < strYearInfo.Length / 2; i++) { sum += strYearInfo[i, 1]; } for (int i = 0; i < strYearInfo.Length / 2; i++) { //定义组成饼形图的各个扇形的颜色 if (i == 0) { p = new Pen(Color.Red); sb = new SolidBrush(Color.Red); } else if (i == 1) { p = new Pen(Color.Blue); sb = new SolidBrush(Color.Blue); } else if (i == 2) { p = new Pen(Color.Green); sb = new SolidBrush(Color.Green); } //根据值绘制各个饼形 float flYearNumber = float.Parse(strYearInfo[i, 1].ToString()); sweepAngle = 360 * (flYearNumber / sum); Rectangle rtl = new Rectangle(x, y, width, height); g.DrawPie(p, rtl, startAngle, sweepAngle); g.FillPie(sb, rtl, startAngle, sweepAngle); startAngle += sweepAngle; //绘制饼形图的说明 string strYear = strYearInfo[i, 0].ToString() + "所占比例:" + (flYearNumber / sum) * 100 + "%"; Font font = new Font("宋体", 12, FontStyle.Bold); Point point = new Point(350, (40 + i * 30)); g.DrawString(strYear, font, sb, point); } }
6.颜色对话框
private void button1_Click(object sender, EventArgs e) { using (System.Windows.Forms.ColorDialog cdlg = new ColorDialog()) { cdlg.Color = Color.Blue; if (cdlg.ShowDialog() == DialogResult.OK) { this.BackColor = cdlg.Color; } } }
7.字体选择
FontDialog fd1 = new FontDialog(); fd1.ShowDialog(); //this.richTextBox1.SelectionFont = fd1.Font; this.richTextBox1.Font = fd1.Font;
注:以上代码收集自网络!!!