using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 使用GDI绘制简单的图形
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 画图需要:一根笔(Pen),一张纸(窗体),一个人(对象),颜色(Brush),定位的点(Point)
// 画一条直线
private void button1_Click(object sender, EventArgs e)
{
// 创建一个GDI对象
Graphics g = this.CreateGraphics();
// 当窗体进入电脑屏幕边缘,则图形消失。这是因为,当窗体移出屏幕时,窗体会被擦除并重绘,
// 而窗体上的图形并没有进行重绘。
g.DrawLine(new Pen(Brushes.Red), new Point(60, 60), new Point(300, 300));
}
int i = 0;
// 当窗体重绘时触发该事件
private void Form1_Paint(object sender, PaintEventArgs e)
{
i++;
textBox1.Text = i.ToString(); // 重绘次数。
// 在该事件中画的图形,会因窗体重绘而重绘。
Graphics g = this.CreateGraphics();
g.DrawLine(new Pen(Brushes.Red), new Point(60, 60), new Point(300, 300));
}
// 画一个文本
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawString("Hello World", new Font("宋体", 30), Brushes.Purple, new Point(200, 200));
}
}
}
C#学习笔记之使用GDI绘制简单的图形
最新推荐文章于 2019-12-02 17:12:39 发布