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() { } } }