單擊時分別在窗體上畫一條直線、畫一個形狀和畫一個文字。其執行介面如下圖所示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 實驗_10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics huazhixian = this.CreateGraphics();
int x, y, h, w;
x=160;
y=170;
h=180;
w=100;
huazhixian.DrawLine(Pens.Red,x,y,x+h,y+w);
}
private void button2_Click(object sender, EventArgs e)
{
Graphics huaxingxian = this.CreateGraphics();
float x, y, r,w;
x = 200;
y = 200;
r = 150;
w = 150;
huaxingxian.DrawEllipse(Pens.Red,x,y,r,w);
}
private void button3_Click(object sender, EventArgs e)
{
Graphics huawenben = this.CreateGraphics();
StringFormat sf1= new StringFormat();
Font f = new Font("楷書",20,FontStyle.Bold);
SolidBrush g = new SolidBrush(Color.Red);
sf1.Alignment = StringAlignment.Near;
huawenben.DrawString("使用DrawString方法",f,g,200,200,sf1);
}
}
}