|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Drawing.Drawing2D; |
| 4 | +using System.Drawing.Text; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Windows.Forms; |
| 7 | + |
| 8 | +namespace Compact_RAM_Cleaner |
| 9 | +{ |
| 10 | + public class MemoryUsageVisualization |
| 11 | + { |
| 12 | + [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] |
| 13 | + static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse); |
| 14 | + |
| 15 | + [DllImport("Gdi32.dll", EntryPoint = "DeleteObject")] |
| 16 | + static extern IntPtr DeleteObject(IntPtr hObject); |
| 17 | + |
| 18 | + readonly Panel _panel; |
| 19 | + Color _backgroundFillColor; |
| 20 | + readonly IMemoryUsageProvider _memoryUsageProvider; |
| 21 | + |
| 22 | + public MemoryUsageVisualization(Panel panel, IMemoryUsageProvider memoryUsageProvider, Action onClick = null) |
| 23 | + { |
| 24 | + _panel = panel; |
| 25 | + _backgroundFillColor = _panel.BackColor; |
| 26 | + _memoryUsageProvider = memoryUsageProvider; |
| 27 | + |
| 28 | + PaintPanel(); |
| 29 | + |
| 30 | + if (onClick != null) |
| 31 | + { |
| 32 | + var defaultColor = _backgroundFillColor; |
| 33 | + |
| 34 | + _panel.Click += (s, e) => onClick(); |
| 35 | + |
| 36 | + _panel.MouseEnter += (s, e) => |
| 37 | + { |
| 38 | + _backgroundFillColor = Color.FromArgb(defaultColor.R + 10, defaultColor.G + 10, defaultColor.B + 10); |
| 39 | + _panel.Refresh(); |
| 40 | + }; |
| 41 | + |
| 42 | + _panel.MouseLeave += (s, e) => |
| 43 | + { |
| 44 | + _backgroundFillColor = defaultColor; |
| 45 | + _panel.Refresh(); |
| 46 | + }; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void Update() => _panel.Refresh(); |
| 51 | + |
| 52 | + void PaintPanel() |
| 53 | + { |
| 54 | + int width = _panel.Width - 2; |
| 55 | + int height = _panel.Height - 2; |
| 56 | + |
| 57 | + _panel.Paint += (s, e) => |
| 58 | + { |
| 59 | + IntPtr ptr = CreateRoundRectRgn(0, 0, _panel.Width, _panel.Height, _panel.Width, _panel.Width); |
| 60 | + _panel.Region = Region.FromHrgn(ptr); |
| 61 | + DeleteObject(ptr); |
| 62 | + |
| 63 | + e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; |
| 64 | + using (var backgroundPen = new Pen(Color.FromArgb(32, 33, 36), 2)) |
| 65 | + using (var background = new SolidBrush(_backgroundFillColor)) |
| 66 | + using (var pen = new Pen(Color.FromArgb(117, 162, 247), 4) { StartCap = LineCap.Round, EndCap = LineCap.Round }) |
| 67 | + { |
| 68 | + e.Graphics.FillPie(background, 5, 5, width - 10, height - 10, -90, 360); |
| 69 | + e.Graphics.DrawArc(backgroundPen, 4, 4, width - 8, height - 8, -90, 360); |
| 70 | + e.Graphics.DrawArc(pen, 4, 4, width - 8, height - 8, -90, (int)Math.Round(360.0 / 100 * _memoryUsageProvider.CurrentUsage)); |
| 71 | + } |
| 72 | + |
| 73 | + using (var font = new Font("Tahoma", 12F)) |
| 74 | + using (var font2 = new Font("Tahoma", 7F)) |
| 75 | + using (var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) |
| 76 | + { |
| 77 | + GetPositions(out var usagePosition, out var percentPosition); |
| 78 | + e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; |
| 79 | + e.Graphics.DrawString(_memoryUsageProvider.CurrentUsageString, font, SystemBrushes.Control, usagePosition, _panel.Height / 2, sf); |
| 80 | + e.Graphics.DrawString("%", font2, SystemBrushes.ControlDark, percentPosition, _panel.Height / 2 + 2, sf); |
| 81 | + |
| 82 | + } |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + void GetPositions(out int usagePosition, out int percentPosition) |
| 87 | + { |
| 88 | + usagePosition = _panel.Width / 2; |
| 89 | + percentPosition = _panel.Width / 2; |
| 90 | + |
| 91 | + if (_memoryUsageProvider.CurrentUsage > 10 && _memoryUsageProvider.CurrentUsage < 100) |
| 92 | + { |
| 93 | + usagePosition += -1; |
| 94 | + percentPosition += 15; |
| 95 | + } |
| 96 | + else if (_memoryUsageProvider.CurrentUsage < 10) |
| 97 | + { |
| 98 | + percentPosition += 10; |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + usagePosition += -2; |
| 103 | + percentPosition += 19; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments