C#代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace paint1
{
public partial class Form1 : Form
{
private Graphics g;
//创建Graphics实例
private Rectangle rc;
//定义Rectangle全局变量
private void InitializeComponent()
{
this.button1=new System.Windows.Forms.Button();
this.button2=new System.Windows.Forms.Button();
this.button3=new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location=new System.Drawing.Point(36,182);
this.button1.Name="Button1";
this.button1.Size=new System.Drawing.Size(75,32);
this.button1.TabIndex=0;
this.button1.Text="画椭圆";
this.button1.Click+=new System.EventHandler(this.button1_Click);
this.button2.Location=new System.Drawing.Point(124,182);
this.button2.Name="Button2";
this.button2.Size=new System.Drawing.Size(75,32);
this.button2.TabIndex=1;
this.button2.Text="填充";
this.button2.Click+=new System.EventHandler(this.button2_Click);
this.button3.Location=new System.Drawing.Point(210,184);
this.button3.Name="Button3";
this.button3.Size=new System.Drawing.Size(75,32);
this.button3.TabIndex=2;
this.button3.Text="画字";
this.button3.Click+=new System.EventHandler(this.button3_Click);
this.AutoscaleBaseSize=new System.Drawing.Size(6,14);
this.ClientSize=new System.Drawing.Size(336,251);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox=false;
this.Name="Form1";
this.Text="Visual C#基本绘图方法";
this.ResumeLayout(false);
this.g=Graphics.FromHwnd(this.Handle);
//从指定的句柄中初始化Graphics实例
this.rc=new Rectangle(10,10,250,150);
//初始化Rectangle结构
}
private void button1_Click(object sender, EventArgs e)
{
Pen pn = new Pen(Color.Blue, 2);
//创建一个画笔
pn.Color = Color.Red;
//定义色彩
g.DrawEllipse(pn,rc);
}
private void button2_Click(object sender, EventArgs e)
{
LinearGradientBrush br = new LinearGradientBrush(rc,Color.Blue,Color.Black,LinearGradientMode.BackwardDiagonal);
//定义一个刷子
g.FillEllipse(br,rc);
//用定义的刷子填充指定区域
}
private void button3_Click(object sender, EventArgs e)
{
Font ft = new Font("宋体", 14);
//定义一个字体
SolidBrush br = new SolidBrush(Color.Yellow);
//定义一个刷子
g.DrawString("Hello World", ft, br, 50, 50);
//在程序窗体的(50,,50)区域,用给定的刷子,给定的字体及大小,绘制文本
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace paint1
{
public partial class Form1 : Form
{
private Graphics g;
//创建Graphics实例
private Rectangle rc;
//定义Rectangle全局变量
Button button1=new Button();
Button button2=new Button();
Button button3=new Button();
public Form1()
{
InitializeComponent();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(36, 182);
this.button1.Name = "Button1";
this.button1.Size = new System.Drawing.Size(75, 32);
this.button1.TabIndex = 0;
this.button1.Text = "画椭圆";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Location = new System.Drawing.Point(124, 182);
this.button2.Name = "Button2";
this.button2.Size = new System.Drawing.Size(75, 32);
this.button2.TabIndex = 1;
this.button2.Text = "填充";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button3.Location = new System.Drawing.Point(210, 184);
this.button3.Name = "Button3";
this.button3.Size = new System.Drawing.Size(75, 32);
this.button3.TabIndex = 2;
this.button3.Text = "画字";
this.button3.Click += new System.EventHandler(this.button3_Click);
//this.AutoscaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 251);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Visual C#基本绘图方法";
this.ResumeLayout(false);
this.g = Graphics.FromHwnd(this.Handle);
//从指定的句柄中初始化Graphics实例
this.rc = new Rectangle(10, 10, 250, 150);
//初始化Rectangle结构
}
private void button1_Click(object sender, EventArgs e)
{
Pen pn = new Pen(Color.Blue, 2);
//创建一个画笔
pn.Color = Color.Red;
//定义色彩
g.DrawEllipse(pn, rc);
}
private void button2_Click(object sender, EventArgs e)
{
LinearGradientBrush br = new LinearGradientBrush(rc, Color.Blue, Color.Black, LinearGradientMode.BackwardDiagonal);
//定义一个刷子
g.FillEllipse(br, rc);
//用定义的刷子填充指定区域
}
private void button3_Click(object sender, EventArgs e)
{
Font ft = new Font("宋体", 14);
//定义一个字体
SolidBrush br = new SolidBrush(Color.Yellow);
//定义一个刷子
g.DrawString("Hello World", ft, br, 50, 50);
//在程序窗体的(50,,50)区域,用给定的刷子,给定的字体及大小,绘制文本
}
}
}