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

  /// <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

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

时间: 2024-10-07 11:38:31

ToolTip提示美化控件----------WinForm控件开发系列的相关文章

TabPanel美化控件----------WinForm控件开发系列

/// <summary> /// TabControl /// </summary> [ToolboxItem(true)] [Description("TabControl")] public partial class TabControlExt : TabControl { #region private AnimationTimer animation; /// <summary> /// 动画组件对象 /// </summary&g

颜色选择美化控件----------WinForm控件开发系列

下面是控件显示界面源码,日期选择面板源码太多了2000行左右,没有贴出来,可以下载源码去看.你可以完善这个颜色控件. /// <summary> /// 颜色选择美化控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("ColorPicker")] [Description("颜色选择美化控件")] public partial class ColorExt : Control { #

雷达分析图控件----------WinForm控件开发系列

/// <summary> /// 雷达分析图控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("ChartLineItems")] [Description("雷达分析图控件")] public partial class RadarChartExt : Control { #region private ChartTypes chartType = ChartTypes.Circ

Chart实时段分析控件----------WinForm控件开发系列

/// <summary> /// 实时段进度控件 /// </summary> [ToolboxItem(true)] [Description("实时段进度控件")] public partial class ChartExt : Control { #region private Timer interval; /// <summary> /// 定时刷新 /// </summary> [Browsable(false)] [Des

数字时间控件----------WinForm控件开发系列

/// <summary> /// 数字时间控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Value")] [DefaultEvent("ValueChanged")] [Description("数字时间控件")] public partial class TimeExt : Control { public delegate void EventHand

MAC鱼眼效果菜单控件----------WinForm控件开发系列

/// <summary> /// 鱼眼菜单 /// </summary> [DefaultProperty("Items")] [DefaultEvent("FisheyeItemClick")] [Description("鱼眼菜单")] public partial class FisheyeBarExt : Control { public delegate void EventHandler(object sen

按钮动画控件----------WinForm控件开发系列

public partial class ButtonExt : Button { private AnimationTimer _Animation; /// <summary> /// 动画组件对象 /// </summary> [Description("动画组件对象")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public AnimationT

雷达扫描控件----------WinForm控件开发系列

/// <summary> /// 雷达扫描控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("Items")] [Description("雷达扫描控件")] public partial class RadarExt : Control { #region private Color areaColor = Color.LawnGreen; /// <summary>

加载等待控件----------WinForm控件开发系列

/// <summary> /// 加载进度控件 /// </summary> [ToolboxItem(true)] [Description("加载进度控件")] [DefaultProperty("Active")] public partial class LoadProgressExt : Control { #region 属性 private LoadProgressType progressType = LoadProgres