using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace JinYuan.ControlLib { [DefaultEvent("Click")] // 增加特性事件 public partial class NaviButton2 : UserControl { public NaviButton2() { 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); // 绑定 Resize 事件 this.Resize += NaviButton2_Resize; // 绑定 Label 的点击事件 this.lbl_Title.Click += lbl_Title_Click; } private bool isSelected = false; [Browsable(true)] // 是否可见 [Category("自定义属性")] // 类别 [Description("设置或显示导航按钮是否选中")] public bool IsSelected { get { return isSelected; } set { isSelected = value; UpdataImage(); this.Invalidate(); // 刷新控件 } } private bool isLeft = true; [Browsable(true)] // 是否可见 [Category("自定义属性")] // 类别 [Description("设置或显示导航按钮是否为左边")] public bool IsLeft { get { return isLeft; } set { isLeft = value; UpdataImage(); this.Invalidate(); // 刷新控件 } } private string titleName = "导航按钮"; [Browsable(true)] // 是否可见 [Category("自定义属性")] // 类别 [Description("设置或显示导航按钮文本内容")] public string TitleName { get { return titleName; } set { titleName = value; this.lbl_Title.Text = titleName; this.lbl_Title.AutoSize = false; // 禁用 AutoSize this.lbl_Title.Size = new Size(this.Width - 10, this.Height - 10); // 设置 Label 的大小 this.Invalidate(); // 刷新控件 } } private bool titleTextMultiLine = true; [Browsable(true)] [Category("自定义属性")] [Description("设置或显示导航按钮文本是否支持多行显示")] public bool TitleTextMultiLine { get { return titleTextMultiLine; } set { titleTextMultiLine = value; this.lbl_Title.AutoSize = false; // 禁用 AutoSize this.lbl_Title.TextMultiLine = titleTextMultiLine; // 设置多行显示 this.Invalidate(); // 刷新控件 } } private Color titleForeColor = Color.Black; [Browsable(true)] [Category("自定义属性")] [Description("设置或显示导航按钮文本的字体颜色")] public Color TitleForeColor { get { return titleForeColor; } set { titleForeColor = value; this.lbl_Title.ForeColor = titleForeColor; this.Invalidate(); // 刷新控件 } } private Font titleFont= new Font("Cambria", 10.5f, FontStyle.Bold); [Browsable(true)] [Category("自定义属性")] [Description("设置或显示导航按钮文本的字体样式")] public Font TitleFont { get { return titleFont; } set { titleFont = value; this.lbl_Title.Font = titleFont; this.Invalidate(); // 刷新控件 } } /// /// 统一更新背景 /// private void UpdataImage() { if (this.isLeft) { this.BackgroundImage = isSelected ? Properties.Resources.LeftSelected3 : Properties.Resources.LeftUnSelected3; } else { this.BackgroundImage = isSelected ? Properties.Resources.RightSelected3 : Properties.Resources.RightUnSelected3; } } [Browsable(true)] // 是否可见 [Category("自定义事件")] // 类别 [Description("设置或显示导航按钮文本内容")] public new event EventHandler Click; private void lbl_Title_Click(object sender, EventArgs e) { // 触发 Click 事件 this.Click?.Invoke(this, e); } private void NaviButton2_Resize(object sender, EventArgs e) { // 调整 Label 的大小和位置 this.lbl_Title.Size = new Size(this.Width - 10, this.Height - 10); this.lbl_Title.Location = new Point(5, 5); // 根据需要调整位置 this.Invalidate(); // 刷新控件 } } //[DefaultEvent("Click")] // 增加特性事件 //public partial class NaviButton2 : UserControl //{ // public NaviButton2() // { // 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); // // 绑定 Resize 事件 // this.Resize += NaviButton2_Resize; // } // private bool isSelected = false; // [Browsable(true)] // 是否可见 // [Category("自定义属性")] // 类别 // [Description("设置或显示导航按钮是否选中")] // public bool IsSelected // { // get { return isSelected; } // set // { // isSelected = value; // UpdataImage(); // } // } // private bool isLeft = true; // [Browsable(true)] // 是否可见 // [Category("自定义属性")] // 类别 // [Description("设置或显示导航按钮是否为左边")] // public bool IsLeft // { // get { return isLeft; } // set // { // isLeft = value; // UpdataImage(); // } // } // private string titleName = "导航按钮"; // [Browsable(true)] // 是否可见 // [Category("自定义属性")] // 类别 // [Description("设置或显示导航按钮文本内容")] // public string TitleName // { // get { return titleName; } // set // { // titleName = value; // this.lbl_Title.Text = titleName; // this.lbl_Title.AutoSize = false; // 禁用 AutoSize // this.lbl_Title.Size = new Size(this.Width - 10, this.Height - 10); // 设置 Label 的大小 // } // } // private float titleFontSize = 12f; // [Browsable(true)] // [Category("自定义属性")] // [Description("设置或显示导航按钮文本的字体大小")] // public float TitleFontSize // { // get { return titleFontSize; } // set // { // titleFontSize = value; // this.lbl_Title.Font = new Font(this.lbl_Title.Font.FontFamily, titleFontSize, this.lbl_Title.Font.Style); // } // } // private bool textMultiLine = true; // [Browsable(true)] // [Category("自定义属性")] // [Description("设置或显示导航按钮文本是否换行")] // public bool TextMultiLine // { // get { return textMultiLine; } // set // { // textMultiLine = value; // this.lbl_Title.AutoSize = false; // 禁用 AutoSize // this.lbl_Title.TextMultiLine = textMultiLine; // } // } // private Color titleForeColor = Color.Black; // [Browsable(true)] // [Category("自定义属性")] // [Description("设置或显示导航按钮文本的字体颜色")] // public Color TitleForeColor // { // get { return titleForeColor; } // set // { // titleForeColor = value; // this.lbl_Title.ForeColor = titleForeColor; // } // } // private FontStyle titleFontStyle = FontStyle.Regular; // [Browsable(true)] // [Category("自定义属性")] // [Description("设置或显示导航按钮文本的字体样式")] // public FontStyle TitleFontStyle // { // get { return titleFontStyle; } // set // { // titleFontStyle = value; // this.lbl_Title.Font = new Font(this.lbl_Title.Font.FontFamily, this.lbl_Title.Font.Size, titleFontStyle); // } // } // /// // /// 统一更新背景 // /// // private void UpdataImage() // { // if (this.isLeft) // { // this.BackgroundImage = isSelected ? Properties.Resources.LeftSelected3 : Properties.Resources.LeftUnSelected3; // } // else // { // this.BackgroundImage = isSelected ? Properties.Resources.RightSelected3 : Properties.Resources.RightUnSelected3; // } // } // [Browsable(true)] // 是否可见 // [Category("自定义事件")] // 类别 // [Description("设置或显示导航按钮文本内容")] // public new EventHandler Click; // private void lbl_Title_Click(object sender, EventArgs e) // { // this.Click?.Invoke(this, e); // } // private void NaviButton2_Resize(object sender, EventArgs e) // { // // 调整 Label 的大小和位置 // this.lbl_Title.Size = new Size(this.Width - 10, this.Height - 10); // this.lbl_Title.Location = new Point(5, 5); // 根据需要调整位置 // } //} }