WingRecorder/IWingRecorder/Utilities.cs

202 lines
7.7 KiB
C#

using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Media;
namespace IWingRecorder
{
/// <summary>
/// 羽云录屏插件系统 SDK ,提供宿主与插件通信的编码基础,
/// 并提供简单的函数封装方便插件开发
/// </summary>
public partial class Utilities
{
/// <summary>
/// V 代表 Version ,即版本,那个数字就是版本号
/// B 代表 Beta ,即测试版本,如果是正式版就没有
/// </summary>
public const string PluginVersion = "V1B";
/// <summary>
/// 对于 WPF 字体样式枚举
/// </summary>
public enum TextDecorationType
{
/// <summary>
/// 无效果
/// </summary>
None,
/// <summary>
/// 下划线
/// </summary>
Underline,
/// <summary>
/// 基线
/// </summary>
Baseline,
/// <summary>
/// 删除线
/// </summary>
Strikethrough,
/// <summary>
/// 上划线
/// </summary>
OverLine
}
/// <summary>
/// 通过 WPF 文字样式获取 TextDecorationType 文本效果类型枚举
/// </summary>
/// <param name="decoration">TextDecoration 对象</param>
/// <returns>返回 TextDecorationType 枚举</returns>
public static TextDecorationType TypeOfTextDecorationItem(TextDecoration decoration)
{
if (decoration == TextDecorations.Underline[0])
{
return TextDecorationType.Underline;
}
if (decoration == TextDecorations.Baseline[0])
{
return TextDecorationType.Baseline;
}
if (decoration == TextDecorations.Strikethrough[0])
{
return TextDecorationType.Strikethrough;
}
if (decoration == TextDecorations.OverLine[0])
{
return TextDecorationType.OverLine;
}
return TextDecorationType.None;
}
/// <summary>
/// 判断 TextDecorationCollection 对象是否包含 TextDecorationType 字体样式枚举
/// </summary>
/// <param name="decorations">TextDecorationCollection 对象</param>
/// <param name="type">TextDecorationType 字体样式枚举</param>
/// <returns>如果包含返回 true</returns>
public static bool TextDecorationTypeContains(TextDecorationCollection decorations, TextDecorationType type)
{
switch (type)
{
case TextDecorationType.None:
return true;
case TextDecorationType.Underline:
return decorations.Contains(TextDecorations.Underline[0]);
case TextDecorationType.Baseline:
return decorations.Contains(TextDecorations.Baseline[0]);
case TextDecorationType.Strikethrough:
return decorations.Contains(TextDecorations.Strikethrough[0]);
case TextDecorationType.OverLine:
return decorations.Contains(TextDecorations.OverLine[0]);
default:
return false;
}
}
/// <summary>
/// 获取包含最大路径的 StringBuilder 对象
/// </summary>
/// <returns></returns>
public static StringBuilder Get_MAX_PATH_BufferStringBuilder()
=> new StringBuilder(260);
/// <summary>
/// 将 WPF 的 Color 对象转化为 ARGB 十六进制数字
/// </summary>
/// <param name="color">WPF 的 Color 对象</param>
/// <returns>ARGB 十六进制数字</returns>
public static uint ColorToRGBHex(Color color)
{
return (uint)(color.A << 24 | color.R << 16 | color.G << 8 | color.B);
}
/// <summary>
/// 忽略大小写比较字符串
/// </summary>
/// <param name="str1">字符串 1</param>
/// <param name="str2">字符串 2</param>
/// <returns>如果忽略大小写字符串相等返回 true</returns>
public static bool StringCompareIgnoreCase(string str1, string str2)
{
return string.Compare(str1, str2, true) == 0;
}
/// <summary>
/// 调用 PropertyChanged 事件函数,通知数据更改
/// </summary>
/// <param name="propertyChanged">数据更改事件处理事件</param>
/// <param name="sender">触发者</param>
/// <param name="propertyName">属性名</param>
public static void OnPropertyChanged(PropertyChangedEventHandler propertyChanged, object sender, string propertyName)
{
propertyChanged?.Invoke(sender, new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// 羽云录屏封装的 WPF 字体对象
/// </summary>
public class WingFont
{
public FontFamily FontFamily { get; set; }
public FontWeight FontWeight { get; set; }
public FontStyle FontStyle { get; set; }
public FontStretch FontStretch { get; set; }
public double FontSize { get; set; }
public TextDecorationCollection TextDecorations { get; set; }
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (TextDecorations != null)
{
foreach (var item in TextDecorations)
{
sb.Append(TypeOfTextDecorationItem(item));
sb.Append(' ');
}
}
return $"{FontFamily} | {FontWeight} | {FontStyle} | {sb}";
}
/// <summary>
/// 将 WingFont 字体转为 C++ 使用的 LOGFONT
/// 注意此转换将会损失一些 WPF 字体特性
/// </summary>
/// <returns>LOGFONT结构体</returns>
public object ToLogFont()
{
var fontstyle = FontStyle;
System.Drawing.FontStyle fontStyle = System.Drawing.FontStyle.Regular;
if (fontstyle == FontStyles.Oblique || fontstyle== FontStyles.Italic)
fontStyle |= System.Drawing.FontStyle.Italic;
var textd = TextDecorations;
if (textd != null)
{
if(TextDecorationTypeContains(textd, TextDecorationType.Underline)||
TextDecorationTypeContains(textd, TextDecorationType.Baseline))
fontStyle|= System.Drawing.FontStyle.Underline;
if(TextDecorationTypeContains(textd, TextDecorationType.Strikethrough))
fontStyle |= System.Drawing.FontStyle.Strikeout;
}
var fontweight = FontWeight;
if (fontweight != null)
{
if (fontweight == FontWeights.Bold || fontweight == FontWeights.SemiBold ||
fontweight == FontWeights.DemiBold || fontweight == FontWeights.UltraBold ||
fontweight == FontWeights.ExtraBold)
fontStyle |= System.Drawing.FontStyle.Bold;
}
System.Drawing.Font font = new System.Drawing.Font(FontFamily.Source, (float)FontSize,fontStyle);
object logFont = new object();
font.ToLogFont(logFont);
return logFont;
}
}
}
}