您好,登錄后才能下訂單哦!
如何在c#項目中實現一個winform主題?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
1、一個接口,需要做主題的控件、窗體都要實現這個接口
/// <summary> /// 使用主題的控件、窗體需要實現此接口 /// </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> /// 主題設置 /// </summary> public class Theme { internal delegate void CheckedThemeEventHandle(ITheme theme); /// <summary> /// 改變主題事件 /// </summary> static internal event CheckedThemeEventHandle CheckedThemeEvent; static ITheme currentTheme; /// <summary> /// 當前主題 /// </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> /// 標題欄顏色 /// </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; } }
我這里做一個深色一個淺色主題
深色的
/// <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() { //這里做一些修改主題時候的業務 } #region 重寫運算符 /// <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 }
淺色的也一樣 只需要實現
ITheme,
IThemeBaseForm,
IThemeFrmLock,
IThemeUCFileItem
這些接口就行(定義的控件接口,這里都要進行實現)
然后添加具體的控件主題實現類
/// <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); } } }
然后就是去控件或窗體里面做事情了,實現接口Theme.IThemeControl,構造函數里面添加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事件添加內容
private void FrmLock_VisibleChanged(object sender, EventArgs e) { if (Visible) { ThisTheme = Theme.Theme.CurrentTheme; } }
實現的接口
Theme.ITheme thisTheme = null; /// <summary> /// 當前頁面正在使用的主題 /// </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; }
以上就是修改代碼,下面看調用
Theme.Theme.CurrentTheme = new Theme.Dark();
關于如何在c#項目中實現一個winform主題問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。