您好,登錄后才能下訂單哦!
如何在c#項(xiàng)目中實(shí)現(xiàn)一個(gè)winform主題?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
1、一個(gè)接口,需要做主題的控件、窗體都要實(shí)現(xiàn)這個(gè)接口
/// <summary> /// 使用主題的控件、窗體需要實(shí)現(xiàn)此接口 /// </summary> public interface IThemeControl { ITheme ThisTheme { get; set; } /// <summary> /// 重置主題 /// </summary> void ResetTheme(); }
/// <summary> /// 主題 /// </summary> public interface ITheme { int Code { get; } /// <summary> /// 初始化 /// </summary> void Init(); }
/// <summary> /// 主題設(shè)置 /// </summary> public class Theme { internal delegate void CheckedThemeEventHandle(ITheme theme); /// <summary> /// 改變主題事件 /// </summary> static internal event CheckedThemeEventHandle CheckedThemeEvent; static ITheme currentTheme; /// <summary> /// 當(dāng)前主題 /// </summary> internal static ITheme CurrentTheme { get { return currentTheme; } set { if (value == null) return; currentTheme = value; currentTheme.Init(); if (CheckedThemeEvent != null) { CheckedThemeEvent(value); } } } /// <summary> /// 加載控件的主題 /// </summary> /// <param name="control"></param> internal static void LoadTheme(IThemeControl control) { control.ResetTheme(); } }
public interface IThemeBaseForm { /// <summary> /// 基本窗體背景色 /// </summary> Color BaseFormBackgroundColor { get; } /// <summary> /// 基本窗體文字顏色 /// </summary> Color BaseFormForeColor { get; } /// <summary> /// 標(biāo)題欄顏色 /// </summary> Color BaseFormTitleColor { get; } }
窗體的樣式接口(例子)
public interface IThemeFrmLock : IThemeBaseForm { Color FrmLock_TxtFillColor { get; } Color FrmLock_TxtRectColor { get; } Color FrmLock_TxtForeColor { get; } Color FrmLock_btnFillColor { get; } Color FrmLock_btnForeColor { get; } Color FrmLock_btnRectColor { get; } }
控件的樣式接口(例子)
public interface IThemeUCFileItem : ITheme { Color UCFileItem_BackgroundColor { get; } Color UCFileItem_ForeColor { get; } Color UCFileItem_BoxColor { get; } Image UCFileItem_Img1 { get; } Image UCFileItem_Img2 { get; } Image UCFileItem_Img3 { get; } Image UCFileItem_Img4 { get; } Image UCFileItem_Img5 { get; } }
我這里做一個(gè)深色一個(gè)淺色主題
深色的
/// <summary> /// 深色 /// </summary> public partial class Dark : ITheme, IThemeBaseForm, IThemeFrmLock, IThemeUCFileItem { public int Code { get { return 1; } } /// <summary> /// 基本窗體背景色 /// </summary> public Color BaseFormBackgroundColor { get { return Color.FromArgb(37, 41, 59); } } /// <summary> /// 基本窗體文字顏色 /// </summary> public Color BaseFormForeColor { get { return Color.White; } } public Color BaseFormTitleColor { get { return Color.FromArgb(38, 45, 67); } } /// <summary> /// 初始化操作 /// </summary> public void Init() { //這里做一些修改主題時(shí)候的業(yè)務(wù) } #region 重寫運(yùn)算符 /// <summary> /// 重寫== /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static bool operator ==(Dark lhs, ITheme rhs) { if (lhs == null && rhs == null) return true; else { if (lhs != null && rhs != null) { if (lhs.Code == rhs.Code) return true; else return false; } else return false; } } /// <summary> /// 重寫!= /// </summary> /// <param name="lhs"></param> /// <param name="rhs"></param> /// <returns></returns> public static bool operator !=(Dark lhs, ITheme rhs) { if (lhs == null && rhs == null) return false; else { if (lhs != null && rhs != null) { if (lhs.Code == rhs.Code) return false; else return true; } else return true; } } public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return false; } if (obj is ITheme) { if (Code == ((ITheme)obj).Code) return true; else return false; } else { return false; } } public override int GetHashCode() { return base.GetHashCode(); } #endregion }
淺色的也一樣 只需要實(shí)現(xiàn)
ITheme,
IThemeBaseForm,
IThemeFrmLock,
IThemeUCFileItem
這些接口就行(定義的控件接口,這里都要進(jìn)行實(shí)現(xiàn))
然后添加具體的控件主題實(shí)現(xiàn)類
/// <summary> /// FrmLock /// </summary> public partial class Dark { public Color FrmLock_TxtFillColor { get { return Color.FromArgb(34, 40, 60); } } public Color FrmLock_TxtRectColor { get { return Color.FromArgb(65, 75, 101); } } public Color FrmLock_TxtForeColor { get { return Color.White; } } public Color FrmLock_btnFillColor { get { return Color.FromArgb(46, 54, 76); } } public Color FrmLock_btnForeColor { get { return Color.FromArgb(175, 193, 225); } } public Color FrmLock_btnRectColor { get { return Color.FromArgb(65, 75, 101); } } }
然后就是去控件或窗體里面做事情了,實(shí)現(xiàn)接口Theme.IThemeControl,構(gòu)造函數(shù)里面添加CheckedThemeEvent事件
public partial class FrmLock : FrmWithTitle,Theme.IThemeControl { public FrmLock() { try { InitializeComponent(); Theme.Theme.CheckedThemeEvent += Theme_CheckedThemeEvent; } catch (Exception ex) { } } void Theme_CheckedThemeEvent(Theme.ITheme theme) { if (this.Visible) { ThisTheme = theme; } }
VisibleChanged事件添加內(nèi)容
private void FrmLock_VisibleChanged(object sender, EventArgs e) { if (Visible) { ThisTheme = Theme.Theme.CurrentTheme; } }
實(shí)現(xiàn)的接口
Theme.ITheme thisTheme = null; /// <summary> /// 當(dāng)前頁面正在使用的主題 /// </summary> public Theme.ITheme ThisTheme { get { if (thisTheme == null) { ThisTheme = Theme.Theme.CurrentTheme; } return thisTheme; } set { if (thisTheme != value) { thisTheme = value; Theme.Theme.LoadTheme(this); } } } public void ResetTheme() { var t = (Theme.IThemeFrmLock)ThisTheme; this.BackColor = t.BaseFormBackgroundColor; this.lblTitle.BackColor = t.BaseFormTitleColor; this.lblTitle.ForeColor = t.BaseFormForeColor; ucControlBase1.FillColor = t.FrmLock_TxtFillColor; ucControlBase1.RectColor = t.FrmLock_TxtRectColor; txtPW.BackColor = t.FrmLock_TxtFillColor; txtPW.ForeColor = t.FrmLock_TxtForeColor; tongyong_btnPaiZhaoPath.FillColor = t.FrmLock_btnFillColor; tongyong_btnPaiZhaoPath.RectColor = t.FrmLock_btnRectColor; tongyong_btnPaiZhaoPath.ForeColor = t.FrmLock_btnForeColor; }
以上就是修改代碼,下面看調(diào)用
Theme.Theme.CurrentTheme = new Theme.Dark();
關(guān)于如何在c#項(xiàng)目中實(shí)現(xiàn)一個(gè)winform主題問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。