You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewUtils.cs 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Shadowsocks.Controller;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Drawing.Imaging;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Windows.Forms;
  10. namespace Shadowsocks.Util
  11. {
  12. public static class ViewUtils
  13. {
  14. public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control
  15. {
  16. if (control.Controls.Count == 0)
  17. {
  18. return Enumerable.Empty<TControl>();
  19. }
  20. var children = control.Controls.OfType<TControl>().ToList();
  21. return children.SelectMany(GetChildControls<TControl>).Concat(children);
  22. }
  23. public static IEnumerable<MenuItem> GetMenuItems(Menu m)
  24. {
  25. if (m?.MenuItems == null || m.MenuItems.Count == 0) return Enumerable.Empty<MenuItem>();
  26. var children = new List<MenuItem>();
  27. foreach (var item in m.MenuItems)
  28. {
  29. children.Add((MenuItem)item);
  30. }
  31. return children.SelectMany(GetMenuItems).Concat(children);
  32. }
  33. // Workaround NotifyIcon's 63 chars limit
  34. // https://stackoverflow.com/questions/579665/how-can-i-show-a-systray-tooltip-longer-than-63-chars
  35. public static void SetNotifyIconText(NotifyIcon ni, string text)
  36. {
  37. if (text.Length >= 128)
  38. throw new ArgumentOutOfRangeException("Text limited to 127 characters");
  39. Type t = typeof(NotifyIcon);
  40. BindingFlags hidden = BindingFlags.NonPublic | BindingFlags.Instance;
  41. t.GetField("text", hidden).SetValue(ni, text);
  42. if ((bool)t.GetField("added", hidden).GetValue(ni))
  43. t.GetMethod("UpdateIcon", hidden).Invoke(ni, new object[] { true });
  44. }
  45. public static Bitmap AddBitmapOverlay(Bitmap original, params Bitmap[] overlays)
  46. {
  47. Bitmap bitmap = new Bitmap(original.Width, original.Height, PixelFormat.Format64bppArgb);
  48. Graphics canvas = Graphics.FromImage(bitmap);
  49. canvas.DrawImage(original, new Point(0, 0));
  50. foreach (Bitmap overlay in overlays)
  51. {
  52. canvas.DrawImage(new Bitmap(overlay, original.Size), new Point(0, 0));
  53. }
  54. canvas.Save();
  55. return bitmap;
  56. }
  57. public static Bitmap ChangeBitmapColor(Bitmap original, Color colorMask)
  58. {
  59. Bitmap newBitmap = new Bitmap(original);
  60. for (int x = 0; x < newBitmap.Width; x++)
  61. {
  62. for (int y = 0; y < newBitmap.Height; y++)
  63. {
  64. Color color = original.GetPixel(x, y);
  65. if (color.A != 0)
  66. {
  67. int red = color.R * colorMask.R / 255;
  68. int green = color.G * colorMask.G / 255;
  69. int blue = color.B * colorMask.B / 255;
  70. int alpha = color.A * colorMask.A / 255;
  71. newBitmap.SetPixel(x, y, Color.FromArgb(alpha, red, green, blue));
  72. }
  73. else
  74. {
  75. newBitmap.SetPixel(x, y, color);
  76. }
  77. }
  78. }
  79. return newBitmap;
  80. }
  81. public static Bitmap ResizeBitmap(Bitmap original, int width, int height)
  82. {
  83. Bitmap newBitmap = new Bitmap(width, height);
  84. using (Graphics g = Graphics.FromImage(newBitmap))
  85. {
  86. g.SmoothingMode = SmoothingMode.HighQuality;
  87. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  88. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  89. g.CompositingQuality = CompositingQuality.HighQuality;
  90. g.DrawImage(original, new Rectangle(0, 0, width, height));
  91. }
  92. return newBitmap;
  93. }
  94. public static int GetScreenDpi()
  95. {
  96. Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
  97. int dpi = (int)graphics.DpiX;
  98. graphics.Dispose();
  99. return dpi;
  100. }
  101. }
  102. }