C# 我常用的经典更换皮肤代码(IrisSkin2)

发布时间:2009年06月24日      浏览次数:950 次
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace MySoft.Inc
{
class skins_class
{
public static string filePath = "Skins\\"; //指定皮肤的文件夹
//从目录中得到所有的皮肤的类型名字字符串
public static string[] skinTypes = getSkinTypes(filePath);
private static string[] getSkinTypes(string path)
{
string[] skinTypes = null;
if (Directory.Exists(path))
{
skinTypes = Directory.GetFiles(path);
for (int i = 0; i < skinTypes.Length; i++)
{
int dashpos = skinTypes[i].LastIndexOf('\\');
int dotpos = skinTypes[i].LastIndexOf('.');
//ToUpper() 转换为大写,ToLower()转换为小写
//如果文件为*.ssk则将文件名赋予数组元素,否则为空
if (skinTypes[i].Substring(dotpos).ToString().Trim().ToLower() == ".ssk")
{
skinTypes[i] = skinTypes[i].Substring(dashpos + 1, dotpos - dashpos - 1).ToString().Trim();
}
else
{
skinTypes[i] = "";
}
}
Array.Sort(skinTypes);
}
return skinTypes;
}
public static Sunisoft.IrisSkin.SkinEngine skin = null;
/// <summary>
/// 载入或初始化已设置的皮肤
/// </summary>
/// <param name="sskFileName"></param>
public static void Skin_Load(string sskFileName)
{
if (File.Exists(sskFileName))
{
try
{
if (skin == null) //首次进行换肤前 skin为null
{
skin = new Sunisoft.IrisSkin.SkinEngine();
skin.SkinFile = sskFileName;
skin.Active = true;
}
else
{
skin.SkinFile = sskFileName;
skin.Active = true;
}
}
catch
{
}
}
}
/// <summary>
/// 增加换肤菜单
/// </summary>
/// <param name="toolMenu"></param>
public static void AddSkinMenu(ToolStripMenuItem toolMenu, string this_skins)
{
bool Menu_Checked = false;
if (skinTypes != null)
{
foreach (string st in skinTypes)
{
//当数组元素为空时,则不显示
if (st != "" && st != null)
{
toolMenu.DropDownItems.Add(new ToolStripMenuItem(st));
toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1].Click += new EventHandler(Form_Skin_Click);
if (Menu_Checked == false && st.ToString().Trim() == this_skins.ToString().Trim())
{
((ToolStripMenuItem)toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1]).CheckState = CheckState.Checked; //定位当前所使用的皮肤
Menu_Checked = true;
}
}
}
}
toolMenu.DropDownItems.Add(new ToolStripMenuItem("怀旧风情"));
toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1].Click += new EventHandler(Form_Skin_Click);
if (Menu_Checked == false) ((ToolStripMenuItem)toolMenu.DropDownItems[toolMenu.DropDownItems.Count - 1]).Checked = true; //如果缺省,则默认为无皮肤,即“怀旧风情”
}
/// <summary>
/// 点击换肤菜单相关操作
/// </summary>
static void Form_Skin_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;
}
else
{
((ToolStripMenuItem)((ToolStripMenuItem)((ToolStripMenuItem)sender).OwnerItem).DropDownItems[i]).CheckState = CheckState.Unchecked;
}
}
if (((ToolStripMenuItem)sender).Text == "怀旧风情")
{
RemoveSkin();
Skins_write("");
return;
}
foreach (string st in skinTypes)
{
if (st == ((ToolStripMenuItem)sender).Text)
{
ChangeSkin(st);
Skins_write(st);
return;
}
}
}
/// <summary>
/// 改变皮肤
/// </summary>
/// <param name="st"></param>
public static void ChangeSkin(string st)
{
if (skin == null) //首次进行换肤前 skin为null
{
skin = new Sunisoft.IrisSkin.SkinEngine();
skin.SkinFile = filePath + st + ".ssk";
skin.Active = true;
for (int i = 0; i < Application.OpenForms.Count; i++)
{
skin.AddForm(Application.OpenForms[i]);
}
}
else
{
skin.SkinFile = filePath + st + ".ssk";
skin.Active = true;
}
}
/// <summary>
/// 移除皮肤
/// </summary>
public static void RemoveSkin()
{
if (skin == null)
{
return;
}
else
{
skin.Active = false;
}
}
/// <summary>
/// 将皮肤文件名写入文件
/// </summary>
public static void Skins_write(string sskFileName)
{
string File_Path = filePath + "Skins_set.config"; //指定用于保存皮肤设置的文件名
StreamWriter sw = null;
sw = File.CreateText(File_Path);
sw.WriteLine(sskFileName);
sw.Flush();
sw.Close();
}
/// <summary>
/// 读出默认皮肤路径
/// </summary>
public static string Skins_read()
{
string File_Path = filePath + "Skins_set.config";
string content = "";
try
{
if (File.Exists(File_Path))
{
StreamReader strd = new StreamReader(File_Path);
while (strd.Peek() != -1)
{
content = strd.ReadLine();
break;
}
strd.Close();
}
}
catch { }
return content;
}
}
}
/*
//////////////////////////////////////////////////////////
//皮肤 调用示例
//////////////////////////////////////////////////////////

//将上面的代码保存为一个类文件
//然后在最初的第一个Application主窗体中调用以下代码即可,Ismdicontainer设置为True
//在主窗体里加上一个MenuStrip菜单或者ContextMenuStrip菜单,建一个2级菜单(如:文件→更换皮肤),然后在Form_Load事件里写上调用代码

string skins_default = skins_class.Skins_read(); //获取当前设定的皮肤文件名
skins_class.AddSkinMenu(Menu_skins, skins_default); //设置系统菜单,Menu_skins为子菜单的名称,如:更换皮肤
skins_class.Skin_Load(skins_class.filePath + skins_default + ".ssk"); //加载指定皮肤
//至于IrisSkin2.dll与相关的皮肤文件(*.ssk),自已搜索下载吧
//皮肤文件夹(Skins\\)
//在项目中新建文件夹Skins,然后将相关皮肤文件(*.ssk)添加至文件夹,再选择所有皮肤文件,在“属性”栏,将“复制到输出目录”设置为“如果较新则复制”。
//////////////////////////////////////////////////////////
*/
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!