欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# winform主題實(shí)現(xiàn)的方法

 更新時(shí)間:2021年03月01日 11:53:49   作者:冰封一夏  
這篇文章主要介紹了c# winform主題實(shí)現(xiàn)的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

winform的主題實(shí)現(xiàn)沒(méi)有bs里面那么舒服,下面做了一個(gè)簡(jiǎn)單實(shí)現(xiàn),記錄一下。

1、一個(gè)接口,需要做主題的控件、窗體都要實(shí)現(xiàn)這個(gè)接口

/// <summary>
 /// 使用主題的控件、窗體需要實(shí)現(xiàn)此接口
 /// </summary>
 public interface IThemeControl
 {
  ITheme ThisTheme { get; set; }
  /// <summary>
  /// 重置主題
  /// </summary>
  void ResetTheme();
 }

2、一個(gè)主題接口

/// <summary>
 /// 主題
 /// </summary>
 public interface ITheme
 {
  int Code { get; }
  /// <summary>
  /// 初始化
  /// </summary>
  void Init();

 }

3、一個(gè)主題控制類(lèi)

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

4、添加一個(gè)窗體通用的主題接口

public interface IThemeBaseForm
 {
  /// <summary>
  /// 基本窗體背景色
  /// </summary>
  Color BaseFormBackgroundColor { get; }
  /// <summary>
  /// 基本窗體文字顏色
  /// </summary>
  Color BaseFormForeColor { get; }
  /// <summary>
  /// 標(biāo)題欄顏色
  /// </summary>
  Color BaseFormTitleColor { get; }
 }

5、添加對(duì)應(yīng)的窗體或控件的主題接口

窗體的樣式接口(例子)

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 重寫(xiě)運(yùn)算符
  /// <summary>
  /// 重寫(xiě)==
  /// </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>
  /// 重寫(xiě)!=
  /// </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)類(lèi)

/// <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)前頁(yè)面正在使用的主題
  /// </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();

效果

作者:冰封一夏
出處:http://www.cnblogs.com/bfyx/
HZHControls官網(wǎng):http://www.hzhcontrols.com

以上就是c# winform主題實(shí)現(xiàn)的方法的詳細(xì)內(nèi)容,更多關(guān)于c# winform主題實(shí)現(xiàn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論