ToolTip提示美化控件----------WinForm控件开发系列

2021-02-06 11:17

阅读:335

标签:rop   chord   ref   rgb   tps   模式   内容   pos   cte   

技术图片

  /// 
  /// 提示信息控件
  /// 
  [ToolboxItem(true)]
  [DefaultProperty("TitleShow")]
  [DefaultEvent("Popup")]
  [Description("提示信息控件")]
  public partial class ToolTipExt : ToolTip
  {
    #region

    private ToolTipAnchor toolAnchor = ToolTipAnchor.TopCenter;
    /// 
    /// 提示框位置
    /// 
    [Browsable(false)]
    [DefaultValue(ToolTipAnchor.TopCenter)]
    [Description("提示框位置")]
    public ToolTipAnchor ToolAnchor
    {
      get { return this.toolAnchor; }
      set
      {
        if (this.toolAnchor == value)
          return;
        this.toolAnchor = value;
        this.InitializeComponent();
      }
    }

    private int anchorDistance = 20;
    /// 
    /// 提示框位置距离
    /// 
    [DefaultValue(20)]
    [Description("提示框位置距离")]
    public int AnchorDistance
    {
      get { return this.anchorDistance; }
      set
      {
        if (this.anchorDistance == value || value 0)
          return;
        this.anchorDistance = value;
        this.InitializeComponent();
      }
    }

    private int padding = 3;
    /// 
    /// 内边距
    /// 
    [DefaultValue(3)]
    [Description("内边距")]
    public int Padding
    {
      get { return this.padding; }
      set
      {
        if (this.padding == value || value 0)
          return;
        this.padding = value;
        this.InitializeComponent();
      }
    }

    private Size minSize = new Size(20, 10);
    /// 
    /// 内容最小大小
    /// 
    [DefaultValue(typeof(Size), "20,10")]
    [Description("内容最小大小")]
    public Size MinSize
    {
      get { return this.minSize; }
      set
      {
        if (this.minSize == value || value.Width 0 || value.Height 0)
          return;
        this.minSize = value;
        this.InitializeComponent();
      }
    }

    private Size maxSize = new Size(0, 0);
    /// 
    /// 内容最大大小
    /// 
    [DefaultValue(typeof(Size), "0,0")]
    [Description("内容最大大小")]
    public Size MaxSize
    {
      get { return this.maxSize; }
      set
      {
        if (this.maxSize == value || value.Width 0 || value.Height 0)
          return;
        this.maxSize = value;
        this.InitializeComponent();
      }
    }

    #region 标题

    private bool titleShow = false;
    /// 
    /// 是否显示标题
    /// 
    [DefaultValue(false)]
    [Description("是否显示标题")]
    public bool TitleShow
    {
      get { return this.titleShow; }
      set
      {
        if (this.titleShow == value)
          return;
        this.titleShow = value;
        this.InitializeComponent();
      }
    }

    private int titleHeight = 26;
    /// 
    /// 标题高度
    /// 
    [DefaultValue(26)]
    [Description("标题高度")]
    public int TitleHeight
    {
      get { return this.titleHeight; }
      set
      {
        if (this.titleHeight == value)
          return;
        this.titleHeight = value;
        this.InitializeComponent();
      }
    }

    private TiTleAnchor titleStation = TiTleAnchor.Top;
    /// 
    /// 提示框标题位置
    /// 
    [DefaultValue(TiTleAnchor.Top)]
    [Description("提示框标题位置")]
    public TiTleAnchor TitleStation
    {
      get { return this.titleStation; }
      set
      {
        if (this.titleStation == value)
          return;
        this.titleStation = value;
        this.InitializeComponent();
      }
    }

    private Font titleFont = new Font("宋体", 11);
    /// 
    /// 标题字体
    /// 
    [DefaultValue(typeof(Font), "宋体, 11pt")]
    [Description("标题字体")]
    public Font TitleFont
    {
      get { return this.titleFont; }
      set
      {
        if (this.titleFont == value)
          return;
        this.titleFont = value;
        this.InitializeComponent();
      }
    }

    private Color titleBackColor = Color.FromArgb(192, 206, 55);
    /// 
    /// 标题背景颜色
    /// 
    [DefaultValue(typeof(Color), "192, 206, 55")]
    [Description("标题背景颜色")]
    public Color TitleBackColor
    {
      get { return this.titleBackColor; }
      set
      {
        if (this.titleBackColor == value)
          return;
        this.titleBackColor = value;
        this.InitializeComponent();
      }
    }

    private Color titleColor = Color.FromArgb(255, 255, 255);
    /// 
    /// 标题颜色
    /// 
    [DefaultValue(typeof(Color), "255, 255, 255")]
    [Description("标题颜色")]
    public Color TitleColor
    {
      get { return this.titleColor; }
      set
      {
        if (this.titleColor == value)
          return;
        this.titleColor = value;
        this.InitializeComponent();
      }
    }

    #endregion

    #region 文本

    private Font font = new Font("宋体", 10);
    /// 
    /// 文本字体
    /// 
    [DefaultValue(typeof(Font), "宋体, 10pt")]
    [Description("文本字体")]
    public Font Font
    {
      get { return this.font; }
      set
      {
        if (this.font == value)
          return;
        this.font = value;
        this.InitializeComponent();
      }
    }

    #endregion

    #endregion

    public ToolTipExt()
    {
      this.OwnerDraw = true;
      this.BackColor = Color.FromArgb(255, 255, 255);
      this.ForeColor = Color.FromArgb(245, 168, 154);
      this.Popup += new PopupEventHandler(this.ToolTipExt_Popup);
      this.Draw += new System.Windows.Forms.DrawToolTipEventHandler(this.ToolTipExt_Draw);
    }

    private void ToolTipExt_Popup(object sender, PopupEventArgs e)
    {
      e.ToolTipSize = this.GetToolTipSize(e.AssociatedControl);
    }

    private void ToolTipExt_Draw(object sender, DrawToolTipEventArgs e)
    {

      #region 背景

      SolidBrush back_sb = new SolidBrush(this.BackColor);
      e.Graphics.FillRectangle(back_sb, e.Bounds);
      back_sb.Dispose();

      #endregion

      #region 标题

      Rectangle titleback_rect = new Rectangle();
      if (this.TitleShow)
      {
        StringFormat title_sf = new StringFormat();
        if (this.TitleStation == TiTleAnchor.Left || this.TitleStation == TiTleAnchor.Right)
          title_sf.FormatFlags = StringFormatFlags.DirectionVertical;
        Size title_size = e.Graphics.MeasureString(this.ToolTipTitle, this.TitleFont, 0, title_sf).ToSize();


        Rectangle title_rect = new Rectangle();
        if (this.TitleStation == TiTleAnchor.Top)
        {
          titleback_rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, this.TitleHeight);
          title_rect = new Rectangle(titleback_rect.X + this.Padding, titleback_rect.Y + (titleback_rect.Height - title_size.Height) / 2, titleback_rect.Width - this.Padding * 2, titleback_rect.Height - this.Padding * 2);
        }
        else if (this.TitleStation == TiTleAnchor.Bottom)
        {
          titleback_rect = new Rectangle(e.Bounds.X, e.Bounds.Bottom - this.TitleHeight, e.Bounds.Width, this.TitleHeight);
          title_rect = new Rectangle(titleback_rect.X + this.Padding, e.Bounds.Bottom - titleback_rect.Height + (titleback_rect.Height - title_size.Height) / 2, titleback_rect.Width - this.Padding * 2, titleback_rect.Height - this.Padding * 2);
        }
        else if (this.TitleStation == TiTleAnchor.Left)
        {
          titleback_rect = new Rectangle(e.Bounds.X, e.Bounds.Y, this.TitleHeight, e.Bounds.Height);
          title_rect = new Rectangle(titleback_rect.X + (titleback_rect.Width - title_size.Width) / 2, titleback_rect.Y + this.Padding, titleback_rect.Width - this.Padding * 2, titleback_rect.Height - this.Padding * 2);
        }
        else if (this.TitleStation == TiTleAnchor.Right)
        {
          titleback_rect = new Rectangle(e.Bounds.Right - this.TitleHeight, e.Bounds.Y, this.TitleHeight, e.Bounds.Height);
          title_rect = new Rectangle(titleback_rect.Right - titleback_rect.Width + (titleback_rect.Width - title_size.Width) / 2, titleback_rect.Y + this.Padding, titleback_rect.Width - this.Padding * 2, titleback_rect.Height - this.Padding * 2);
        }

        if (this.TitleBackColor != Color.Empty)
        {
          SolidBrush titleback_sb = new SolidBrush(this.TitleBackColor);
          e.Graphics.FillRectangle(titleback_sb, titleback_rect);
          titleback_sb.Dispose();
        }

        SolidBrush title_sb = new SolidBrush(this.TitleColor);
        e.Graphics.DrawString(this.ToolTipTitle, this.TitleFont, title_sb, title_rect, title_sf);
        title_sb.Dispose();
        title_sf.Dispose();
      }

      #endregion

      #region 内容

      Size text_size = e.Graphics.MeasureString(e.ToolTipText, this.Font).ToSize();
      Rectangle text_rect = new Rectangle();
      if (this.TitleStation == TiTleAnchor.Top)
      {
        text_rect = new Rectangle(e.Bounds.X + this.Padding, titleback_rect.Bottom + this.Padding, e.Bounds.Width, e.Bounds.Height - titleback_rect.Height);
      }
      else if (this.TitleStation == TiTleAnchor.Bottom)
      {
        text_rect = new Rectangle(e.Bounds.X + this.Padding, e.Bounds.Y + this.Padding, e.Bounds.Width, e.Bounds.Height - titleback_rect.Height);
      }
      else if (this.TitleStation == TiTleAnchor.Left)
      {
        text_rect = new Rectangle(e.Bounds.X + titleback_rect.Width + this.Padding, e.Bounds.Y + this.Padding, e.Bounds.Width, e.Bounds.Height - titleback_rect.Height);
      }
      else if (this.TitleStation == TiTleAnchor.Right)
      {
        text_rect = new Rectangle(e.Bounds.X + this.Padding, e.Bounds.Y + this.Padding, e.Bounds.Width, e.Bounds.Height - titleback_rect.Height);
      }

      SolidBrush text_sb = new SolidBrush(this.ForeColor);
      e.Graphics.DrawString(e.ToolTipText, this.Font, text_sb, text_rect);
      text_sb.Dispose();

      #endregion

    }

    /// 
    /// 设置与指定控件关联的工具提示文本,然后在指定的相对位置以模式方式显示该工具提示。
    /// 
    /// 包含新工具提示文本的 System.String。
    /// 要为其显示工具提示的 System.Windows.Forms.Control。
    /// 工具提示的位置
    public void Show(string text, IWin32Window window, ToolTipAnchor anchor)
    {
      this.toolAnchor = anchor;
      Control control = (Control)window;
      Size size = this.GetToolTipSize(control, text);
      Point point = new Point(0, 0);
      if (this.ToolAnchor == ToolTipAnchor.TopCenter)
      {
        point = new Point((control.Width - size.Width) / 2, -this.AnchorDistance - size.Height);
      }
      else if (this.ToolAnchor == ToolTipAnchor.BottomCenter)
      {
        point = new Point((control.Width - size.Width) / 2, control.Height + this.AnchorDistance);
      }
      else if (this.ToolAnchor == ToolTipAnchor.LeftCenter)
      {
        point = new Point(-(size.Width + this.AnchorDistance), (control.Height - size.Height) / 2);
      }
      else if (this.ToolAnchor == ToolTipAnchor.RightCenter)
      {
        point = new Point(control.Width + this.AnchorDistance, (control.Height - size.Height) / 2);
      }
      this.Show(text, window, point);
    }

    /// 
    /// 设置与指定控件关联的工具提示文本,然后在指定的相对位置以模式方式显示该工具提示。
    /// 
    /// 包含新工具提示文本的 System.String。
    /// 要为其显示工具提示的 System.Windows.Forms.Control。
    /// 工具提示的位置
    /// 包含工具提示持续显示时间(以毫秒为单位)的 System.Int32。
    public void Show(string text, IWin32Window window, ToolTipAnchor anchor, int duration)
    {
      this.toolAnchor = anchor;
      Control control = (Control)window;
      Size size = this.GetToolTipSize(control, text);
      Point point = new Point(0, 0);
      if (this.ToolAnchor == ToolTipAnchor.TopCenter)
      {
        point = new Point((control.Width - size.Width) / 2, -this.AnchorDistance - size.Height);
      }
      else if (this.ToolAnchor == ToolTipAnchor.BottomCenter)
      {
        point = new Point((control.Width - size.Width) / 2, control.Height + this.AnchorDistance);
      }
      else if (this.ToolAnchor == ToolTipAnchor.LeftCenter)
      {
        point = new Point(-this.AnchorDistance - size.Width, (control.Height - size.Height) / 2);
      }
      else if (this.ToolAnchor == ToolTipAnchor.RightCenter)
      {
        point = new Point(control.Width + this.AnchorDistance, (control.Height - size.Height) / 2);
      }

      this.Show(text, window, point, duration);
    }

    /// 
    /// 通过文本计算工具提示大小(text为null时根据control的文本计算)
    /// 
    /// 要为其检索 System.Windows.Forms.ToolTip 文本的 System.Windows.Forms.Control。
    /// 要计算的文本
    /// 
    private Size GetToolTipSize(Control control, string text = null)
    {
      string text_str = text == null ? this.GetToolTip(control) : text;
      Size text_size = TextRenderer.MeasureText(text_str, this.Font);
      text_size.Width += this.Padding * 2;
      text_size.Height += this.Padding * 2;

      if (this.MinSize.Width > 0 && this.MinSize.Width > text_size.Width)
        text_size.Width = this.MinSize.Width;
      if (this.MinSize.Height > 0 && this.MinSize.Height > text_size.Height)
        text_size.Height = this.MinSize.Height;

      if (this.MaxSize.Width > 0 && text_size.Width > this.MaxSize.Width)
        text_size.Width = this.MaxSize.Width;
      if (this.MaxSize.Height > 0 && text_size.Height > this.MaxSize.Height)
        text_size.Height = this.MaxSize.Height;

      if (this.TitleShow)
      {
        if (this.TitleStation == TiTleAnchor.Top || this.TitleStation == TiTleAnchor.Bottom)
        {
          text_size.Height += this.TitleHeight;
        }
        else
        {
          text_size.Width += this.TitleHeight;
        }
      }
      return text_size;
    }

    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    /// 
    /// 提示框标题位置
    /// 
    [Description("提示框标题位置")]
    public enum TiTleAnchor
    {
      /// 
      /// 顶部
      /// 
      Top,
      /// 
      /// 左边
      /// 
      Left,
      /// 
      /// 右边
      /// 
      Right,
      /// 
      /// 下边
      /// 
      Bottom
    }

    /// 
    /// 提示框位置
    /// 
    [Description("提示框位置")]
    public enum ToolTipAnchor
    {
      /// 
      /// 顶部居中
      /// 
      TopCenter,
      /// 
      /// 左边居中
      /// 
      LeftCenter,
      /// 
      /// 右边居中
      /// 
      RightCenter,
      /// 
      /// 下边居中
      /// 
      BottomCenter
    }
  }

源码下载:ToolTip提示美化控件.zip

ToolTip提示美化控件----------WinForm控件开发系列

标签:rop   chord   ref   rgb   tps   模式   内容   pos   cte   

原文地址:https://www.cnblogs.com/tlmbem/p/11420004.html


评论


亲,登录后才可以留言!