MoonTip.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Drawing;
  2. using System.Drawing.Drawing2D;
  3. using System.Windows.Forms;
  4. namespace Optimizer
  5. {
  6. class MoonTip : ToolTip
  7. {
  8. TextFormatFlags toolTipFlags = TextFormatFlags.VerticalCenter | TextFormatFlags.LeftAndRightPadding | TextFormatFlags.HorizontalCenter | TextFormatFlags.NoClipping;
  9. Font font = new Font("Segoe UI Semibold", 10f, FontStyle.Regular);
  10. public MoonTip()
  11. {
  12. this.OwnerDraw = true;
  13. this.IsBalloon = false;
  14. this.Popup += new PopupEventHandler(this.OnPopup);
  15. this.Draw += new DrawToolTipEventHandler(this.OnDraw);
  16. }
  17. private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip
  18. {
  19. string toolTipText = (sender as ToolTip).GetToolTip(e.AssociatedControl);
  20. using (var g = e.AssociatedControl.CreateGraphics())
  21. {
  22. var textSize = Size.Add(TextRenderer.MeasureText(
  23. g, toolTipText, font, Size.Empty, toolTipFlags), new Size(10, 10));
  24. e.ToolTipSize = textSize;
  25. }
  26. // e.ToolTipSize = new Size(400, 400);
  27. }
  28. private void OnDraw(object sender, DrawToolTipEventArgs e) // use this event to customise the tool tip
  29. {
  30. Graphics g = e.Graphics;
  31. LinearGradientBrush b = new LinearGradientBrush(e.Bounds,
  32. Color.FromArgb(25, 25, 25), Color.FromArgb(25, 25, 25), 45f);
  33. g.FillRectangle(b, e.Bounds);
  34. g.DrawRectangle(new Pen(Color.FromArgb(55, 55, 55), 1), new Rectangle(e.Bounds.X, e.Bounds.Y,
  35. e.Bounds.Width - 1, e.Bounds.Height - 1));
  36. g.DrawString(e.ToolTipText, font, Brushes.White,
  37. new PointF(e.Bounds.X + 6, e.Bounds.Y + 6)); // top layer
  38. b.Dispose();
  39. }
  40. }
  41. }