//
//
//
//
【实验目的】
1.理解Graphics对象概念,并熟悉Graphics对象的创建方法。
2.掌握利用Graphics对象绘制线条和形状方法。
3.掌握利用Graphics对象的DrawString()呈现文本方法。
4.掌握利用Graphics对象DrawImage()显示图像方法。
//
//
//
//
//
//
【实验内容】
1、编写一个Windows应用程序,实现窗体自上而下,由白色到绿色渐变的背景。
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;
using System.Drawing.Drawing2D;
namespace test7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
Color.White, Color.Green, LinearGradientMode.Vertical
);
g.FillRectangle(brush, this.ClientRectangle);
g.Dispose();
}
}
}
//
/
////
///
2、编写一个Windows应用程序。当窗体加载后,呈现一个半径为100像素的蓝色圆。该圆在窗体范围内左右移动,并在圆内显示圆心相对于窗体的坐标。
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 test7
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Gray, 1);
int r = 80;
Random rd = new Random();
int X = rd.Next(0, this.ClientSize.Width - 2*r);
int Y = rd.Next(0, this.ClientSize.Height - 2*r);
int x1, x2;
x1 = X; x2 = Y;
g.DrawEllipse(pen, x1, x2, 2 * r, 2 * r);
SolidBrush brush = new SolidBrush(Color.Blue);
g.FillEllipse(brush, x1, x2, 2 * r, 2 * r);
label1.Location = new Point(x1 + r, x2 + r);
label1.Text = "x=" + (x1+r) + ",y=" +( x2+r);
}
private void Form2_Load(object sender, EventArgs e)
{
label1.BackColor = Color.Transparent;
label1.ForeColor = Color.White;
}
}
}
3.编写一个Windows应用程序。利用Graphics对象的DrawString方法在窗体上绘制文字“烟台大学”,要求文字用一幅图片填充。给出图片名称p1.jpg,大家也可以自行寻找其它图片。
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 test7
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Image image = Image.FromFile(Application.StartupPath + @"\p1.jpg");
TextureBrush brush = new TextureBrush(image);
Font font = new Font("黑体", 60, FontStyle.Underline ^ FontStyle.Bold);
g.DrawString("烟台大学", font, brush, new Point(10, 10));
g.Dispose();
}
}
}
//
//
//
//
4.画线实验。创建一个窗体,在窗体上添加一个按钮(按钮标题为“选择画笔颜色”)。窗体界面如下图所示。单击“选择画笔颜色”按钮,能够打开一个颜色对话框,选择颜色,进而更改画笔颜色。使用画笔可以在窗体上画任意的曲线。
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 test7
{
public partial class Form4 : Form
{
Pen pen = new Pen(Color.Blue);
Point point1, point2;
Graphics graphics = null;
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pen.Color = colorDialog1.Color;
}
private void Form4_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
point2 = e.Location;
}
private void Form4_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
point1 = point2;
point2 = e.Location;
graphics.DrawLine(pen, point1, point2);
}
}
private void Form4_Paint(object sender, PaintEventArgs e)
{
graphics = this.CreateGraphics();
}
private void Form4_Load(object sender, EventArgs e)
{
button1.Text = "选择画笔颜色";
}
}
}