[WinForm]IrisSkin皮肤的基本使用以及单独控件样式设置

介绍

IrisSkin 是为Microsoft Visual  Studio dotNET开发的最易用的界面增强dotNET(WinForm)组件包。它能完全自动的为您的应用程序添加支持换肤功能,甚至不需要更改您的设计好的Form以及添加一行代码!您也不再需要花费很多时间来使得自己的应用程序更漂亮。

IrisSkin提供一个强大的组件SkinEngine组件来帮助您完成这一切所有的工作。您需要做的,只是将一个SkinEngine组件拖放到您设计的主Form上,并且设置一些属性,然后所有的Form以及对话框,都会自动的在运行时支持换肤功能。您也可以轻易的让应用程序回复到原始Windows风格。

使用

1.在WinForm中添加资源文件后,将喜欢的皮肤拖放至里面,如图:

image

后台代码:

        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
运行效果如图

image

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);
        }

实现效果

image

image

相关代码:

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);
        }
    }
}

参考资料

irisskin

A Generic Method for Cross-thread Winforms Access


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值