📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace Example060_图形容器的应用
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Draw an ellipse centered at (100, 100).
Graphics myGraphics=e.Graphics;
Pen myBlackPen=new Pen(Color.Black);
myGraphics.TranslateTransform(100, 100);
myGraphics.DrawEllipse(myBlackPen, -40, -60, 80, 120);
// Draw the eye at the center of the ellipse.
DrawEye(myGraphics);
// Draw an ellipse centered at 200, 100.
myGraphics.TranslateTransform(100, 0, MatrixOrder.Append);
myGraphics.DrawEllipse(myBlackPen, -40, -60, 80, 120);
// Rotate the eye 40 degrees, and draw it 30 units above
// the center of the ellipse.
GraphicsContainer myGraphicsContainer = myGraphics.BeginContainer();
myGraphics.RotateTransform(-40);
myGraphics.TranslateTransform(0, -30, MatrixOrder.Append);
DrawEye(myGraphics);
myGraphics.EndContainer(myGraphicsContainer);
// Draw an ellipse centered at (300, 100).
myGraphics.TranslateTransform(100, 0, MatrixOrder.Append);
myGraphics.DrawEllipse(myBlackPen, -40, -60, 80, 120);
// Stretch and rotate the eye, and draw it at the
// center of the ellipse.
myGraphicsContainer = myGraphics.BeginContainer();
myGraphics.ScaleTransform(2, 1.5f);
myGraphics.RotateTransform(45, MatrixOrder.Append);
DrawEye(myGraphics);
myGraphics.EndContainer(myGraphicsContainer);
}
private void DrawEye(Graphics myGraphics)
{
GraphicsContainer eyeContainer;
eyeContainer = myGraphics.BeginContainer();
Pen myBlackPen = new Pen(Color.Black);
SolidBrush myGreenBrush = new SolidBrush(Color.Green);
SolidBrush myBlackBrush = new SolidBrush(Color.Black);
GraphicsPath myTopPath = new GraphicsPath();
myTopPath.AddEllipse(-30, -50, 60, 60);
GraphicsPath myBottomPath = new GraphicsPath();
myBottomPath.AddEllipse(-30, -10, 60, 60);
Region myTopRegion = new Region(myTopPath);
Region myBottomRegion = new Region(myBottomPath);
// Draw the outline of the eye.
// The outline of the eye consists of two ellipses.
// The top ellipse is clipped by the bottom ellipse, and
// the bottom ellipse is clipped by the top ellipse.
myGraphics.Clip = myTopRegion;
myGraphics.DrawPath(myBlackPen, myBottomPath);
myGraphics.Clip = myBottomRegion;
myGraphics.DrawPath(myBlackPen, myTopPath);
// Fill the iris.
// The iris is clipped by the bottom ellipse.
myGraphics.FillEllipse(myGreenBrush, -10, -15, 20, 22);
// Fill the pupil.
myGraphics.FillEllipse(myBlackBrush, -3, -7, 6, 9);
myGraphics.EndContainer(eyeContainer);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -