📄 thememanager.cs
字号:
namespace Imps.Client.Pc.Theme
{
using Imps.Client.Pc;
using Imps.Client.Pc.BizControls;
using Imps.Client.Pc.BizControls.NotifyWindows.Templates;
using Imps.Client.Pc.Controls;
using Imps.Client.Pc.XControls;
using Imps.Client.Pc.XControlStyle;
using Imps.Client.Resource;
using Imps.Client.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using System.Xml;
public static class ThemeManager
{
private static string[] _avaibleThemes = null;
private const string _buttonImageFileName = "buttonPreview.png";
private static string _curThemeName = string.Empty;
private const string _defaultThemeName = "default";
private static Hashtable _hash = new Hashtable();
private const string _previewImageFileName = "preview.png";
private static string _rootDir = Path.Combine(ImpsPathInfo.StartupPath, "theme");
private const string _themeCfgFileName = "config.xml";
private const string _themeDir = "theme";
private const string _themeFilePath = @"{0}\Theme\{1}\{2}";
private static Dictionary<string, string> _themes = null;
private static bool _useTheme = false;
public static void ApplyTheme(string themeName)
{
themeName = themeName.Trim();
if ((themeName != null) && (themeName.Length != 0))
{
SetToDefault();
try
{
_useTheme = true;
ThemeName = themeName;
LoadTheme();
}
catch (Exception exception)
{
_useTheme = false;
ThemeName = string.Empty;
ClientLogger.WriteException(exception);
throw;
}
}
}
public static void ChangeThemeColor(int hueDelta, double satDelta, double lumDelta)
{
if (((hueDelta != 0) || (satDelta != 0)) || (lumDelta != 0))
{
FormEx.Style.ChangeColor(hueDelta, satDelta, lumDelta);
NotifyWindowTemplateBase.Style.ChangeColor(hueDelta, satDelta, lumDelta);
}
}
private static bool CheckTheme(string cfgPath)
{
XmlDocument document = new XmlDocument();
document.Load(cfgPath);
foreach (string text in _hash.Keys)
{
if (document.GetElementsByTagName(text).Count == 0)
{
return false;
}
}
return true;
}
public static Image GetButtonImage(string themeName)
{
return GetImage(themeName, "buttonPreview.png");
}
public static string GetCfgFullPath(string themeName)
{
return Path.Combine(Path.Combine(_rootDir, themeName), "config.xml");
}
public static Color GetColor(string colorStr)
{
try
{
string[] textArray = colorStr.Split(new char[] { ',' });
return Color.FromArgb(int.Parse(textArray[0]), int.Parse(textArray[1]), int.Parse(textArray[2]));
}
catch
{
return Color.Black;
}
}
public static Icon GetIcon(string fileName)
{
return GetIcon(ThemeName, fileName);
}
public static Icon GetIcon(string themeName, string fileName)
{
try
{
return new Icon(GetImagePath(themeName, fileName));
}
catch
{
}
}
public static Image GetImage(string fileName)
{
return GetImage(ThemeName, fileName);
}
public static Image GetImage(string themeName, string fileName)
{
fileName = Path.Combine(Path.Combine(_rootDir, themeName), fileName);
if (File.Exists(fileName))
{
try
{
return Image.FromFile(fileName);
}
catch
{
return null;
}
}
return null;
}
public static ImageList GetImageList(string fileName, int col)
{
ImageList list = new ImageList();
list.ColorDepth = ColorDepth.Depth32Bit;
try
{
Image image = Image.FromFile(GetImagePath(fileName));
list.ImageSize = new Size(image.Width / col, image.Height);
list.Images.AddStrip(image);
}
catch (Exception exception)
{
ErrorLog.WriteLog(exception.Message);
}
return list;
}
public static ImageList GetImageList(Image input, int col, int row)
{
return GetImageList(input, col, row, true);
}
public static ImageList GetImageList(string fileName, int col, int row)
{
return GetImageList(ThemeName, fileName, col, row);
}
public static ImageList GetImageList(Image input, int col, int row, bool bMakeTransparent)
{
ImageList list = new ImageList();
list.ColorDepth = ColorDepth.Depth32Bit;
try
{
using (Bitmap bitmap = new Bitmap(input))
{
if (bMakeTransparent)
{
bitmap.MakeTransparent();
}
int width = input.Width / col;
int height = input.Height / row;
list.ImageSize = new Size(width, height);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
RectangleF rect = new RectangleF((float) (j * width), (float) (i * height), (float) width, (float) height);
list.Images.Add(bitmap.Clone(rect, PixelFormat.Format32bppArgb));
}
}
return list;
}
}
catch
{
}
return list;
}
public static ImageList GetImageList(string themeName, string fileName, int col, int row)
{
try
{
return GetImageList(Image.FromFile(GetImagePath(themeName, fileName)), col, row);
}
catch
{
}
return null;
}
public static string GetImagePath(string fileName)
{
return GetImagePath(ThemeName, fileName);
}
public static string GetImagePath(string themeName, string fileName)
{
return string.Format(@"{0}\Theme\{1}\{2}", ImpsPathInfo.StartupPath, themeName, fileName);
}
public static Image GetPreviewImage(string themeName)
{
return GetImage(themeName, "preview.png");
}
public static void LoadTheme()
{
XmlDocument document = new XmlDocument();
string cfgPath = GetCfgFullPath(ThemeName);
if (CheckTheme(cfgPath))
{
document.Load(cfgPath);
foreach (XmlNode node in document.DocumentElement.ChildNodes)
{
SetTheme(node as XmlElement);
}
}
}
public static bool RegisterThemeName(string themeName, StyleBase style)
{
if (_hash.Contains(themeName))
{
return false;
}
_hash.Add(themeName, style);
return true;
}
public static void SetColor(XmlElement elem, ref Color color, string attributeName)
{
if (elem.HasAttribute(attributeName))
{
color = GetColor(elem.GetAttribute(attributeName));
}
}
public static void SetImage(XmlElement elem, ref Image image, string attributeName)
{
if (elem.HasAttribute(attributeName))
{
string fileName = elem.GetAttribute(attributeName);
if (File.Exists(GetImagePath(fileName)))
{
image = GetImage(fileName);
}
}
}
public static void SetInt(XmlElement elem, ref int val, string attributeName, int defaultValue)
{
val = defaultValue;
if (elem.HasAttribute(attributeName))
{
int.TryParse(elem.GetAttribute(attributeName), ref val);
}
}
private static void SetTheme(XmlElement elem)
{
StyleBase base2 = _hash[elem.Name] as StyleBase;
if (base2 != null)
{
base2.SetStyle(elem);
}
else
{
switch (elem.Name)
{
case "XButton":
XButton.Style.SetStyle(elem);
return;
case "XTextBox":
XTextBox.Style.SetStyle(elem);
return;
case "XComboBox":
XComboBox.Style.SetStyle(elem);
return;
case "XSplitContainer":
XSplitContainer.Style.SetStyle(elem);
return;
case "XTabControl":
XTabControl.Style.SetStyle(elem);
return;
case "XIMForm":
FormEx.Style.SetStyle(elem);
return;
case "XDialog":
XIMDialog.Style.SetStyle(elem);
return;
case "BPanel":
BPanel.Style.SetStyle(elem);
return;
case "XToolStrip":
IMToolstrip.Style.SetStyle(elem);
return;
case "NotifyWindow":
NotifyWindowTemplateBase.Style.SetStyle(elem);
return;
}
}
}
private static void SetToDefault()
{
XButton.Style.SetDefaultStyle();
XTextBox.Style.SetDefaultStyle();
XComboBox.Style.SetDefaultStyle();
XSplitContainer.Style.SetDefaultStyle();
BPanel.Style.SetDefaultStyle();
}
public static void SyncImageList(ImageList srcImageList, ImageList dstImageList)
{
if (srcImageList == null)
{
if (dstImageList != null)
{
srcImageList = dstImageList;
}
}
else
{
for (int i = 0; i < dstImageList.Images.Count; i++)
{
if ((i + 1) <= srcImageList.Images.Count)
{
srcImageList.Images[i] = dstImageList.Images[i];
}
}
}
}
private static string[] AvaibleThemes
{
get
{
if (_avaibleThemes == null)
{
try
{
if (Directory.Exists(_rootDir))
{
_avaibleThemes = Directory.GetDirectories(_rootDir);
for (int i = 0; i < _avaibleThemes.Length; i++)
{
_avaibleThemes[i] = _avaibleThemes[i].Substring(_avaibleThemes[i].LastIndexOf('\\') + 1);
}
}
}
catch
{
}
}
return _avaibleThemes;
}
}
public static string DefaultThemeName
{
get
{
return "default";
}
}
public static string ThemeCfgFileName
{
get
{
return "config.xml";
}
}
public static string ThemeFilePath
{
get
{
return @"{0}\Theme\{1}\{2}";
}
}
public static string ThemeName
{
get
{
if (_curThemeName.Length > 0)
{
return _curThemeName;
}
return DefaultThemeName;
}
set
{
_curThemeName = value;
}
}
public static Dictionary<string, string> Themes
{
get
{
if (_themes == null)
{
_themes = new Dictionary<string, string>();
for (int i = 0; i < AvaibleThemes.Length; i++)
{
string filename = Path.Combine(Path.Combine(_rootDir, AvaibleThemes[i]), "config.xml");
try
{
XmlDocument document = new XmlDocument();
document.Load(filename);
if (document.DocumentElement.HasAttribute("Version") && ((document.DocumentElement.GetAttribute("Version") == "2.0") && document.DocumentElement.HasAttribute("Name")))
{
_themes.Add(document.DocumentElement.Attributes["Name"].Value, AvaibleThemes[i]);
}
}
catch
{
_themes.Add(AvaibleThemes[i], AvaibleThemes[i]);
}
}
}
return _themes;
}
}
public static bool UseTheme
{
get
{
return _useTheme;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -