介绍
IrisSkin 是为Microsoft Visual Studio dotNET开发的最易用的界面增强dotNET(WinForm)组件包。它能完全自动的为您的应用程序添加支持换肤功能,甚至不需要更改您的设计好的Form以及添加一行代码!您也不再需要花费很多时间来使得自己的应用程序更漂亮。
IrisSkin提供一个强大的组件SkinEngine组件来帮助您完成这一切所有的工作。您需要做的,只是将一个SkinEngine组件拖放到您设计的主Form上,并且设置一些属性,然后所有的Form以及对话框,都会自动的在运行时支持换肤功能。您也可以轻易的让应用程序回复到原始Windows风格。
使用
1.在WinForm中添加资源文件后,将喜欢的皮肤拖放至里面,如图:
后台代码:
public static SkinEngine AppSkin { get; set; }
#endregion
#region 构造函数
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
InitBasic();
InitCom();
SetSkinFile(skin, LHResource.Page);
}
private void SetSkinFile(SkinEngine skin, byte[] bytes)
{
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
skin.SkinStream = memoryStream;
AppSkin = skin;
}
}
#endregion
运行效果如图
2.若想设置某个控件的背景颜色,会发现无法修改的的情况,这时候需要设置需要修改控件的tag属性即可,下面以修改Button控件为例:
Bitmap btnbmp = null;
/// <summary>
/// 修改button控件的背景颜色
/// </summary>
/// <param name="button"></param>
private void ChangButtonColor(Button button)
{
if (btnbmp == null)
{
btnbmp = new Bitmap(button.Width, button.Height);
using (Graphics g = Graphics.FromImage(btnbmp))
{
Rectangle r = new Rectangle(0, 0, btnbmp.Width, btnbmp.Height);
using (LinearGradientBrush br = new LinearGradientBrush(
r,
Color.Red,
Color.DarkRed,
LinearGradientMode.Vertical))
{
g.FillRectangle(br, r);
}
}
}
CrossThreadOperate.InvokeControlAction<Button>(button, delegate(Button btn)
{
btn.ForeColor = Color.White;
btn.BackgroundImage = btnbmp;
btn.Tag = frmMain.AppSkin.DisableTag;
});
}
/// <summary>
/// 还原button控件背景颜色
/// </summary>
/// <param name="button"></param>
public void ReturnToBtnColor(Button button)
{
CrossThreadOperate.InvokeControlAction<Button>(button, delegate(Button btn)
{
btn.UseVisualStyleBackColor = true;
btn.Tag = null;
});
}
private void SetButtonBackColor<T>(T t, Button button) where T : Control
{
foreach (Control ctrl in t.Controls)
{
if (ctrl is Button)
{
Button btnTemp = (Button)ctrl;
if (btnTemp.Name == button.Name)
ChangButtonColor(button);
else
ReturnToBtnColor(btnTemp);
}
}
}
使用代码
private void btnFInitPLC_Click(object sender, EventArgs e)
{
SetButtonBackColor<GroupBox>(gpFunction, btnFInitPLC);
SetTabPageVisible(tbcMain, tpInitPLC);
}
实现效果
相关代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
public class CrossThreadOperate
{
public static void InvokeControlAction<t>(t cont, Action<t> action) where t : Control
{
if (cont.InvokeRequired)
{
cont.Invoke(new Action<t, Action<t>>(InvokeControlAction), new object[] { cont, action });
}
else
{
action(cont);
}
}
}
参考资料
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>