123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System.Collections;
- using System.ComponentModel;
- namespace System.Windows.Forms
- {
- public class ToolTip : IDisposable
- {
- private Hashtable strings;
- private Timer timer;
- private Control activeControl;
- protected System.Windows.Controls.ToolTip tooltip { get; }
- public bool Active { get; set; } // chart
- public int AutoPopDelay { get; set; } // chart
- public int InitialDelay { get; set; } // chart
- public int ReshowDelay { get; set; } // chart
- public bool ShowAlways { get; set; } // chart
- private void Control_MouseLeave(object sender, EventArgs e)
- {
- Hide(sender as Control);
- }
- public string GetToolTip(Control control) => (string)strings[control] ?? "";
- public void SetToolTip(Control control, string text)
- {
- if (!strings.ContainsKey(control))
- {
- control.MouseEnter += control_MouseEnter;
- control.MouseLeave += control_MouseLeave;
- control.MouseMove += control_MouseMove;
- control.MouseDown += control_MouseDown;
- }
- strings[control] = text;
- if (control.control.IsMouseOver)
- {
- activeControl = control;
- ShowTooltip(control);
- }
- }
- private void control_MouseEnter(object sender, EventArgs e)
- {
- timer.Start();
- activeControl = (Control)sender;
- }
- private void control_MouseLeave(object sender, EventArgs e)
- {
- timer.Stop();
- HideTooltip();
- activeControl = null;
- }
- private void control_MouseMove(object sender, MouseEventArgs e)
- {
- if (!tooltip.IsOpen)
- {
- timer.Stop();
- timer.Start();
- }
- }
- private void control_MouseDown(object sender, MouseEventArgs e)
- {
- timer.Stop();
- HideTooltip();
- }
- private void timer_Tick(object sender, EventArgs e)
- {
- timer.Stop();
- ShowTooltip(activeControl);
- }
- private void ShowTooltip(Control control)
- {
- var pos = System.Windows.Input.Mouse.GetPosition(control.control);
- // position under cursor
- Show(GetToolTip(control), control,
- control.control.FlowDirection == FlowDirection.RightToLeft ? (int)((control.control.ActualWidth - pos.X) * control.DpiScale) : (int)(pos.X * control.DpiScale),
- (int)((pos.Y + 18) * control.DpiScale));
- }
- private void HideTooltip()
- {
- tooltip.IsOpen = false;
- }
- public void Show(string text, Control control, int x, int y)
- {
- x = (int)(x / control.DpiScale);
- y = (int)(y / control.DpiScale);
- tooltip.Placement = Windows.Controls.Primitives.PlacementMode.Relative;
- tooltip.PlacementTarget = control.control;
- tooltip.HorizontalOffset = control.control.FlowDirection == FlowDirection.RightToLeft ? control.Width / control.DpiScale - x : x;
- tooltip.VerticalOffset = y;
- tooltip.Content = text;
- tooltip.FlowDirection = control.control.FlowDirection;
- tooltip.IsOpen = true;
- }
- public void Hide(Control control)
- {
- HideTooltip();
- }
- public void Dispose()
- {
- timer.Dispose();
- }
- public ToolTip()
- {
- tooltip = new();
- strings = new();
- timer = new();
- timer.Interval = SystemInformation.MouseHoverTime;
- timer.Tick += timer_Tick;
- }
- public ToolTip(IContainer container) : this() { }
- }
- }
|