From 1bd29440742f80eab5562c4da8132500ce7d3547 Mon Sep 17 00:00:00 2001 From: 2dust Date: Mon, 30 Sep 2019 10:11:48 +0800 Subject: [PATCH] Update BaseForm.cs --- v2rayN/v2rayN/Forms/BaseForm.cs | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/v2rayN/v2rayN/Forms/BaseForm.cs b/v2rayN/v2rayN/Forms/BaseForm.cs index 9d8f9b0e..ad757fd0 100644 --- a/v2rayN/v2rayN/Forms/BaseForm.cs +++ b/v2rayN/v2rayN/Forms/BaseForm.cs @@ -1,4 +1,5 @@ using System; +using System.Drawing; using System.Windows.Forms; using v2rayN.Mode; @@ -35,5 +36,52 @@ namespace v2rayN.Forms Utils.SaveLog($"Loading custom icon failed: {e.Message}"); } } + + protected Icon GetNotifyIcon() + { + try + { + var index = config.sysAgentEnabled ? config.listenerType : 0; + if (index <= 0) + { + return this.Icon; + } + var color = (new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Green })[index - 1]; + var text = index.ToString(); + + var width = 128; + var height = 128; + //Create bitmap, kind of canvas + Bitmap bitmap = new Bitmap(width, height); + + //var drawFont = new Font(FontFamily.Families[0], 64f, FontStyle.Bold); + //var drawBrush = new SolidBrush(color); + var pen = new Pen(color, 24); + + var graphics = Graphics.FromImage(bitmap); + //graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; + graphics.DrawIcon(this.Icon, 0, 0); + graphics.DrawEllipse(pen, new Rectangle(0, 0, width, height)); + //graphics.DrawString(text, drawFont, drawBrush, width / 4, height / 8); + + //To Save icon to disk + bitmap.Save(Utils.GetPath("temp_icon.ico"), System.Drawing.Imaging.ImageFormat.Icon); + + Icon createdIcon = Icon.FromHandle(bitmap.GetHicon()); + + //drawFont.Dispose(); + //drawBrush.Dispose(); + pen.Dispose(); + graphics.Dispose(); + bitmap.Dispose(); + + return createdIcon; + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + return this.Icon; + } + } } }