ExToolTip.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.ComponentModel;
  7. namespace LYFZ.ComponentLibrary
  8. {
  9. public class ExToolTip : System.Windows.Forms.ToolTip
  10. {
  11. public ExToolTip()
  12. {
  13. this.OwnerDraw = true;
  14. this.Draw += ExToolTip_Draw;
  15. this.Popup += ExToolTip_Popup;
  16. }
  17. Font toolTipFont = new Font("微软雅黑", 16);
  18. /// <summary>
  19. /// 设置提示文字大小
  20. /// </summary>
  21. [DescriptionAttribute("设置提示文字大小"), CategoryAttribute("组件扩展属性")]
  22. public Font ToolTipFont
  23. {
  24. get { return toolTipFont; }
  25. set { toolTipFont = value; }
  26. }
  27. void ExToolTip_Popup(object sender, System.Windows.Forms.PopupEventArgs e)
  28. {
  29. try
  30. {
  31. if (e.AssociatedControl.Tag != null && e.AssociatedControl.Tag.GetType() == typeof(ExToolTipText))
  32. {
  33. ExToolTipText tooltipText = (ExToolTipText)e.AssociatedControl.Tag;
  34. //e.ToolTipSize = System.Windows.Forms.TextRenderer.MeasureText(tooltipText.ToolTipText, toolTipFont);
  35. //e.ToolTipSize = new Size(Convert.ToInt32(e.ToolTipSize.Width * 0.5), Convert.ToInt32(e.ToolTipSize.Height * 0.5));
  36. Size si = System.Windows.Forms.TextRenderer.MeasureText(tooltipText.ToolTipText, toolTipFont);
  37. e.ToolTipSize = new Size(si.Width + 10, si.Height + 10);
  38. }
  39. }
  40. catch
  41. {
  42. }
  43. }
  44. void ExToolTip_Draw(object sender, System.Windows.Forms.DrawToolTipEventArgs e)
  45. {
  46. e.DrawBackground();
  47. e.DrawBorder();
  48. e.Graphics.DrawString(e.ToolTipText, toolTipFont, Brushes.Black, new PointF(2, 2));
  49. }
  50. }
  51. /// <summary>
  52. /// 工具提示文本对象
  53. /// </summary>
  54. public class ExToolTipText
  55. {
  56. public ExToolTipText(string toolTipText, object tag = null)
  57. {
  58. this.tag = tag;
  59. this.toolTipText = toolTipText;
  60. }
  61. object tag = null;
  62. public object Tag
  63. {
  64. get { return tag; }
  65. set { tag = value; }
  66. }
  67. string toolTipText = "";
  68. /// <summary>
  69. /// 自定义工具提示文字
  70. /// </summary>
  71. public string ToolTipText
  72. {
  73. get { return toolTipText; }
  74. set { toolTipText = value; }
  75. }
  76. }
  77. }