【C#窗体开发】让C#窗体变漂亮——WINFORM皮肤插件
最近为想研究下WINFORM界面美化技术而在网上淘资料,没想到淘到了IRISSKIN这个东东,很好很强大。
IRISSKIN是东日软件所提供的一系列基于WINROM的美化皮肤,包括一个动态连接数据库文件irisskin2.dll、一个皮肤文件编辑器以及一系列WINFORM美化皮肤文件(ssk),这些都可以从其官方网站http://www.sunisoft.cn/进行下载。
IRISSKIN所呈现的界面效果非常不错,而且使用起来也十分简便。
首先,将你要调用的皮肤文件放入DEBUG目录下,再将DEBUG文件夹包括入你的项目之中。
然后,将添加外部工具IRISSKIN并将irisskin2.dll添加为引用。
OK,这样就差不多了,剩下的就是要添加一个SkinEngine控件,在WINFORM_LOAD事件中调用皮肤:@"Skins\你要用的皮肤文件.ssk"即可。
此外,这里再提供一个IRISSKIN的构造类:
View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
//TestSkin命令空间,别忘了改成你自己的。
namespace TestSkin
{
class SkinClass
{
public static Sunisoft.IrisSkin.SkinEngine se = null;
/// <summary>
/// 增加换肤菜单
/// </summary>
/// <param name="toolMenu"></param>
public static void AddSkinMenu(ToolStripMenuItem toolMenu)
{
DataSet skin = new DataSet();
try
{
skin.ReadXml("skin.xml", XmlReadMode.Auto);
}
catch
{
}
if (skin == null || skin.Tables.Count < 1)
{
skin = new DataSet();
skin.Tables.Add("skin");
skin.Tables["skin"].Columns.Add("style");
System.Data.DataRow dr = skin.Tables["skin"].NewRow();
dr[0] = "系统默认";
skin.Tables[0].Rows.Add(dr);
skin.WriteXml("skin.xml", XmlWriteMode.IgnoreSchema);
}
foreach (SkinType st in (SkinType[])System.Enum.GetValues(typeof(SkinType)))
{
toolMenu.DropDownItems.Add(new ToolStripMenuItem(st.ToString()));
toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1].Click += new EventHandler(frm_Main_Click);
if (st.ToString() == skin.Tables[0].Rows[0][0].ToString())
{
((ToolStripMenuItem)toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1]).Checked = true;
frm_Main_Click(toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1], null);
}
}
toolMenu.DropDownItems.Add(new ToolStripMenuItem("系统默认"));
toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1].Click += new EventHandler(frm_Main_Click);
if (skin.Tables[0].Rows[0][0].ToString() == "系统默认")
{
((ToolStripMenuItem)toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1]).Checked = true;
}
}
static void frm_Main_Click(object sender, EventArgs e)
{
for (int i = 0; i < ((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems.Count; i++)
{
if (((ToolStripMenuItem)sender).Text == ((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems[i].Text)
{
((ToolStripMenuItem)sender).CheckState = CheckState.Checked;
DataSet skin = new DataSet();
skin.Tables.Add("skin");
skin.Tables["skin"].Columns.Add("style");
System.Data.DataRow dr = skin.Tables["skin"].NewRow();
dr[0] = ((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems[i].Text;
skin.Tables[0].Rows.Add(dr);
skin.WriteXml("skin.xml", XmlWriteMode.IgnoreSchema);
}
else
{
((ToolStripMenuItem)((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems[i]).CheckState = CheckState.Unchecked;
}
}
if (((ToolStripMenuItem)sender).Text == "系统默认")
{
RemoveSkin();
DataSet skin = new DataSet();
skin.Tables.Add("skin");
skin.Tables["skin"].Columns.Add("style");
System.Data.DataRow dr = skin.Tables["skin"].NewRow();
dr[0] = "系统默认";
skin.Tables[0].Rows.Add(dr);
skin.WriteXml("skin.xml", XmlWriteMode.IgnoreSchema);
return;
}
foreach (SkinType st in (SkinType[])System.Enum.GetValues(typeof(SkinType)))
{
if (st.ToString() == ((ToolStripMenuItem)sender).Text)
{
ChangeSkin(st);
return;
}
}
}
/// <summary>
/// 改变皮肤
/// </summary>
/// <param name="st"></param>
public static void ChangeSkin(SkinType st)
{
System.Reflection.Assembly thisDll = System.Reflection.Assembly.GetExecutingAssembly();
if (se == null)
{
//TestSkin是指命名空间,这里你可以换成你自己的。
se = new Sunisoft.IrisSkin.SkinEngine(Application.OpenForms[0], thisDll.GetManifestResourceStream("TestSkin.skin." + st.ToString() + ".ssk"));
se.Active = true;
for (int i = 0; i < Application.OpenForms.Count; i++)
{
se.AddForm(Application.OpenForms[i]);
}
}
else
{
//TestSkin是指命名空间,这里你可以换成你自己的。
se.SkinStream = thisDll.GetManifestResourceStream("TestSkin.skin." + st.ToString() + ".ssk");
se.Active = true;
}
}
/// <summary>
/// 移除皮肤
/// </summary>
public static void RemoveSkin()
{
if (se == null)
{
return;
}
else
{
se.Active = false;
}
}
}
/// <summary>
/// 换肤类型
/// </summary>
public enum SkinType
{
Calmness,
DeepCyan,
Eighteen,
Emerald,
GlassBrown,
Longhorn,
MacOS,
Midsummer,
MP10,
MSN,
OneBlue,
Page,
RealOne,
Silver,
SportsBlack,
SteelBlack,
vista1,
Vista2,
Warm,
Wave,
XPSilver
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
//TestSkin命令空间,别忘了改成你自己的。
namespace TestSkin
{
class SkinClass
{
public static Sunisoft.IrisSkin.SkinEngine se = null;
/// <summary>
/// 增加换肤菜单
/// </summary>
/// <param name="toolMenu"></param>
public static void AddSkinMenu(ToolStripMenuItem toolMenu)
{
DataSet skin = new DataSet();
try
{
skin.ReadXml("skin.xml", XmlReadMode.Auto);
}
catch
{
}
if (skin == null || skin.Tables.Count < 1)
{
skin = new DataSet();
skin.Tables.Add("skin");
skin.Tables["skin"].Columns.Add("style");
System.Data.DataRow dr = skin.Tables["skin"].NewRow();
dr[0] = "系统默认";
skin.Tables[0].Rows.Add(dr);
skin.WriteXml("skin.xml", XmlWriteMode.IgnoreSchema);
}
foreach (SkinType st in (SkinType[])System.Enum.GetValues(typeof(SkinType)))
{
toolMenu.DropDownItems.Add(new ToolStripMenuItem(st.ToString()));
toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1].Click += new EventHandler(frm_Main_Click);
if (st.ToString() == skin.Tables[0].Rows[0][0].ToString())
{
((ToolStripMenuItem)toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1]).Checked = true;
frm_Main_Click(toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1], null);
}
}
toolMenu.DropDownItems.Add(new ToolStripMenuItem("系统默认"));
toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1].Click += new EventHandler(frm_Main_Click);
if (skin.Tables[0].Rows[0][0].ToString() == "系统默认")
{
((ToolStripMenuItem)toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1]).Checked = true;
}
}
static void frm_Main_Click(object sender, EventArgs e)
{
for (int i = 0; i < ((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems.Count; i++)
{
if (((ToolStripMenuItem)sender).Text == ((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems[i].Text)
{
((ToolStripMenuItem)sender).CheckState = CheckState.Checked;
DataSet skin = new DataSet();
skin.Tables.Add("skin");
skin.Tables["skin"].Columns.Add("style");
System.Data.DataRow dr = skin.Tables["skin"].NewRow();
dr[0] = ((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems[i].Text;
skin.Tables[0].Rows.Add(dr);
skin.WriteXml("skin.xml", XmlWriteMode.IgnoreSchema);
}
else
{
((ToolStripMenuItem)((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems[i]).CheckState = CheckState.Unchecked;
}
}
if (((ToolStripMenuItem)sender).Text == "系统默认")
{
RemoveSkin();
DataSet skin = new DataSet();
skin.Tables.Add("skin");
skin.Tables["skin"].Columns.Add("style");
System.Data.DataRow dr = skin.Tables["skin"].NewRow();
dr[0] = "系统默认";
skin.Tables[0].Rows.Add(dr);
skin.WriteXml("skin.xml", XmlWriteMode.IgnoreSchema);
return;
}
foreach (SkinType st in (SkinType[])System.Enum.GetValues(typeof(SkinType)))
{
if (st.ToString() == ((ToolStripMenuItem)sender).Text)
{
ChangeSkin(st);
return;
}
}
}
/// <summary>
/// 改变皮肤
/// </summary>
/// <param name="st"></param>
public static void ChangeSkin(SkinType st)
{
System.Reflection.Assembly thisDll = System.Reflection.Assembly.GetExecutingAssembly();
if (se == null)
{
//TestSkin是指命名空间,这里你可以换成你自己的。
se = new Sunisoft.IrisSkin.SkinEngine(Application.OpenForms[0], thisDll.GetManifestResourceStream("TestSkin.skin." + st.ToString() + ".ssk"));
se.Active = true;
for (int i = 0; i < Application.OpenForms.Count; i++)
{
se.AddForm(Application.OpenForms[i]);
}
}
else
{
//TestSkin是指命名空间,这里你可以换成你自己的。
se.SkinStream = thisDll.GetManifestResourceStream("TestSkin.skin." + st.ToString() + ".ssk");
se.Active = true;
}
}
/// <summary>
/// 移除皮肤
/// </summary>
public static void RemoveSkin()
{
if (se == null)
{
return;
}
else
{
se.Active = false;
}
}
}
/// <summary>
/// 换肤类型
/// </summary>
public enum SkinType
{
Calmness,
DeepCyan,
Eighteen,
Emerald,
GlassBrown,
Longhorn,
MacOS,
Midsummer,
MP10,
MSN,
OneBlue,
Page,
RealOne,
Silver,
SportsBlack,
SteelBlack,
vista1,
Vista2,
Warm,
Wave,
XPSilver
}
}
在你的主窗体里加上一个MenuStrip菜单,建一个2级菜单(如:文件→更换皮肤),然后在Form_Load事件里写上调用代码:
View Code
private void Form1_Load(object sender, EventArgs e)
{
//“更换皮肤ToolStripMenuItem” 是上面例子里“更换皮肤”这个ToolStripMenuItem的Name。
SkinClass.AddSkinMenu(更换皮肤ToolStripMenuItem);
}
{
//“更换皮肤ToolStripMenuItem” 是上面例子里“更换皮肤”这个ToolStripMenuItem的Name。
SkinClass.AddSkinMenu(更换皮肤ToolStripMenuItem);
}
这样完成之后,偶们就可以欣赏一下效果啦。。。。。。