using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Text; using System.Linq; using System.Reflection.Emit; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace JinYuan.ControlLib { //public partial class TitleTime : UserControl //{ // public TitleTime() // { // InitializeComponent(); // //设置控件样式,去除缓冲 // this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // this.SetStyle(ControlStyles.DoubleBuffer, true); // this.SetStyle(ControlStyles.ResizeRedraw, true); // this.SetStyle(ControlStyles.Selectable, true); // this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); // } // private string ttfFelePath = ""; // private string ttfPath = ""; // [Browsable(true)]//是否可见 // [Category("自定义属性")] //类别/Assts/Fonts/zaozigongfangjinzhuti.ttf // [Description("设置字体路径")] // public string TtfPath // { // get { return ttfPath; } // set // { // ttfPath = value; // if (ttfPath != "") // { // ttfFelePath = System.Windows.Forms.Application.StartupPath + $"{ttfPath}"; // this.Invalidate(); // } // } // } // private string titleName = "标题名称"; // [Browsable(true)]//是否可见 // [Category("自定义属性")] //类别 // [Description("设置或显示标题名称")] // public string TitleName // { // get { return titleName; } // set // { // titleName = value; // if (ttfFelePath != "") // { // string fontPath = ttfFelePath; // LoadFont(fontPath, 24); // } // this.lb_TitleName.Text = titleName; // } // } // private Color titleNameForColor = Color.FromArgb(255, 77, 59); // [Browsable(true)] // [Category("自定义属性")] // [Description("设置或获取标题名称字体颜色")] // public Color TitleNameForColor // { // get { return titleNameForColor; } // set // { // titleNameForColor = value; // this.lb_TitleName.ForeColor = titleNameForColor; // } // } // private string title = "控件内容"; // [Browsable(true)]//是否可见 // [Category("自定义属性")] //类别 // [Description("设置或显示内容")] // public string Title // { // get { return title; } // set // { // title = value; // if (ttfFelePath != "") // { // string fontPath = ttfFelePath; // LoadFont(fontPath, 24); // } // this.lbl_Title.Text = title; // } // } // public void LoadFont(string path, float size) // { // //int fontId = AddFontResource(path); // PrivateFontCollection fontCollection = new PrivateFontCollection(); // fontCollection.AddFontFile(path); // Font myFont = new Font(fontCollection.Families[0].Name, 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); // //Font font = new Font(fontCollection.Families[0], size); // this.lbl_Title.Font = myFont; // //RemoveFontResource(path); // //return font; // } // // 辅助方法 // private class FontLoader // { // //[DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true)] // //public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)] string lpFileName); // //[DllImport("gdi32.dll", EntryPoint = "RemoveFontResourceW", SetLastError = true)] // //public static extern int RemoveFontResource([In][MarshalAs(UnmanagedType.LPWStr)] string lpFileName); // public Font LoadFont(string path, float size) // { // //int fontId = AddFontResource(path); // PrivateFontCollection fontCollection = new PrivateFontCollection(); // fontCollection.AddFontFile(path); // //Font myFont = new Font(font.Families[0].Name, 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); // Font font = new Font(fontCollection.Families[0], size); // //RemoveFontResource(path); // return font; // } // } //} public partial class TitleTime : UserControl { public TitleTime() { InitializeComponent(); //设置控件样式,去除缓冲 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } private string ttfFelePath = ""; private string ttfPath = ""; [Browsable(true)] [Category("自定义属性")] [Description("设置字体路径")] public string TtfPath { get { return ttfPath; } set { ttfPath = value; if (ttfPath != "") { ttfFelePath = System.Windows.Forms.Application.StartupPath + $"{ttfPath}"; this.Invalidate(); } } } private float titleFontSize = 24f; // 默认字体大小 [Browsable(true)] [Category("自定义属性")] [Description("设置或获取标题字体大小")] public float TitleFontSize { get { return titleFontSize; } set { titleFontSize = value; if (ttfFelePath != "") { LoadFont(ttfFelePath, titleFontSize); } this.Invalidate(); // 强制重绘控件 } } private string titleName = "标题名称"; [Browsable(true)] [Category("自定义属性")] [Description("设置或显示标题名称")] public string TitleName { get { return titleName; } set { titleName = value; if (ttfFelePath != "") { LoadFont(ttfFelePath, TitleFontSize); } this.lb_TitleName.Text = titleName; } } private Color titleNameForColor = Color.FromArgb(255, 77, 59); [Browsable(true)] [Category("自定义属性")] [Description("设置或获取标题名称字体颜色")] public Color TitleNameForColor { get { return titleNameForColor; } set { titleNameForColor = value; this.lb_TitleName.ForeColor = titleNameForColor; } } private string title = "控件内容"; [Browsable(true)] [Category("自定义属性")] [Description("设置或显示内容")] public string Title { get { return title; } set { title = value; if (ttfFelePath != "") { LoadFont(ttfFelePath, TitleFontSize); } this.lbl_Title.Text = title; } } public void LoadFont(string path, float size) { PrivateFontCollection fontCollection = new PrivateFontCollection(); fontCollection.AddFontFile(path); Font myFont = new Font(fontCollection.Families[0].Name, size, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); this.lbl_Title.Font = myFont; this.lb_TitleName.Font = myFont; // 如果需要标题名称也使用相同的字体 } } }