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

8 years ago
8 years ago
8 years ago
1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Windows.Forms;
  6. namespace Shadowsocks.Util
  7. {
  8. public static class ViewUtils
  9. {
  10. public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control
  11. {
  12. if (control.Controls.Count == 0)
  13. {
  14. return Enumerable.Empty<TControl>();
  15. }
  16. var children = control.Controls.OfType<TControl>().ToList();
  17. return children.SelectMany(GetChildControls<TControl>).Concat(children);
  18. }
  19. // Workaround NotifyIcon's 63 chars limit
  20. // https://stackoverflow.com/questions/579665/how-can-i-show-a-systray-tooltip-longer-than-63-chars
  21. public static void SetNotifyIconText(NotifyIcon ni, string text)
  22. {
  23. if (text.Length >= 128)
  24. throw new ArgumentOutOfRangeException("Text limited to 127 characters");
  25. Type t = typeof(NotifyIcon);
  26. BindingFlags hidden = BindingFlags.NonPublic | BindingFlags.Instance;
  27. t.GetField("text", hidden).SetValue(ni, text);
  28. if ((bool)t.GetField("added", hidden).GetValue(ni))
  29. t.GetMethod("UpdateIcon", hidden).Invoke(ni, new object[] { true });
  30. }
  31. }
  32. }