MyStatusBar.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using BluePointLilac.Methods;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace BluePointLilac.Controls
  7. {
  8. public sealed class MyStatusBar : Panel
  9. {
  10. public static readonly string DefaultText = $"Ver: {Application.ProductVersion} {Application.CompanyName}";
  11. public MyStatusBar()
  12. {
  13. this.Text = DefaultText;
  14. this.Height = 30.DpiZoom();
  15. this.Dock = DockStyle.Bottom;
  16. this.Font = SystemFonts.StatusFont;
  17. this.BackColor = Color.FromArgb(70, 130, 200);
  18. this.ForeColor = Color.White;
  19. }
  20. [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
  21. public override string Text { get => base.Text; set => base.Text = value; }
  22. protected override void OnPaint(PaintEventArgs e)
  23. {
  24. base.OnPaint(e);
  25. string txt = this.Text;
  26. int left = this.Height / 3;
  27. for(int i = this.Text.Length - 1; i >= 0; i--)
  28. {
  29. Size size = TextRenderer.MeasureText(txt, this.Font);
  30. if(size.Width < ClientSize.Width - 2 * left)
  31. {
  32. using(Brush brush = new SolidBrush(this.ForeColor))
  33. {
  34. int top = (this.Height - size.Height) / 2;
  35. e.Graphics.Clear(this.BackColor);
  36. e.Graphics.DrawString(txt, this.Font, brush, left, top);
  37. break;
  38. }
  39. }
  40. txt = this.Text.Substring(0, i) + "...";
  41. }
  42. }
  43. protected override void OnResize(EventArgs e)
  44. {
  45. base.OnResize(e); this.Refresh();
  46. }
  47. protected override void OnTextChanged(EventArgs e)
  48. {
  49. base.OnTextChanged(e); this.Refresh();
  50. }
  51. protected override void OnFontChanged(EventArgs e)
  52. {
  53. base.OnFontChanged(e); this.Refresh();
  54. }
  55. protected override void OnForeColorChanged(EventArgs e)
  56. {
  57. base.OnForeColorChanged(e); this.Refresh();
  58. }
  59. protected override void OnBackColorChanged(EventArgs e)
  60. {
  61. base.OnBackColorChanged(e); this.Refresh();
  62. }
  63. }
  64. }