ToolTip.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System.Collections;
  2. using System.ComponentModel;
  3. namespace System.Windows.Forms
  4. {
  5. public class ToolTip : IDisposable
  6. {
  7. private Hashtable strings;
  8. private Timer timer;
  9. private Control activeControl;
  10. protected System.Windows.Controls.ToolTip tooltip { get; }
  11. public bool Active { get; set; } // chart
  12. public int AutoPopDelay { get; set; } // chart
  13. public int InitialDelay { get; set; } // chart
  14. public int ReshowDelay { get; set; } // chart
  15. public bool ShowAlways { get; set; } // chart
  16. private void Control_MouseLeave(object sender, EventArgs e)
  17. {
  18. Hide(sender as Control);
  19. }
  20. public string GetToolTip(Control control) => (string)strings[control] ?? "";
  21. public void SetToolTip(Control control, string text)
  22. {
  23. if (!strings.ContainsKey(control))
  24. {
  25. control.MouseEnter += control_MouseEnter;
  26. control.MouseLeave += control_MouseLeave;
  27. control.MouseMove += control_MouseMove;
  28. control.MouseDown += control_MouseDown;
  29. }
  30. strings[control] = text;
  31. if (control.control.IsMouseOver)
  32. {
  33. activeControl = control;
  34. ShowTooltip(control);
  35. }
  36. }
  37. private void control_MouseEnter(object sender, EventArgs e)
  38. {
  39. timer.Start();
  40. activeControl = (Control)sender;
  41. }
  42. private void control_MouseLeave(object sender, EventArgs e)
  43. {
  44. timer.Stop();
  45. HideTooltip();
  46. activeControl = null;
  47. }
  48. private void control_MouseMove(object sender, MouseEventArgs e)
  49. {
  50. if (!tooltip.IsOpen)
  51. {
  52. timer.Stop();
  53. timer.Start();
  54. }
  55. }
  56. private void control_MouseDown(object sender, MouseEventArgs e)
  57. {
  58. timer.Stop();
  59. HideTooltip();
  60. }
  61. private void timer_Tick(object sender, EventArgs e)
  62. {
  63. timer.Stop();
  64. ShowTooltip(activeControl);
  65. }
  66. private void ShowTooltip(Control control)
  67. {
  68. var pos = System.Windows.Input.Mouse.GetPosition(control.control);
  69. // position under cursor
  70. Show(GetToolTip(control), control,
  71. control.control.FlowDirection == FlowDirection.RightToLeft ? (int)((control.control.ActualWidth - pos.X) * control.DpiScale) : (int)(pos.X * control.DpiScale),
  72. (int)((pos.Y + 18) * control.DpiScale));
  73. }
  74. private void HideTooltip()
  75. {
  76. tooltip.IsOpen = false;
  77. }
  78. public void Show(string text, Control control, int x, int y)
  79. {
  80. x = (int)(x / control.DpiScale);
  81. y = (int)(y / control.DpiScale);
  82. tooltip.Placement = Windows.Controls.Primitives.PlacementMode.Relative;
  83. tooltip.PlacementTarget = control.control;
  84. tooltip.HorizontalOffset = control.control.FlowDirection == FlowDirection.RightToLeft ? control.Width / control.DpiScale - x : x;
  85. tooltip.VerticalOffset = y;
  86. tooltip.Content = text;
  87. tooltip.FlowDirection = control.control.FlowDirection;
  88. tooltip.IsOpen = true;
  89. }
  90. public void Hide(Control control)
  91. {
  92. HideTooltip();
  93. }
  94. public void Dispose()
  95. {
  96. timer.Dispose();
  97. }
  98. public ToolTip()
  99. {
  100. tooltip = new();
  101. strings = new();
  102. timer = new();
  103. timer.Interval = SystemInformation.MouseHoverTime;
  104. timer.Tick += timer_Tick;
  105. }
  106. public ToolTip(IContainer container) : this() { }
  107. }
  108. }