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 3.9 kB

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