我想美化窗體或者是其他控件、無外乎三種方式:
1、重寫WinForm自帶的控件视译,這需要熟練掌握GDI+ 技術(shù)、并且需要熟悉控件的各種事件及屬性归敬,還有些控件沒有太多可以重寫的東東酷含,
所以這種方式對于我而言有點(diǎn)難度,放棄汪茧![放棄的原因是有更好的方式]
2第美、使用第三方控件÷剿可惜大部分是需要付費(fèi)的什往,放棄!
3慌闭、使用皮膚控件别威。 以 “C# 皮膚控件” Google ,哇驴剔,好多啊… 突然間看到一個很熟悉的名字 “IrisSkin2.dll”省古,沒錯就是它,以前還用過的丧失,
但是由于當(dāng)時沒有好看的.ssk皮膚文件假夺,所以對它沒引起足夠的重視波势。而此時突然有了自己制作ssk文件的想法橄教,于是 借助Google強(qiáng)大的搜索引擎找到了“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
{
///
/// 設(shè)置窗體的圓角矩形
///
/// 需要設(shè)置的窗體
/// 圓角矩形的半徑
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 坐標(biāo)
///
public static int LOWORD(int value)
{
return value & 0xFFFF;
}
///
/// 取高位 Y 坐標(biāo)
///
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));
}
///
/// 畫九宮圖
///
/// 繪圖對象
/// 所需繪制的圖片
/// 目標(biāo)矩形
/// 來源矩形
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函數(shù)
將
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);// 指定標(biāo)題欄的Font鲁沥。
Application.Run(frm);
另外看SkinEngine重載的構(gòu)造函數(shù),還有如下的幾個耕魄。
嘿嘿画恰,第三個構(gòu)造函數(shù): 流… ?可以存儲加密ssk文件啦…
雖然skinBuilder 支持編譯加密的ssk文件,但是覺得效果不好吸奴,有時直接編譯出的加密ssk文件無法正常使用允扇。
}