我想美化窗体或者是其他控件、无外乎三种方式:
1、重写WinForm自带的控件,这需要熟练掌握GDI+ 技术、并且需要熟悉控件的各种事件及属性,还有些控件没有太多可以重写的东东,
所以这种方式对于我而言有点难度,放弃![放弃的原因是有更好的方式]
2、使用第三方控件。可惜大部分是需要付费的,放弃!
3、使用皮肤控件。 以 “C# 皮肤控件” Google ,哇,好多啊… 突然间看到一个很熟悉的名字 “IrisSkin2.dll”,没错就是它,以前还用过的,
但是由于当时没有好看的.ssk皮肤文件,所以对它没引起足够的重视。而此时突然有了自己制作ssk文件的想法,于是 借助Google强大的搜索引擎找到了“skinbuilder” 一款制作ssk皮肤文件的牛X工具。
1、重写WinForm自带的控件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Demo
{
///
/// 窗体自绘辅助类
///
public class RenderHelper
{
///
/// 设置窗体的圆角矩形
///
/// 需要设置的窗体
/// 圆角矩形的半径
public static void SetFormRoundRectRgn(Form form, int rgnRadius)
{
int hRgn = 0;
hRgn = Win32.CreateRoundRectRgn(0, 0, form.Width 1, form.Height 1, rgnRadius, rgnRadius);
Win32.SetWindowRgn(form.Handle, hRgn, true);
Win32.DeleteObject(hRgn);
}
///
/// 移动窗体
///
public static void MoveWindow(Form form)
{
Win32.ReleaseCapture();
Win32.SendMessage(form.Handle, Win32.WM_NCLBUTTONDOWN, Win32.HTCAPTION, 0);
}
///
/// 取低位 X 坐标
///
public static int LOWORD(int value)
{
return value & 0xFFFF;
}
///
/// 取高位 Y 坐标
///
public static int HIWORD(int value)
{
return value >> 16;
}
///
/// 绘制窗体边框
///
/// 需要绘制边框的窗体
/// 绘制边框所用的绘图对象
/// 边框图片
/// 边框的圆角矩形
public static void DrawFormFringe(Form destForm, Graphics g, Image fringeImg, int radius)
{
DrawNineRect(
g,
fringeImg,
new Rectangle(-radius, -radius, destForm.ClientSize.Width 2 * radius, destForm.ClientSize.Height 2 * radius),
new Rectangle(0, 0, fringeImg.Width, fringeImg.Height));
}
///
/// 画九宫图
///
/// 绘图对象
/// 所需绘制的图片
/// 目标矩形
/// 来源矩形
public static void DrawNineRect(Graphics g, Image img, Rectangle DestRect, Rectangle SrcRect)
{
int offset = 5;
Rectangle NineRect = new Rectangle(img.Width / 2 - offset, img.Height / 2 - offset, 2 * offset, 2 * offset);
int x = 0, y = 0, nWidth, nHeight;
int xSrc = 0, ySrc = 0, nSrcWidth, nSrcHeight;
int nDestWidth, nDestHeight;
nDestWidth = DestRect.Width;
nDestHeight = DestRect.Height;
// 左上-------------------------------------;
x = DestRect.Left;
y = DestRect.Top;
nWidth = NineRect.Left - SrcRect.Left;
nHeight = NineRect.Top - SrcRect.Top;
xSrc = SrcRect.Left;
ySrc = SrcRect.Top;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
// 上-------------------------------------;
x = DestRect.Left NineRect.Left - SrcRect.Left;
nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
xSrc = NineRect.Left;
nSrcWidth = NineRect.Right - NineRect.Left;
nSrcHeight = NineRect.Top - SrcRect.Top;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
// 右上-------------------------------------;
x = DestRect.Right - (SrcRect.Right - NineRect.Right);
nWidth = SrcRect.Right - NineRect.Right;
xSrc = NineRect.Right;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
// 左-------------------------------------;
x = DestRect.Left;
y = DestRect.Top NineRect.Top - SrcRect.Top;
nWidth = NineRect.Left - SrcRect.Left;
nHeight = DestRect.Bottom - y - (SrcRect.Bottom - NineRect.Bottom);
xSrc = SrcRect.Left;
ySrc = NineRect.Top;
nSrcWidth = NineRect.Left - SrcRect.Left;
nSrcHeight = NineRect.Bottom - NineRect.Top;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
// 中-------------------------------------;
x = DestRect.Left NineRect.Left - SrcRect.Left;
nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
xSrc = NineRect.Left;
nSrcWidth = NineRect.Right - NineRect.Left;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
// 右-------------------------------------;
x = DestRect.Right - (SrcRect.Right - NineRect.Right);
nWidth = SrcRect.Right - NineRect.Right;
xSrc = NineRect.Right;
nSrcWidth = SrcRect.Right - NineRect.Right;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
// 左下-------------------------------------;
x = DestRect.Left;
y = DestRect.Bottom - (SrcRect.Bottom - NineRect.Bottom);
nWidth = NineRect.Left - SrcRect.Left;
nHeight = SrcRect.Bottom - NineRect.Bottom;
xSrc = SrcRect.Left;
ySrc = NineRect.Bottom;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
// 下-------------------------------------;
x = DestRect.Left NineRect.Left - SrcRect.Left;
nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
xSrc = NineRect.Left;
nSrcWidth = NineRect.Right - NineRect.Left;
nSrcHeight = SrcRect.Bottom - NineRect.Bottom;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
// 右下-------------------------------------;
x = DestRect.Right - (SrcRect.Right - NineRect.Right);
nWidth = SrcRect.Right - NineRect.Right;
xSrc = NineRect.Right;
g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
}
///
/// 绘制窗体主体部分白色透明层
///
/// 需要绘制的窗体
/// 绘图对象
public static void DrawFromAlphaMainPart(Form form, Graphics g)
{
Color[] colors =
{
Color.FromArgb(5, Color.White),
Color.FromArgb(30, Color.White),
Color.FromArgb(150, Color.White),
Color.FromArgb(180, Color.White),
Color.FromArgb(30, Color.White),
Color.FromArgb(5, Color.White)
};
float[] pos =
{
0.0f,
0.05f,
0.15f,
0.85f,
0.99f,
1.0f
};
ColorBlend colorBlend = new ColorBlend(6);
colorBlend.Colors = colors;
colorBlend.Positions = pos;
RectangleF destRect = new RectangleF(0, 0, form.Width, form.Height);
using (LinearGradientBrush lBrush = new LinearGradientBrush(destRect, colors[0], colors[5], LinearGradientMode.Vertical))
{
lBrush.InterpolationColors = colorBlend;
g.FillRectangle(lBrush, destRect);
}
}
}
3、使用皮肤控件。
例如IrisSkin2.dll皮肤控件
1、添加引用 IrisSkin2.dll 或 IrisSkin4.dll。
2、修改 Program.cs Main函数
将
Application.Run(new Form1());
修改为
Form1 frm = new Form1();
Sunisoft.IrisSkin.SkinEngine skin = new Sunisoft.IrisSkin.SkinEngine((System.ComponentModel.Component)frm);
skin.SkinFile = "***.ssk"; // 指定皮肤文件
skin.TitleFont = new System.Drawing.Font("微软雅黑", 10F);// 指定标题栏的Font。
Application.Run(frm);
另外看SkinEngine重载的构造函数,还有如下的几个。
嘿嘿,第三个构造函数: 流… 可以存储加密ssk文件啦…
虽然skinBuilder 支持编译加密的ssk文件,但是觉得效果不好,有时直接编译出的加密ssk文件无法正常使用。
}