zoukankan      html  css  js  c++  java
  • ToolTip美化扩展----------WinForm控件开发系列

    该控件是继承于 ToolTip 基类开发的。

    ToolAnchor  属性可以设置提示框位置。

    TitleStation  属性可以设置提示窗体标题位置。

      1         private void ToolTipExt_Popup(object sender, PopupEventArgs e)
      2         {
      3             e.ToolTipSize = this.GetToolTipSize(e.AssociatedControl);
      4         }
      5 
      6         private void ToolTipExt_Draw(object sender, DrawToolTipEventArgs e)
      7         {
      8             #region 背景
      9 
     10             SolidBrush back_sb = new SolidBrush(this.BackColor);
     11             e.Graphics.FillRectangle(back_sb, e.Bounds);
     12             back_sb.Dispose();
     13 
     14             #endregion
     15 
     16             #region 标题
     17 
     18             Rectangle titleback_rect = new Rectangle();
     19             if (this.TitleShow)
     20             {
     21                 StringFormat title_sf = new StringFormat();
     22                 if (this.TitleStation == TitleAnchor.Left || this.TitleStation == TitleAnchor.Right)
     23                     title_sf.FormatFlags = StringFormatFlags.DirectionVertical;
     24                 Size title_size = e.Graphics.MeasureString(this.ToolTipTitle, this.TitleFont, 0, title_sf).ToSize();
     25 
     26 
     27                 Rectangle title_rect = new Rectangle();
     28                 if (this.TitleStation == TitleAnchor.Top)
     29                 {
     30                     titleback_rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, this.TitleHeight);
     31                     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);
     32                 }
     33                 else if (this.TitleStation == TitleAnchor.Bottom)
     34                 {
     35                     titleback_rect = new Rectangle(e.Bounds.X, e.Bounds.Bottom - this.TitleHeight, e.Bounds.Width, this.TitleHeight);
     36                     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);
     37                 }
     38                 else if (this.TitleStation == TitleAnchor.Left)
     39                 {
     40                     titleback_rect = new Rectangle(e.Bounds.X, e.Bounds.Y, this.TitleHeight, e.Bounds.Height);
     41                     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);
     42                 }
     43                 else if (this.TitleStation == TitleAnchor.Right)
     44                 {
     45                     titleback_rect = new Rectangle(e.Bounds.Right - this.TitleHeight, e.Bounds.Y, this.TitleHeight, e.Bounds.Height);
     46                     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);
     47                 }
     48 
     49                 if (this.TitleBackColor != Color.Empty)
     50                 {
     51                     SolidBrush titleback_sb = new SolidBrush(this.TitleBackColor);
     52                     e.Graphics.FillRectangle(titleback_sb, titleback_rect);
     53                     titleback_sb.Dispose();
     54                 }
     55 
     56                 SolidBrush title_sb = new SolidBrush(this.TitleColor);
     57                 e.Graphics.DrawString(this.ToolTipTitle, this.TitleFont, title_sb, title_rect, title_sf);
     58                 title_sb.Dispose();
     59                 title_sf.Dispose();
     60             }
     61 
     62             #endregion
     63 
     64             #region 内容
     65 
     66             Size text_size = e.Graphics.MeasureString(e.ToolTipText, this.Font, new Size()).ToSize();
     67             Rectangle text_rect = new Rectangle();
     68             if (this.TitleStation == TitleAnchor.Top)
     69             {
     70                 text_rect = new Rectangle(e.Bounds.X + this.Padding, titleback_rect.Bottom + this.Padding, e.Bounds.Width, e.Bounds.Height - titleback_rect.Height);
     71             }
     72             else if (this.TitleStation == TitleAnchor.Bottom)
     73             {
     74                 text_rect = new Rectangle(e.Bounds.X + this.Padding, e.Bounds.Y + this.Padding, e.Bounds.Width, e.Bounds.Height - titleback_rect.Height);
     75             }
     76             else if (this.TitleStation == TitleAnchor.Left)
     77             {
     78                 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);
     79             }
     80             else if (this.TitleStation == TitleAnchor.Right)
     81             {
     82                 text_rect = new Rectangle(e.Bounds.X + this.Padding, e.Bounds.Y + this.Padding, e.Bounds.Width, e.Bounds.Height - titleback_rect.Height);
     83             }
     84 
     85             SolidBrush text_sb = new SolidBrush(this.ForeColor);
     86             e.Graphics.DrawString(e.ToolTipText, this.Font, text_sb, text_rect);
     87             text_sb.Dispose();
     88 
     89             #endregion
     90 
     91         }
     92 
     93         /// <summary>
     94         /// 通过文本计算工具提示大小(text为null时根据control的文本计算)
     95         /// </summary>
     96         /// <param name="control">要为其检索 System.Windows.Forms.ToolTip 文本的 System.Windows.Forms.Control。</param>
     97         /// <param name="text">要计算的文本</param>
     98         /// <returns></returns>
     99         private Size GetToolTipSize(Control control, string text = null)
    100         {
    101             Graphics g = control.CreateGraphics();
    102             string text_str = text == null ? this.GetToolTip(control) : text;
    103             Size text_size = g.MeasureString(text_str, this.Font, new Size()).ToSize();
    104             text_size.Width += this.Padding * 2;
    105             text_size.Height += this.Padding * 2;
    106 
    107             if (this.MinSize.Width > 0 && this.MinSize.Width > text_size.Width)
    108                 text_size.Width = this.MinSize.Width;
    109             if (this.MinSize.Height > 0 && this.MinSize.Height > text_size.Height)
    110                 text_size.Height = this.MinSize.Height;
    111 
    112             if (this.MaxSize.Width > 0 && text_size.Width > this.MaxSize.Width)
    113                 text_size.Width = this.MaxSize.Width;
    114             if (this.MaxSize.Height > 0 && text_size.Height > this.MaxSize.Height)
    115                 text_size.Height = this.MaxSize.Height;
    116 
    117             if (this.TitleShow)
    118             {
    119                 if (this.TitleStation == TitleAnchor.Top || this.TitleStation == TitleAnchor.Bottom)
    120                 {
    121                     text_size.Height += this.TitleHeight;
    122                 }
    123                 else
    124                 {
    125                     text_size.Width += this.TitleHeight;
    126                 }
    127             }
    128             g.Dispose();
    129             return text_size;
    130         }

    公开方法如下

    控件库的源码已整体发布到gitee,下载地址:(花木兰控件库)https://gitee.com/tlmbem/hml

  • 相关阅读:
    luogu P1833 樱花 看成混合背包
    luogu P1077 摆花 基础记数dp
    luogu P1095 守望者的逃离 经典dp
    Even Subset Sum Problem CodeForces
    Maximum White Subtree CodeForces
    Sleeping Schedule CodeForces
    Bombs CodeForces
    病毒侵袭持续中 HDU
    病毒侵袭 HDU
    Educational Codeforces Round 35 (Rated for Div. 2)
  • 原文地址:https://www.cnblogs.com/tlmbem/p/11420004.html
Copyright © 2011-2022 走看看