不使用OnPaint()进行绘制
作者:互联网
一些人在论坛上询问关于不使用OnPaint方法或事件进行绘画的问题。
为什么要使用onpaint函数呢?主要原因是我们可以轻易地使用paint函数得到Graphics对象。实际上,不使用onpaint函数进行绘制是非常简单的。只需要在正在绘制的窗体中得到Graphics对象即可。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace DrawWithNoPaintDemo 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void radioButton1_CheckedChanged(object sender, EventArgs e) 20 { 21 22 } 23 24 private void button1_Click(object sender, EventArgs e) 25 { 26 Graphics grfx = Graphics.FromHwnd(this.Handle); 27 //Graphics grfx = CreateGraphics(); 28 Pen pn = new Pen(Color.Blue,10); 29 SolidBrush br = new SolidBrush(Color.AliceBlue); 30 Rectangle rc = new Rectangle(200,50,200,200); 31 32 if (radioButton1.Checked) 33 { 34 pn.Color = Color.Red; 35 br.Color = Color.Red; 36 } 37 if (radioButton2.Checked) 38 { 39 pn.Color = Color.Green; 40 br.Color = Color.Green; 41 } 42 if (radioButton3.Checked) 43 { 44 pn.Color = Color.Blue; 45 br.Color = Color.Blue; 46 } 47 48 if (checkBox1.Checked) 49 { 50 grfx.FillEllipse(br,rc); 51 grfx.DrawEllipse(pn,rc); 52 } 53 if (checkBox2.Checked) 54 { 55 grfx.DrawRectangle(pn,rc); 56 } 57 if (checkBox3.Checked) 58 { 59 grfx.DrawString("Test String",new Font("Verdana",14),new SolidBrush(Color.Black),rc); 60 } 61 62 pn.Dispose(); 63 br.Dispose(); 64 } 65 } 66 }
标签:Checked,Color,pn,System,OnPaint,br,使用,using,绘制 来源: https://www.cnblogs.com/chenlight/p/16542100.html