首页 > Windows开发 > 详细

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

时间:2019-08-27 19:07:49      阅读:138      评论:0      收藏:0      [点我收藏+]

技术分享图片

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

    private ToolTipAnchor toolAnchor = ToolTipAnchor.TopCenter;
    /// <summary>
    /// 提示框位置
    /// </summary>
    [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;
    /// <summary>
    /// 提示框位置距离
    /// </summary>
    [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;
    /// <summary>
    /// 内边距
    /// </summary>
    [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);
    /// <summary>
    /// 内容最小大小
    /// </summary>
    [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);
    /// <summary>
    /// 内容最大大小
    /// </summary>
    [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;
    /// <summary>
    /// 是否显示标题
    /// </summary>
    [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;
    /// <summary>
    /// 标题高度
    /// </summary>
    [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;
    /// <summary>
    /// 提示框标题位置
    /// </summary>
    [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);
    /// <summary>
    /// 标题字体
    /// </summary>
    [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);
    /// <summary>
    /// 标题背景颜色
    /// </summary>
    [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);
    /// <summary>
    /// 标题颜色
    /// </summary>
    [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);
    /// <summary>
    /// 文本字体
    /// </summary>
    [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

    }

    /// <summary>
    /// 设置与指定控件关联的工具提示文本,然后在指定的相对位置以模式方式显示该工具提示。
    /// </summary>
    /// <param name="text">包含新工具提示文本的 System.String。</param>
    /// <param name="window">要为其显示工具提示的 System.Windows.Forms.Control。</param>
    /// <param name="anchor">工具提示的位置</param>
    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);
    }

    /// <summary>
    /// 设置与指定控件关联的工具提示文本,然后在指定的相对位置以模式方式显示该工具提示。
    /// </summary>
    /// <param name="text">包含新工具提示文本的 System.String。</param>
    /// <param name="window">要为其显示工具提示的 System.Windows.Forms.Control。</param>
    /// <param name="anchor">工具提示的位置</param>
    /// <param name="duration">包含工具提示持续显示时间(以毫秒为单位)的 System.Int32。</param>
    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);
    }

    /// <summary>
    /// 通过文本计算工具提示大小(text为null时根据control的文本计算)
    /// </summary>
    /// <param name="control">要为其检索 System.Windows.Forms.ToolTip 文本的 System.Windows.Forms.Control。</param>
    /// <param name="text">要计算的文本</param>
    /// <returns></returns>
    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);
    }

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

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

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!