📄 sample9.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
/// <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 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(40, 32);
this.panel1.Name = "panel1";
this.panel1.TabIndex = 0;
this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void panel1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Pen blackPen= new Pen(Color.Black, 3);
int x = 10;
int y = 10;
int width = 60;
int height = 100;
int startAngle = 30;
int sweepAngle = 220;
e.Graphics.DrawArc(blackPen, x, y, width, height,
startAngle, sweepAngle);
//使用DrawArc方法绘制弧线
Point start = new Point(100, 100);
Point control1 = new Point(150, 20);
Point control2 = new Point(250, 50);
Point end = new Point(300, 100);
e.Graphics.DrawBezier(blackPen, start, control1, control2,end);
//使用DrawBezier方法绘制贝塞尔样条
PointF startF = new PointF(50.0F, 50.0F);
PointF controlF1 = new PointF(80.0F, 30.0F);
PointF controlF2 = new PointF(100.0F, 50.0F);
PointF end1 = new PointF(330.0F, 100.0F);
PointF controlF3 = new PointF(400.0F, 150.0F);
PointF controlF4 = new PointF(450.0F, 250.0F);
PointF end2 = new PointF(400.0F, 300.0F);
PointF[] bezierPoints ={startF, controlF1, controlF2,
end1,controlF3, controlF4, end2};
e.Graphics.DrawBeziers(blackPen, bezierPoints);
//使用DrawBeziers方法绘制一系列贝塞尔样条
PointF point1 = new PointF( 50.0F, 50.0F);
PointF point2 = new PointF(100.0F, 25.0F);
PointF point3 = new PointF(200.0F, 5.0F);
PointF point4 = new PointF(250.0F, 50.0F);
PointF point5 = new PointF(300.0F, 100.0F);
PointF point6 = new PointF(350.0F, 200.0F);
PointF point7 = new PointF(250.0F, 250.0F);
PointF[] curvePoints ={point1,point2, point3,point4,point5,point6,point7};
e.Graphics.DrawLines(blackPen, curvePoints);
//使用DrawLines方法绘制一组线段
int offset = 1;
int num =3;
e.Graphics.DrawCurve(blackPen, curvePoints, offset, num);
//使用DrawCurve方法绘制基数样条
x = 420;
y = 30;
width =80;
height =33;
e.Graphics.DrawEllipse(blackPen, x, y, width, height);
//使用DrawEllipse方法绘制椭圆
Icon newIcon = new Icon("C:\\a.ico");
x = 0;
y = 0;
e.Graphics.DrawIcon(newIcon, x, y);
//使用DrawIcon方法绘制图像
Rectangle rect = new Rectangle( 500, 0, 500, 100);
e.Graphics.DrawIconUnstretched(newIcon, rect);
//使用DrawIconUnstretched方法不缩放地绘制图像
Image newImage = Image.FromFile("C:\\w2k.jpg");
Point ulCorner = new Point( 200, 200);
e.Graphics.DrawImage(newImage, ulCorner);
//使用DrawImage方法按原始大小绘制指定的Image对象
int x1=15;
int x2=498;
int y1=8;
int y2=8;
e.Graphics.DrawLine(blackPen, x1, y1, x2, y2);
//使用DrawLine方法绘制线段
x = 300;
y = 20;
width = 200;
height = 100;
startAngle = 0;
sweepAngle = 45;
e.Graphics.DrawPie(blackPen, x, y, width, height,startAngle, sweepAngle);
//使用DrawPie方法绘制扇形
point1 = new PointF( 450.0F, 150.0F);
point2 = new PointF(400.0F, 225.0F);
point3 = new PointF(500.0F, 235.0F);
point4 = new PointF(450.0F, 250.0F);
point5 = new PointF(400.0F, 200.0F);
point6 = new PointF(450.0F, 200.0F);
point7 = new PointF(450.0F, 210.0F);
PointF[] curvePointFs ={point1, point2,point3, point4,point5,point6, point7};
e.Graphics.DrawPolygon(blackPen, curvePointFs);
//使用DrawPolygon方法绘制多边形
x=400;
y=30;
width=20;
height=50;
e.Graphics.DrawRectangle(blackPen,x,y,width,height);
//使用DrawRectangle方法绘制矩形
RectangleF[] rects =
{
new RectangleF( 0.0F, 0.0F, 100.0F, 200.0F),new RectangleF(100.0F, 200.0F, 250.0F, 50.0F),
new RectangleF(300.0F, 0.0F, 50.0F, 100.0F)};
e.Graphics.DrawRectangles(blackPen, rects);
//使用DrawRectangles方法绘制一系列的矩形
e.Graphics.DrawString("测试", new Font("Arial",13),
Brushes.Black, 110, 0);
//使用DrawString方法绘制指定的文本字符串
Rectangle testR = new Rectangle(100, 100, 180, 180);
Region testRn = new Region(testR);
e.Graphics.ExcludeClip(testRn);
//使用ExcludeClip方法更新剪辑区域
e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), 0, 0, 250, 250);
//使用FillRectangle方法填充矩形
SolidBrush blackBrush = new SolidBrush(Color.Black);
Point p1 = new Point(100, 100);
Point p2 = new Point(200, 50);
Point p3 = new Point(250, 200);
Point p4 = new Point( 50, 150);
Point[] points = {p1, p2, p3, p4};
e.Graphics.FillClosedCurve(blackBrush, points);
//使用FillClosedCurve方法闭合基数样条曲线的内部
x=300;
y=80;
width=50;
height=30;
RectangleF testRF = new RectangleF( x, y, width, height);
e.Graphics.FillEllipse(blackBrush, testRF);
//使用FillEllipse方法填充椭圆内部
testR = new Rectangle( 80, 80, 200, 100);
startAngle = 0;
sweepAngle = 45;
e.Graphics.FillPie(blackBrush, testR, startAngle, sweepAngle);
//使用FillPie方法填充扇形区的内部
point1= new PointF( 50, 50);
point2 = new PointF(100, 25);
point3 = new PointF(200, 5);
point4 = new PointF(250, 50);
point5 = new PointF(300, 100);
point6 = new PointF(350, 200);
point7 = new PointF(250, 250);
PointF[] testPF =
{
point1,point2,point3,point4,point5,point6,point7
};
e.Graphics.FillPolygon(blackBrush, testPF);
//使用FillPolygon方法填充多边形的内部
testRF = new RectangleF(180.0F,180.0F, 200.0F, 200.0F);
e.Graphics.FillRectangle(blackBrush, testRF);
//使用FillRectangle方法填充矩形
RectangleF[] testRFA =
{
new RectangleF( 0.0F, 0.0F, 100.0F, 200.0F),
new RectangleF(100.0F, 200.0F, 250.0F, 50.0F),
new RectangleF(300.0F, 0.0F, 50.0F, 100.0F)
};
e.Graphics.FillRectangles(blackBrush, testRFA);
//使用FillRectangles方法填充多个矩形
testR= new Rectangle(100, 100, 200, 200);
testRn = new Region(testR);
e.Graphics.FillRegion(blackBrush, testRn);
//使用FillRegion方法填充 Region 对象的内部
IntPtr hdc = e.Graphics.GetHdc();
Graphics newGraphics = Graphics.FromHdc(hdc);
newGraphics.DrawRectangle(new Pen(Color.Green,5),0,0, 200, 100);
e.Graphics.ReleaseHdc(hdc);
//使用DrawRectangle方法绘制矩形
Color testC = Color.FromArgb(200, 180, 60 , 140);
SolidBrush testB = new SolidBrush(testC);
e.Graphics.FillEllipse(testB, 80, 10, 400, 150);
Color tempC = e.Graphics.GetNearestColor(testC);
SolidBrush testSB = new SolidBrush(tempC);
e.Graphics.FillEllipse(testSB, 100, 0,100, 20);
//使用FillEllipse方法绘制椭圆
testRn = new Region(new Rectangle(50, 50, 100, 100));
e.Graphics.SetClip(testRn, CombineMode.Replace);
x1 = 100;
y1 = 100;
x2 = 200;
y2 = 200;
if (e.Graphics.IsVisible(x1, y1))
//使用IsVisible方法判断一对坐标指定的点是否包含在此
//Graphics 对象的可见剪辑区域内
e.Graphics.FillEllipse(new SolidBrush(Color.Red),x1,y1,10, 10);
if (e.Graphics.IsVisible(x2, y2))
e.Graphics.FillEllipse(new SolidBrush(Color.Blue), x2, y2, 10, 10);
string measureString = "第一和第二区域";
Font stringFont = new Font("Times New Roman", 16.0F);
CharacterRange[] characterRanges =
{
new CharacterRange(0, 5),
new CharacterRange(10, 6)
};
x = 450;
y = 50;
width = 35;
height = 200;
testRF = new RectangleF(x, y, width, height);
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags =
StringFormatFlags.DirectionVertical;
stringFormat.SetMeasurableCharacterRanges(characterRanges);
e.Graphics.DrawString(measureString,stringFont,Brushes.Black,x, y,stringFormat);
//使用DrawString方法绘制字符串
Region[] stringRegions = new Region[2];
stringRegions = e.Graphics.MeasureCharacterRanges(measureString,stringFont,testRF,stringFormat);
//使用MeasureCharacterRanges方法获取Region对象数组
RectangleF measureRect1 =
stringRegions[0].GetBounds(e.Graphics);
e.Graphics.DrawRectangle(new Pen(Color.Yellow, 1),
Rectangle.Round(measureRect1));
RectangleF measureRect2 =
stringRegions[1].GetBounds(e.Graphics);
e.Graphics.DrawRectangle(new Pen(Color.Green, 1),
Rectangle.Round(measureRect2));
measureString = "字符串";
stringFont = new Font("Arial", 16);
int stringWidth = 200;
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString,
stringFont, stringWidth);
//使用MeasureString方法测量用指定的 Font 对象绘制的指定字符串
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F,
0.0F, stringSize.Width, stringSize.Height);
e.Graphics.DrawString(measureString,stringFont,Brushes.White ,new PointF(450, 30));
testR = new Rectangle(0, 0, 200, 200);
e.Graphics.SetClip(testR);
//使用SetClip方法将此 Graphics 对象的剪辑区域设置为指定 Graphics 对象的 Clip 属性
testRF = new RectangleF(100.0F,100.0F,
200.0F, 200.0F);
e.Graphics.IntersectClip(testRF);
//使用IntersectClip方法将此 Graphics 对象的剪辑区域更新为当前剪辑区域与指定Rectangle结构的交集
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 500, 500);
e.Graphics.ResetClip();
//使用ResetClip方法将此 Graphics 对象的剪辑区域重置为无限区域
e.Graphics.DrawRectangle(new Pen(Color.Black), testR);
e.Graphics.DrawRectangle(new Pen(Color.Red),
Rectangle.Round(testRF));
e.Graphics.TranslateTransform(100, 0);
//使用TranslateTransform方法将指定的平移添加到此Graphics 对象的变换矩阵前
GraphicsState transState = e.Graphics.Save();
//使用Save方法保存此 Graphics 对象的当前状态,并用 GraphicsState 对象标识保存的状态
e.Graphics.ResetTransform();
e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0,0, 100, 100);
e.Graphics.Restore(transState);
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), 0,0, 100, 100);
e.Graphics.TranslateTransform(100.0F, 0.0F);
e.Graphics.RotateTransform(30.0F);
//使用RotateTransform方法将指定旋转应用于此Graphics 对象的变换矩阵
e.Graphics.DrawEllipse(new Pen(Color.Blue, 3), 0, 0,200, 80);
e.Graphics.RotateTransform(30.0F);
e.Graphics.ScaleTransform(3.0F, 1.0F, MatrixOrder.Append);
//使用ScaleTransform方法将指定的缩放操作应用于此Graphics 对象的变换矩阵
e.Graphics.DrawRectangle(new Pen(Color.Blue, 3), 50, 0, 100, 40);
testRn = new Region(new Rectangle(0, 0, 100, 100));
e.Graphics.SetClip(testRn, CombineMode.Replace);
e.Graphics.FillRectangle(new SolidBrush(Color.Black), 0, 0, 500, 300);
PointF[] pointFA =
{
new PointF(0.0F, 0.0F),
new PointF(100.0F, 50.0F)
};
e.Graphics.DrawLine(new Pen(Color.Blue,
3),pointFA[0],pointFA[1]);
e.Graphics.TranslateTransform(40.0F, 30.0F);
e.Graphics.TransformPoints(CoordinateSpace.Page,
CoordinateSpace.World,pointFA);
e.Graphics.ResetTransform();
//使用ResetTransform方法将此 Graphics 对象的全局变换矩阵重置为单位矩阵
e.Graphics.DrawLine(new Pen(Color.Red,
3),pointFA[0],pointFA[1]);
testRF = new RectangleF(0.0F, 0.0F, 100.0F, 100.0F);
e.Graphics.SetClip(testR);
float dx = 50.0F;
float dy = 50.0F;
e.Graphics.TranslateClip(dx, dy);
//使用TranslateClip方法将此 Graphics 对象的剪辑区域沿水平方向和垂直方向平移指定的量
e.Graphics.FillRectangle(new SolidBrush(Color.Black), 0,0, 500, 300);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -