C#的GDI+绘图详解

C#的GDI+绘图详解

创建 Graphics 的对应3个方法。

// 1.在窗体或控件的Paint事件中,用PaintEventArgs e 创建绘图对象 ---- 控件或窗体重绘时

private void Form1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics; //绘图对象

}

//2.创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics(); //为控件创建绘图对象

//3.基于图片去绘制

Graphics g = Graphics.FromImage(Image.FromFile("imgs/05.jpg"));

//--------------------以上方法三选一

//呈现质量

g.SmoothingMode= SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//开始绘制。。。

private void btnLine_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics(); //为控件创建绘图对象

//呈现质量

g.SmoothingMode= SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//开始绘制。。。

//1.画一条直线

Pen pen01 = new Pen(Color.Green, 2);

g.DrawLine(pen01, 10, 10, 100, 10);//画一条线,起点(10,10) 终点(100,10)

g.DrawLine(pen01, new Point(100, 10), new Point(100, 100));//画一条线,起点(100,10) 终点(100,100)

g.DrawLine(pen01, new Point(100, 100), new Point(10, 100));

g.DrawLine(pen01, new Point(10, 100), new Point(10, 10));

pen01.Dispose();

//2.画多条直线 点依次连接起来

Pen pen03 = new Pen(Color.Red, 2);

Point[] points = new Point[]

{

new Point(200, 10),

new Point(300, 10),

new Point(300,150),

new Point(200,100),

new Point(200, 10)

};

g.DrawLines(pen03, points);

pen03.Dispose();

//释放绘图对象

g.Dispose();

}

private void btnCurve_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//画曲线

Pen pen01 = new Pen(Color.Green, 2);

Point[] points = new Point[]

{

new Point(100, 10),//起点

new Point(300, 50),//控制点

new Point(200,100),//控制点

new Point(400,150),//控制点

new Point(300,180),//控制点

new Point(200,130) //终点

};

//g.DrawCurve(pen01, points);//没有指定张力,默认0.5

g.DrawCurve(pen01, points,0.6f);

pen01.Dispose();

g.Dispose();

}

private void btnClosedCurve_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//画曲线

Pen pen01 = new Pen(Color.Green, 2);

Point[] points = new Point[]

{

new Point(100, 200),//起点

new Point(200, 150),//控制点

new Point(300,100),//控制点

new Point(350,200),//控制点

new Point(380,270),//控制点

new Point(400,230)//终点

};

//填充闭合曲线

g.FillClosedCurve(new SolidBrush(Color.Pink), points);

//绘制闭合曲线

g.DrawClosedCurve(pen01, points);

pen01.Dispose();

g.Dispose();

}

private void btnArc_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//画弧线

Pen pen01 = new Pen(Color.Green, 2);

//画弧线 起始角度 180,旋转60度

g.DrawArc(pen01, new Rectangle(50, 20, 200, 100), 180, 60);

// g.DrawEllipse(new Pen(Color.Red,1), new Rectangle(50, 20, 200, 100));

//画弧线 起始角度 0,旋转120度

g.DrawArc(new Pen(Color.Purple, 3), new Rectangle(50, 20, 200, 100), 0, 120);

//画弧线 起始角度 90,旋转120度

g.DrawArc(new Pen(Color.Navy, 3), new Rectangle(100, 100, 200, 200), 90, 120);

pen01.Dispose();

g.Dispose();

}

private void btnEllipse_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

Pen pen01 = new Pen(Color.Orange, 2);

Rectangle rect = new Rectangle(100, 50, 200, 100);

//填充椭圆内部

g.FillEllipse(new SolidBrush(Color.Pink), rect);

//画椭圆

g.DrawEllipse(pen01, rect);

g.FillEllipse(new SolidBrush(Color.LightBlue), 400, 50, 100, 100);

//画圆

g.DrawEllipse(pen01, 400, 50, 100, 100);

//另一种:都填充----边框和背景填充

//边框椭圆填充

g.FillEllipse(new SolidBrush(Color.Orange), 100, 200, 200, 100);

//背景椭圆填充

g.FillEllipse(new SolidBrush(Color.Pink), 100+2, 200+2, 200-2*2, 100-2*2);

pen01.Dispose();

g.Dispose();

}

private void btnRectangle_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//画矩形及填充

Pen pen01 = new Pen(Color.Orange, 2);

Rectangle rect = new Rectangle(100, 50, 300, 100);

//填充矩形

g.FillRectangle(new SolidBrush(Color.LightGreen), rect);

//填充多个矩形的内部

g.FillRectangles(new SolidBrush(Color.LightSkyBlue), new Rectangle[] {

new Rectangle(300,200,100,50),

new Rectangle(450,200,50,50),

new Rectangle(300,300,80,60)

});

//画矩形

g.DrawRectangle(pen01, rect);

//画多个矩形

g.DrawRectangles(pen01,new Rectangle[] {

new Rectangle(300,200,100,50),

new Rectangle(450,200,50,50),

new Rectangle(300,300,80,60)

});

pen01.Dispose();

g.Dispose();

}

private void btnPolygon_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//画多边形及填充

Pen pen01 = new Pen(Color.Orange, 2);

//定义多边形的形状

Point[] points =

{

new Point(100,50),

new Point(300,100),

new Point(300,200),

new Point(200,170),

new Point(140,120)

};

//填充

g.FillPolygon(new SolidBrush(Color.Gray), points);

//画多边形

g.DrawPolygon(pen01, points);

Point[] points1 =

{

new Point(300,50),

new Point(400,50),

new Point(480,200),

new Point(350,150)

};

//填充多边形

g.FillPolygon(new SolidBrush(Color.DarkOrchid), points1);

pen01.Dispose();

g.Dispose();

}

private void btnPie_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//扇形的绘制与填充

Pen pen01 = new Pen(Color.Orange, 2);

Rectangle rect = new Rectangle(100, 20, 100, 100);

//填充

g.FillPie(new SolidBrush(Color.LightBlue), rect, 0, 60);

//绘制扇形 60

g.DrawPie(pen01, rect, 0, 60);

pen01.Dispose();

g.Dispose();

}

private void btnPath_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//路径的绘制与填充

Pen pen01 = new Pen(Color.Orange, 2);

//路径

GraphicsPath path1 = new GraphicsPath();

path1.AddLine(50, 50, 50, 100);//直线

path1.AddArc(new Rectangle(50, 50, 100, 100), 270, 90);//弧线

path1.AddLines(new Point[] //两条直线

{

new Point(200,100),

new Point(200,150),

new Point(100,200)

});

//添加曲线

path1.AddCurve(new Point[]

{

new Point(100,200),

new Point(50,120),

new Point(20,100),

new Point(50, 50)

});

//添加闭合曲线

GraphicsPath path2 = new GraphicsPath();

path2.AddClosedCurve(new Point[]

{

new Point(100,30),

new Point(150,50),

new Point(200,20)

});

//追加子路径

path1.AddPath(path2,true);

//添加椭圆

path1.AddEllipse(new Rectangle(220, 100, 100, 60));

//添加扇形

path1.AddPie(new Rectangle(200, 200, 100, 200), 200, 100);

//添加文字

path1.AddString("复合图形", this.Font.FontFamily, (int)FontStyle.Italic, 14, new Point(100, 300), new StringFormat() { Alignment = StringAlignment.Center });

//填充路径

g.FillPath(new SolidBrush(Color.LightBlue), path1);

//绘制路径

g.DrawPath(pen01, path1);

pen01.Dispose();

g.Dispose();

}

private void btnImageAndIcon_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//图片绘制

Image imgs = Image.FromFile("Imgs/04-jh.jpg");

g.DrawImage(imgs, new Rectangle(50, 50, 100, 100));//以指定尺寸在指定位置处绘制图片

//g.DrawImage(imgs, new Point(100, 50));//指定位置处以原始大小绘制,没有缩放图片

//g.DrawImageUnscaled(imgs, new Point(100, 50));//指定位置处以原始大小绘制,没有缩放图片

//g.DrawImageUnscaledAndClipped(imgs, new Rectangle(50, 50, 100, 100));//不缩放,以指定尺寸进行绘制,需要时会进行裁剪

//图标绘制

Icon icon = new Icon("Imgs/zxlogo.ico");

g.DrawIcon(icon, new Rectangle(200, 50, 50, 50)); //会进行缩放

//g.DrawIconUnstretched(icon, new Rectangle(300, 50, 100, 100));//不会根据目标尺寸进行缩放

g.Dispose();

}

private void btnFont_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//绘制文字

g.DrawString("正在绘制文本", new Font("微软雅黑", 14f, FontStyle.Bold), new SolidBrush(Color.ForestGreen), new Point(100, 100));

Rectangle rect = new Rectangle(200, 150, 100, 40);

g.FillEllipse(new SolidBrush(Color.LightBlue), new Rectangle(200, 150, 100, 40));

//g.DrawString("添加站点", new Font("微软雅黑", 13, FontStyle.Bold), new SolidBrush(Color.White), 215, 157);

StringFormat format = new StringFormat();

//水平垂直都居中

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

g.DrawString("添加站点", new Font("微软雅黑", 13, FontStyle.Bold), new SolidBrush(Color.White),rect, format);

g.Dispose();

}

//标签重绘

private void label1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.HighQuality;

g.Clear(label1.BackColor);

Rectangle rect = label1.ClientRectangle;//工作区矩形

Rectangle rect1 = new Rectangle(rect.X, rect.Y, rect.Width - 1, rect.Height - 1);

g.DrawRectangle(new Pen(Color.OrangeRed, 1), rect1);

StringFormat format = new StringFormat();

//水平垂直都居中

format.Alignment = StringAlignment.Center;

format.LineAlignment = StringAlignment.Center;

g.DrawString(label1.Text, label1.Font, new SolidBrush(label1.ForeColor), rect, format);

}

//圆角矩形的绘制

private void btnRoundRect_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//圆角矩形的绘制

Pen pen01 = new Pen(Color.Orange, 2);

Rectangle rect1 = new Rectangle(100, 50, 200, 100);

//圆角路径

GraphicsPath roundRect = GetRoundRect(rect1, 10);

//填充圆角矩形

g.FillPath(new SolidBrush(Color.LightBlue), roundRect);

//画圆角矩形形状

g.DrawPath(pen01, roundRect);

pen01.Dispose();

g.Dispose();

}

// 返回圆角矩形路径 四个圆角是一样的

private GraphicsPath GetRoundRect(Rectangle rect,int r)

{

GraphicsPath path = new GraphicsPath();

int x = rect.X;

int y = rect.Y;

int w= rect.Width;

int h= rect.Height;

int d = 2 * r;//直径

//左上角

path.AddArc(new Rectangle(x, y, d, d), 180, 90);

//上方

path.AddLine(x + r, y, x + w - r, y);

//右上角

path.AddArc(new Rectangle(x + w - d, y, d, d), 270, 90);

//右边

path.AddLine(x+w,y+r,x+w,y+h-r);

//右下角

path.AddArc(new Rectangle(x+w-d,y+h-d, d, d), 0, 90);

//下边

path.AddLine(x + w-r, y + h, x + r, y + h);

//左下角

path.AddArc(new Rectangle(x , y + h - d, d, d), 90, 90);

//左边

path.AddLine(x , y + h-r, x , y + r);

return path;

}

// 渐变填充

private void btnGradient_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

//线性渐变

Rectangle rect1 = new Rectangle(100, 50, 200, 100);

//创建线性渐变画刷

//LinearGradientBrush gbrush = new LinearGradientBrush(rect1, Color.Silver, Color.Orange, LinearGradientMode.Horizontal);//从左到右的渐变

//LinearGradientBrush gbrush = new LinearGradientBrush(rect1, Color.Silver, Color.Orange, LinearGradientMode.Vertical);//从上到下的渐变

//LinearGradientBrush gbrush = new LinearGradientBrush(rect1, Color.Silver, Color.Orange, LinearGradientMode.ForwardDiagonal);//从左上到右下的渐变

//LinearGradientBrush gbrush = new LinearGradientBrush(rect1, Color.Silver, Color.Orange, LinearGradientMode.BackwardDiagonal);//从右上到左下的渐变

//LinearGradientBrush gbrush = new LinearGradientBrush(rect1, Color.Silver, Color.Orange, 50); //按角度渐变

LinearGradientBrush gbrush = new LinearGradientBrush(new Point(100,50),new Point(100,150), Color.Silver, Color.Orange); //从开始点到结束点之间渐变

g.FillRectangle(gbrush, rect1);

//路径渐变

GraphicsPath path1 = new GraphicsPath();

path1.AddEllipse(new Rectangle(100, 200, 100, 100));//椭圆

PathGradientBrush pbrush = new PathGradientBrush(path1);//路径渐变画刷

pbrush.CenterColor = Color.White;//中点颜色

pbrush.SurroundColors = new Color[] { Color.Green };

g.FillPath(pbrush, path1);//路径填充

g.DrawPath(new Pen(Color.Gray, 2), path1);

g.Dispose();

}

// 平移、旋转、缩放操作

private void btnTransform_Click(object sender, EventArgs e)

{

//创建绘图对象 panel1中绘图

Graphics g = panel1.CreateGraphics();//为控件创建绘图对象

//呈现质量

g.SmoothingMode = SmoothingMode.HighQuality;//消除锯齿 或 AntiAlias

////1.平移变换

//Rectangle rect = new Rectangle(100, 50, 200, 100);

//g.FillRectangle(new SolidBrush(Color.SteelBlue), rect);

////平移 x:50 y:20

//g.TranslateTransform(50, 20);

//g.FillRectangle(new SolidBrush(Color.LightGreen), rect);

////平移 x:-30 y:-50

//g.TranslateTransform(-30, -50);

//g.FillRectangle(new SolidBrush(Color.CadetBlue), rect);

////2.旋转变换

//Rectangle rect = new Rectangle(200, 50, 200, 100);

//g.DrawRectangle(new Pen(Color.Orange, 2), rect);

////平移

//g.TranslateTransform(100, 30);

//g.DrawRectangle(new Pen(Color.Green, 2), rect);

////旋转30度

//g.RotateTransform(30);

//g.DrawRectangle(new Pen(Color.Green, 2), rect);

//3.缩放变换

Rectangle rect = new Rectangle(200, 50, 100, 100);

g.DrawEllipse(new Pen(Color.Orange, 2), rect);

//放大 x:2倍 y:1.5倍

g.ScaleTransform(2, 1.5f);

g.DrawEllipse(new Pen(Color.Red, 2), rect);

g.Dispose();

}

CircleLabel.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WinGraphics.UControls

{

public partial class CircleLabel : Label

{

public CircleLabel()

{

InitializeComponent();

//ControlStyles设置

SetStyle(ControlStyles.UserPaint, true);

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

SetStyle(ControlStyles.ResizeRedraw, true);

SetStyle(ControlStyles.Selectable, true);

SetStyle(ControlStyles.SupportsTransparentBackColor, true);

this.Size = new Size(80, 30);

BackColor = Color.Transparent;//背景透明

rect = this.ClientRectangle;

}

Rectangle rect;//工作区

//背景色 边框色 边框粗细

private Color bgColor= Color.LightGray;

///

/// 背景画刷

///

public Color BgColor

{

get { return bgColor; }

set { bgColor = value;

Invalidate();

}

}

private Color borderColor =Color.Gray;

///

/// 边框画刷

///

public Color BorderColor

{

get { return borderColor; }

set

{

borderColor = value;

Invalidate();

}

}

private int borderWidth=0;

///

/// 边框粗细

///

public int BorderWidth

{

get { return borderWidth; }

set { borderWidth = value;

Invalidate();

}

}

protected override void OnSizeChanged(EventArgs e)

{

rect= ClientRectangle;

rect.Width -= 1;

rect.Height -= 1;

}

///

/// 重绘控件

///

///

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.HighQuality;

//边框 背景

Rectangle rectBg;

if (borderWidth > 0)//有边框

{

g.FillEllipse(new SolidBrush(BorderColor), rect);//边框椭圆

rectBg = new Rectangle(rect.X + borderWidth, rect.Y + borderWidth, rect.Width - 2 * borderWidth, rect.Height - 2 * borderWidth);

}

else

rectBg = rect;

//背景填充

g.FillEllipse(new SolidBrush(BgColor), rectBg);

StringFormat format = new StringFormat();

format.LineAlignment= StringAlignment.Center;

format.Alignment= StringAlignment.Center;

//文本绘制

SolidBrush fontBrush = new SolidBrush(this.ForeColor);

g.DrawString(this.Text, this.Font, fontBrush, rect, format);

}

}

}

UCircleButton.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WinGraphics.UControls

{

public class UCircleButton:Button

{

public UCircleButton()

{

//ControlStyles设置

SetStyle(ControlStyles.UserPaint, true);

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

SetStyle(ControlStyles.ResizeRedraw, true);

SetStyle(ControlStyles.Selectable, true);

SetStyle(ControlStyles.SupportsTransparentBackColor, true);

//this.FlatAppearance.MouseDownBackColor = Color.Transparent;

this.Size = new Size(80, 30);

BackColor = Color.Transparent;//背景透明

this.FlatAppearance.BorderSize = 0;

this.FlatStyle= FlatStyle.Flat;

rect = this.ClientRectangle;

}

Rectangle rect;//工作区

private Color bgColor = Color.LightGray;

///

/// 背景色1

///

[DefaultValue(typeof(Color), "LightGray"),Description("按钮的背景颜色1"),Category("自定义")]

public Color BgColor

{

get { return bgColor; }

set

{

bgColor = value;

Invalidate();

}

}

private Color bgColor2 = Color.White;

///

/// 背景色2

///

public Color BgColor2

{

get { return bgColor2; }

set

{

bgColor2 = value;

Invalidate();

}

}

private Color borderColor = Color.Gray;

///

/// 边框色

///

public Color BorderColor

{

get { return borderColor; }

set

{

borderColor = value;

Invalidate();

}

}

private int borderWidth = 0;

///

/// 边框粗细

///

public int BorderWidth

{

get { return borderWidth; }

set

{

borderWidth = value;

Invalidate();

}

}

private Color mouseDownFColor = Color.Blue;

///

/// 按下时的文本颜色

///

public Color MouseDownFColor

{

get { return mouseDownFColor; }

set

{

mouseDownFColor = value;

Invalidate();

}

}

private Color mouseDownBgColor = Color.OrangeRed;

///

/// 按下时的背景颜色

///

public Color MouseDownBgColor

{

get { return mouseDownBgColor; }

set

{

mouseDownBgColor = value;

Invalidate();

}

}

private int radius=5;

///

/// 边框圆角半径

///

public int Radius

{

get { return radius; }

set { radius = value;

Invalidate();

}

}

private LinearGradientMode gradientMode = LinearGradientMode.Vertical;

///

/// 渐变模式

///

public LinearGradientMode GradientMode

{

get { return gradientMode; }

set {

gradientMode = value;

Invalidate();

}

}

//鼠标是否按下

bool isMouseDown = false;

protected override void OnMouseDown(MouseEventArgs mevent)

{

base.OnMouseDown(mevent);

isMouseDown = true;

}

protected override void OnMouseLeave(EventArgs e)

{

base.OnMouseLeave(e);

isMouseDown = false;

}

protected override void OnSizeChanged(EventArgs e)

{

rect = ClientRectangle;

this.Region = new Region(rect);

rect.Width -= 1;

rect.Height -= 1;

}

///

/// 控件重绘

///

///

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.HighQuality;

//边框 背景

Rectangle rectBg;

//路径 path pathbg

GraphicsPath path = new GraphicsPath();//边框的路径

GraphicsPath pathBg = new GraphicsPath();//背景的路径

if(radius>0)

{

//边框的路径

path = PaintHelper.GetRoundRect(rect, radius);

if(borderWidth>0)

{

g.FillPath(new SolidBrush(borderColor), path);//填充边框路径

//背景区域

rectBg=new Rectangle(rect.X+borderWidth,rect.Y+borderWidth,rect.Width-2*borderWidth,rect.Height-2*borderWidth);

//背景路径

pathBg=PaintHelper.GetRoundRect(rectBg, radius);

}

else

{

pathBg = path;

rectBg = rect;

}

if(bgColor2!=Color.Transparent)//渐变填充

{

LinearGradientBrush bgBrush = new LinearGradientBrush(rectBg, bgColor, bgColor2, gradientMode);

if(isMouseDown)

{

bgBrush= new LinearGradientBrush(rectBg, mouseDownBgColor, bgColor2, gradientMode);

}

g.FillPath(bgBrush, pathBg);//背景填充

}

else

{

SolidBrush bgBrush = new SolidBrush(bgColor);

if (isMouseDown)

{

bgBrush= new SolidBrush(mouseDownBgColor);

}

g.FillPath(bgBrush, pathBg);

}

}

else

{

if(borderWidth>0)

{

g.FillRectangle(new SolidBrush(borderColor), rect);

rectBg = new Rectangle(rect.X + borderWidth, rect.Y + borderWidth, rect.Width - 2 * borderWidth, rect.Height - 2 * borderWidth);

}

else

{

rectBg=rect;

}

if (bgColor2 != Color.Transparent)//渐变填充

{

LinearGradientBrush bgBrush = new LinearGradientBrush(rectBg, bgColor, bgColor2, gradientMode);

if (isMouseDown)

{

bgBrush= new LinearGradientBrush(rectBg, mouseDownBgColor, bgColor2, gradientMode);

}

g.FillRectangle(bgBrush, rectBg);//背景填充

}

else

{

SolidBrush bgBrush = new SolidBrush(bgColor);

if (isMouseDown)

{

bgBrush=new SolidBrush(mouseDownBgColor);

}

g.FillRectangle(bgBrush, rectBg);

}

}

//文本绘制

StringFormat format = new StringFormat();

format.LineAlignment = StringAlignment.Center;

format.Alignment = StringAlignment.Center;

SolidBrush fontBrush = new SolidBrush(this.ForeColor);

if(isMouseDown)

{

fontBrush = new SolidBrush(this.mouseDownFColor);

}

g.DrawString(this.Text, this.Font, fontBrush, rect, format);

}

}

}

UPanel.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WinGraphics.UControls

{

public class UPanel:Panel

{

public UPanel() {

//ControlStyles设置

SetStyle(ControlStyles.UserPaint, true);

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

SetStyle(ControlStyles.ResizeRedraw, true);

SetStyle(ControlStyles.SupportsTransparentBackColor, true);

this.Size = new Size(100, 50);

BackColor = Color.Transparent;//背景透明

rect = this.ClientRectangle;

}

Rectangle rect;//工作区

private Color bgColor = Color.LightGray;

///

/// 背景色1

///

[DefaultValue(typeof(Color), "LightGray"), Description("按钮的背景颜色1"), Category("自定义")]

public Color BgColor

{

get { return bgColor; }

set

{

bgColor = value;

Invalidate();

}

}

private Color bgColor2 = Color.White;

///

/// 背景色2

///

public Color BgColor2

{

get { return bgColor2; }

set

{

bgColor2 = value;

Invalidate();

}

}

private Color borderColor = Color.Gray;

///

/// 边框色

///

public Color BorderColor

{

get { return borderColor; }

set

{

borderColor = value;

Invalidate();

}

}

private int borderWidth = 0;

///

/// 边框粗细

///

public int BorderWidth

{

get { return borderWidth; }

set

{

borderWidth = value;

Invalidate();

}

}

private int radius = 5;

///

/// 边框圆角半径

///

public int Radius

{

get { return radius; }

set

{

radius = value;

Invalidate();

}

}

private LinearGradientMode gradientMode = LinearGradientMode.Vertical;

///

/// 渐变模式

///

public LinearGradientMode GradientMode

{

get { return gradientMode; }

set

{

gradientMode = value;

Invalidate();

}

}

protected override void OnSizeChanged(EventArgs e)

{

rect = ClientRectangle;

this.Region = new Region(rect);

rect.Width -= 1;

rect.Height -= 1;

}

///

/// 控件重绘

///

///

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.HighQuality;

//边框 背景

Rectangle rectBg;

//路径 path pathbg

GraphicsPath path = new GraphicsPath();//边框的路径

GraphicsPath pathBg = new GraphicsPath();//背景的路径

if (radius > 0)

{

//边框的路径

path = PaintHelper.GetRoundRect(rect, radius);

if (borderWidth > 0)

{

g.FillPath(new SolidBrush(borderColor), path);//填充边框路径

//背景区域

rectBg = new Rectangle(rect.X + borderWidth, rect.Y + borderWidth, rect.Width - 2 * borderWidth, rect.Height - 2 * borderWidth);

//背景路径

pathBg = PaintHelper.GetRoundRect(rectBg, radius);

}

else

{

pathBg = path;

rectBg = rect;

}

if (bgColor2 != Color.Transparent)//渐变填充

{

LinearGradientBrush bgBrush = new LinearGradientBrush(rectBg, bgColor, bgColor2, gradientMode);

g.FillPath(bgBrush, pathBg);//背景填充

}

else

{

SolidBrush bgBrush = new SolidBrush(bgColor);

g.FillPath(bgBrush, pathBg);

}

}

else

{

if (borderWidth > 0)

{

g.FillRectangle(new SolidBrush(borderColor), rect);

rectBg = new Rectangle(rect.X + borderWidth, rect.Y + borderWidth, rect.Width - 2 * borderWidth, rect.Height - 2 * borderWidth);

}

else

{

rectBg = rect;

}

if (bgColor2 != Color.Transparent)//渐变填充

{

LinearGradientBrush bgBrush = new LinearGradientBrush(rectBg, bgColor, bgColor2, gradientMode);

g.FillRectangle(bgBrush, rectBg);//背景填充

}

else

{

SolidBrush bgBrush = new SolidBrush(bgColor);

g.FillRectangle(bgBrush, rectBg);

}

}

}

}

}

USwitch.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WinGraphics.UControls

{

[DefaultEvent("CheckedChanged")]

public class USwitch : Control

{

public USwitch()

{

//ControlStyles设置

SetStyle(ControlStyles.UserPaint, true);

SetStyle(ControlStyles.AllPaintingInWmPaint, true);

SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

SetStyle(ControlStyles.ResizeRedraw, true);

SetStyle(ControlStyles.Selectable, true);

SetStyle(ControlStyles.SupportsTransparentBackColor, true);

this.Size = new Size(100, 30);

this.Click += USwitch_Click;

}

private void USwitch_Click(object sender, EventArgs e)

{

CheckedChanged?.Invoke(this, new EventArgs());

}

public event EventHandler CheckedChanged;//开关状态改变后触发

private Color trueColor = Color.FromArgb(34, 163, 169);

[Description("打开时的颜色"), Category("自定义")]

public Color TrueColor

{

get { return trueColor; }

set

{

trueColor = value;

Invalidate();

}

}

private Color falseColor = Color.FromArgb(111, 122, 126);

[Description("关闭时的颜色"), Category("自定义")]

public Color FalseColor

{

get { return falseColor; }

set

{

falseColor = value;

Invalidate();

}

}

private bool swChecked;

[Description("当前开关的状态"), Category("自定义")]

public bool Checked

{

get { return swChecked; }

set

{

swChecked = value;

Invalidate();

}

}

private string[] texts = new string[] { "On", "Off" };

[Description("开关文本值,两个值,一个是开的文本,一个是关的文本"), Category("自定义")]

public string[] Texts

{

get { return texts; }

set

{

texts = value;

Invalidate();

}

}

public override Font Font {

get => base.Font;

set

{

base.Font = value;

Invalidate();

}

}

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.HighQuality;

//背景填充色

var fillColor=swChecked?trueColor: falseColor;

//路径

GraphicsPath path = new GraphicsPath();

//上边线 x:height/2,y:1 x:width-height/2,y:1

path.AddLine(this.Height / 2, 1, this.Width - this.Height / 2, 1);

//右边半圆弧

path.AddArc(new Rectangle(Width - Height - 1, 1, this.Height - 2, Height - 2), 270, 180);

//下边线 x:width-height/2,y:height-1 x:height/2,y:height-1

path.AddLine(this.Width - this.Height / 2, Height - 1, this.Height / 2, Height - 1);

//左边半圆弧

path.AddArc(new Rectangle(1, 1, this.Height - 2, Height - 2), 90, 180);

//路径填充

g.FillPath(new SolidBrush(fillColor), path);

string text = "";//要画的文字

if(texts!=null&&texts.Length==2)

{

if(swChecked)

text= texts[0];

else

text= texts[1];

}

if(swChecked)

{

//右边画圆,左边画文本

//圆的直径:Height-2-4

g.FillEllipse(Brushes.White, new Rectangle(Width - Height - 1 + 2, 1 + 2, Height - 2 - 4, Height - 2 - 4));

//画文本

SizeF sizeFont = g.MeasureString(text.Replace(" ", "A"), Font);

//计算文本左上角的y坐标

int fPointY= (this.Height - (int)sizeFont.Height) / 2 + 2;

int fPointX = (Height - 2 - 4) / 2;

g.DrawString(text,Font,Brushes.White,fPointX, fPointY);

}

else

{

//左边画圆,右边画文本

g.FillEllipse(Brushes.White, new Rectangle( 1 + 2, 1 + 2, Height - 2 - 4, Height - 2 - 4));

//画文本

SizeF sizeFont = g.MeasureString(text.Replace(" ", "A"), Font);

//计算文本左上角的y坐标

int fPointY=(this.Height -(int)sizeFont.Height) / 2 + 2;

int fPointX = Width - 2 - Height / 2 - ((int)sizeFont.Width) + 10;

g.DrawString(text, Font, Brushes.White, fPointX, fPointY);

}

}

}

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WinGraphics

{

public partial class FrmPrint : Form

{

public FrmPrint()

{

InitializeComponent();

}

string[] colNames = { "编号", "编码", "站点", "类型", "取件方式", "快递状态" };

List dataList = new List();

///

/// 打印设置

///

///

///

private void btnPrintSetting_Click(object sender, EventArgs e)

{

pageSetupDialog1.ShowDialog();//显示打印设置对话框

}

///

/// 打印机---选择打印机,点击打印,开始打印

///

///

///

private void btnPrint_Click(object sender, EventArgs e)

{

if(printDialog1.ShowDialog()==DialogResult.OK)

{

printDocument1.Print();//引发PrintPage事件

}

}

///

/// 打开打印预览

///

///

///

private void btnPreview_Click(object sender, EventArgs e)

{

printPreviewDialog1.ShowDialog();//显示打印预览对话框

}

int pageIndex = 1;//当前页索引

int rowIndex = 0;//当前行索引

///

/// 打印每一页时引发

///

///

///

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

{

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.HighQuality;

//g.DrawString("欢迎来到GDI+", new Font("微软雅黑", 10.8f, FontStyle.Bold), new SolidBrush(Color.Orange), 40, 20);

Pen pen = new Pen(Color.DarkGray, 1);//画线

int start = 40, top = 20;

int width = e.PageBounds.Width - 2 * start;

//标题矩形

Rectangle rectTitle = new Rectangle(start, top, width, 40);

g.DrawRectangle(pen, rectTitle);

//标题文本

StringFormat format = new StringFormat();

format.LineAlignment = StringAlignment.Center;

format.Alignment = StringAlignment.Center;

g.DrawString("快递信息列表", new Font("微软雅黑", 10.8f, FontStyle.Bold), new SolidBrush(Color.Purple), rectTitle, format);

pen = new Pen(Color.Black, 1);

//列标题栏的矩形

Rectangle rectColHeader = new Rectangle(start, top + rectTitle.Height + 20, width, 30);

g.DrawRectangle(pen, rectColHeader);

int ycol = top + rectTitle.Height + 20;//列表标题栏矩形左上角y

int colWidth = 100;

int[] colWidths = { 100, 200, 240, 150, 100, 100 };//各列的宽

int colWidth1=start+colWidth;//第二列x

g.DrawLine(pen, colWidth1, ycol, colWidth1, ycol + 30);//第一列右侧竖线

g.DrawLine(pen, start, ycol, colWidth1, ycol + 30);//第一列中的斜线

g.DrawString("标题", new Font("微软雅黑", 9f, FontStyle.Regular), new SolidBrush(Color.Black), start+60,ycol+3);

g.DrawString("数据", new Font("微软雅黑", 9f, FontStyle.Regular), new SolidBrush(Color.Red), start + 20, ycol + 12);

//画列标题栏

for (int i = 0; i < colNames.Length; i++)

{

g.DrawString(colNames[i], new Font("微软雅黑", 10.8f, FontStyle.Bold), new SolidBrush(Color.Green), colWidth1 + 20, ycol + 7);

colWidth1 += colWidths[i];//当前列右侧的x

if (i < colNames.Length - 1)

g.DrawLine(pen, colWidth1, ycol, colWidth1, ycol + 30);//如果不是最后一列,画右侧竖线

}

//画数据行 分页

while(rowIndex

{

ycol += 30;//下一行的左上角y

g.DrawLine(pen, start, ycol, start, ycol + 30);//左边竖线

//画行号

g.DrawString((rowIndex+1).ToString("00"), new Font("微软雅黑", 10.8f, FontStyle.Regular), new SolidBrush(Color.Brown), start + 20, ycol + 7);

colWidth1 = start + colWidth;

//第一列右侧竖线

g.DrawLine(pen, colWidth1, ycol, colWidth1, ycol + 30);

//数据行的信息 第rowIndex行的数据

for (int j = 0; j < dataList[rowIndex].Length; j++)

{

g.DrawString(dataList[rowIndex][j], new Font("微软雅黑", 10.8f, FontStyle.Regular), new SolidBrush(Color.DimGray), colWidth1 + 20, ycol + 7);

colWidth1+= colWidths[j];

if (j < dataList[rowIndex].Length-1)//不是最后一列

g.DrawLine(pen, colWidth1, ycol, colWidth1, ycol + 30);//如果不是最后一列,画右侧竖线

}

//当前行下边线

g.DrawLine(pen, start, ycol+30, e.PageBounds.Width-start, ycol + 30);

//当前行的右边竖线

g.DrawLine(pen, e.PageBounds.Width - start, ycol, e.PageBounds.Width - start, ycol + 30);

rowIndex++;

if(ycol+30+30>e.PageBounds.Bottom)//下一行的高度超过页的底部

{

e.HasMorePages= true;//添加下一页

break;

}

}

pageIndex++;

}

private void FrmPrint_Load(object sender, EventArgs e)

{

dataList.Add(new string[] { "101", "20230223114800001", "游仙区仙人路菜鸟驿站", "省外/小体积", "自提", "未取件" });

dataList.Add(new string[] { "102", "20230223114800002", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "103", "20230223114800003", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "104", "20230223114800004", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "105", "20230223114800005", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "106", "20230223114800006", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "107", "20230223114800007", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "108", "20230223114800008", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "109", "20230223114800009", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "110", "20230223114800010", "游仙区仙人路菜鸟驿站1", "省外/大体积", "派送", "已签收" });

dataList.Add(new string[] { "102", "20230223114800002", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "103", "20230223114800003", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "104", "20230223114800004", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "105", "20230223114800005", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "106", "20230223114800006", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "107", "20230223114800007", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "108", "20230223114800008", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "109", "20230223114800009", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "110", "20230223114800010", "游仙区仙人路菜鸟驿站1", "省外/大体积", "派送", "已签收" });

dataList.Add(new string[] { "101", "20230223114800001", "游仙区仙人路菜鸟驿站", "省外/小体积", "自提", "未取件" });

dataList.Add(new string[] { "102", "20230223114800002", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "103", "20230223114800003", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "104", "20230223114800004", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "105", "20230223114800005", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "106", "20230223114800006", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "107", "20230223114800007", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "108", "20230223114800008", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "109", "20230223114800009", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "110", "20230223114800010", "游仙区仙人路菜鸟驿站1", "省外/大体积", "派送", "已签收" });

dataList.Add(new string[] { "112", "20230223114800002", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "113", "20230223114800003", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "114", "20230223114800004", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "115", "20230223114800005", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "116", "20230223114800006", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "117", "20230223114800007", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "118", "20230223114800008", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "119", "20230223114800009", "游仙区仙人路菜鸟驿站1", "省外/小体积1", "自提", "未取件" });

dataList.Add(new string[] { "120", "20230223114800010", "游仙区仙人路菜鸟驿站1", "省外/大体积", "派送", "已签收" });

}

}

}

相关推荐

专访聚合数据CEO左磊:创业14年,虽经历阵痛 终熬到上市
8800万用户为什么选择南方+?这份报告为你解密
中国人民解放军内务条令(试行)
365娱乐场投注

中国人民解放军内务条令(试行)

10-04 👁️ 9466
gta5单机超级跑车位置
Bet体育365提款流水

gta5单机超级跑车位置

07-14 👁️ 769