● 开发环境
Microsoft Visual Studio
● 开发语言
Visual C#
● 开发过程
1、新建一个Windows窗体应用(.NET Framework),项目名称为MyApp,确定生成解决方案。
2、在解决方案资源管理器里将Form1更改为FormMain。
3、在解决方案资源管理器里右击解决方案'MyApp',添加-新建项目-类库(.NET Framework),项目名称为Framework,确定添加项目。
Framework项目
4、将默认的Class1.cs更改为Framework.cs;在Framework项目上右键-添加-Windows窗体,名称为FormSkin.cs;再添加一个Windows窗体,名称为FormMain.cs。
5、准备一张四周边缘透明的位图,添加到Resources.resx。
6、双击FormSkin.cs,打开窗体设计器,将FormSkin的FormBorderStyle更改为None,MaximizeBox更改为False,MinimizeBox更改为False,Size更改为800,450,StartPosition更改为CenterScreen。
7、双击FormMain.cs,打开窗体设计器,将FormMain的FormBorderStyle更改为None,MaximizeBox更改为False,MinimizeBox更改为False,Size更改为800,450,StartPosition更改为CenterScreen。
8、打开Framework.cs代码编辑器,添加如下代码:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
namespace Framework
{
public static class GraphicsPathHelper
{
public static GraphicsPath CreatePath(Rectangle rect, int radius, RoundStyle style, bool correction)
{
GraphicsPath path = new GraphicsPath();
int radiusCorrection = correction ? 1 : 0;
switch (style)
{
case RoundStyle.None:
path.AddRectangle(rect);
break;
case RoundStyle.All:
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
break;
case RoundStyle.Left:
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddLine(rect.Right - radiusCorrection, rect.Y, rect.Right - radiusCorrection, rect.Bottom - radiusCorrection);
path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
break;
case RoundStyle.Right:
path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
break;
case RoundStyle.Top:
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
path.AddLine(rect.Right - radiusCorrection, rect.Bottom - radiusCorrection, rect.X, rect.Bottom - radiusCorrection);
break;
case RoundStyle.Bottom:
path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
break;
case RoundStyle.BottomLeft:
path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
path.AddLine(rect.Right - radiusCorrection, rect.Y, rect.Right - radiusCorrection, rect.Bottom - radiusCorrection);
break;
case RoundStyle.BottomRight:
path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
break;
}
path.CloseFigure();
return path;
}
}
public enum RoundStyle
{
/// <summary>
/// 四个角都不是圆角
/// </summary>
None = 0,
/// <summary>
/// 四个角都为圆角
/// </summary>
All = 1,
/// <summary>
/// 左边两个为圆角
/// </summary>
Left = 2,
/// <summary>
/// 右边两个为圆角
/// </summary>
Right = 3,
/// <summary>
/// 上边两个为圆角
/// </summary>
Top = 4,
/// <summary>
/// 下边两个为圆角
/// </summary>
Bottom = 5,
/// <summary>
/// 左下角为圆角
/// </summary>
BottomLeft = 6,
/// <summary>
/// 右下角为圆角
/// </summary>
BottomRight = 7,
}
public partial class ImageDrawRect
{
public static ContentAlignment anyRight = ContentAlignment.BottomRight | (ContentAlignment.MiddleRight | ContentAlignment.TopRight);
public static ContentAlignment anyTop = ContentAlignment.TopRight | (ContentAlignment.TopCenter | ContentAlignment.TopLeft);
public static ContentAlignment anyBottom = ContentAlignment.BottomRight | (ContentAlignment.BottomCenter | ContentAlignment.BottomLeft);
public static ContentAlignment anyCenter = ContentAlignment.BottomCenter | (ContentAlignment.MiddleCenter | ContentAlignment.TopCenter);
public static ContentAlignment anyMiddle = ContentAlignment.MiddleRight | (ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft);
public static void DrawRect(Graphics g, Bitmap img, Rectangle r, Rectangle lr, int index, int Totalindex)
{
if (img == null) return;
Rectangle r1, r2;
int x = (index - 1) * img.Width / Totalindex;
int y = 0;
int x1 = r.Left;
int y1 = r.Top;
if (r.Height > img.Height && r.Width <= img.Width / Totalindex)
{
r1 = new Rectangle(x, y, img.Width / Totalindex, lr.Top);
r2 = new Rectangle(x1, y1, r.Width, lr.Top);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x, y + lr.Top, img.Width / Totalindex, img.Height - lr.Top - lr.Bottom);
r2 = new Rectangle(x1, y1 + lr.Top, r.Width, r.Height - lr.Top - lr.Bottom);
if ((lr.Top + lr.Bottom) == 0) r1.Height -= 1;
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x, y + img.Height - lr.Bottom, img.Width / Totalindex, lr.Bottom);
r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, r.Width, lr.Bottom);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
}
else if (r.Height <= img.Height && r.Width > img.Width / Totalindex)
{
r1 = new Rectangle(x, y, lr.Left, img.Height);
r2 = new Rectangle(x1, y1, lr.Left, r.Height);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + lr.Left, y, img.Width / Totalindex - lr.Left - lr.Right, img.Height);
r2 = new Rectangle(x1 + lr.Left, y1, r.Width - lr.Left - lr.Right, r.Height);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, img.Height);
r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, r.Height);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
}
else if (r.Height <= img.Height && r.Width <= img.Width / Totalindex)
{
r1 = new Rectangle((index - 1) * img.Width / Totalindex, 0, img.Width / Totalindex, img.Height - 1);
g.DrawImage(img, new Rectangle(x1, y1, r.Width, r.Height), r1, GraphicsUnit.Pixel);
}
else if (r.Height > img.Height && r.Width > img.Width / Totalindex)
{
r1 = new Rectangle(x, y, lr.Left, lr.Top);
r2 = new Rectangle(x1, y1, lr.Left, lr.Top);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x, y + img.Height - lr.Bottom, lr.Left, lr.Bottom);
r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, lr.Left, lr.Bottom);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x, y + lr.Top, lr.Left, img.Height - lr.Top - lr.Bottom);
r2 = new Rectangle(x1, y1 + lr.Top, lr.Left, r.Height - lr.Top - lr.Bottom);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + lr.Left, y, img.Width / Totalindex - lr.Left - lr.Right, lr.Top);
r2 = new Rectangle(x1 + lr.Left, y1, r.Width - lr.Left - lr.Right, lr.Top);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, lr.Top);
r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, lr.Top);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + lr.Top, lr.Right, img.Height - lr.Top - lr.Bottom);
r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + lr.Top, lr.Right, r.Height - lr.Top - lr.Bottom);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + img.Height - lr.Bottom, lr.Right, lr.Bottom);
r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + r.Height - lr.Bottom, lr.Right, lr.Bottom);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + lr.Left, y + img.Height - lr.Bottom, img.Width / Totalindex - lr.Left - lr.Right, lr.Bottom);
r2 = new Rectangle(x1 + lr.Left, y1 + r.Height - lr.Bottom, r.Width - lr.Left - lr.Right, lr.Bottom);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
r1 = new Rectangle(x + lr.Left, y + lr.Top, img.Width / Totalindex - lr.Left - lr.Right, img.Height - lr.Top - lr.Bottom);
r2 = new Rectangle(x1 + lr.Left, y1 + lr.Top, r.Width - lr.Left - lr.Right, r.Height - lr.Top - lr.Bottom);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
}
}
public static void DrawRect(Graphics g, Bitmap img, Rectangle r, int index, int Totalindex)
{
if (img == null) return;
int width = img.Width / Totalindex;
int height = img.Height;
Rectangle r1, r2;
int x = (index - 1) * width;
int y = 0;
int x1 = r.Left;
int y1 = r.Top;
r1 = new Rectangle(x, y, width, height);
r2 = new Rectangle(x1, y1, r.Width, r.Height);
g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
}
public static Rectangle HAlignWithin(Size alignThis, Rectangle withinThis, ContentAlignment align)
{
if ((align & anyRight) != (ContentAlignment)0)
{
wit