WinformExtensions.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace Ips.Library.DxpLib
  10. {
  11. public static class WinformExtensions
  12. {
  13. public static void ToFullScreen(this Form form, int screen = 0)
  14. {
  15. form.WindowState = FormWindowState.Maximized;
  16. form.StartPosition = FormStartPosition.Manual;
  17. form.Bounds = Screen.AllScreens[screen].Bounds;
  18. }
  19. public static void Invoke(this Control ctrl, Action action)
  20. {
  21. if (ctrl == null || ctrl.IsDisposed || !ctrl.IsHandleCreated) return;
  22. if (ctrl.InvokeRequired)
  23. {
  24. ctrl.Invoke(action);
  25. }
  26. else
  27. {
  28. action();
  29. }
  30. }
  31. public static void BeginInvoke(this Control ctrl, Action action)
  32. {
  33. if (ctrl == null || ctrl.IsDisposed || !ctrl.IsHandleCreated) return;
  34. if (ctrl.InvokeRequired)
  35. {
  36. ctrl.BeginInvoke(action);
  37. }
  38. else
  39. {
  40. action();
  41. }
  42. }
  43. public static T FindParent<T>(this Control ctrl) where T : Control
  44. {
  45. T result = null;
  46. Control curCtrl = ctrl;
  47. while (true)
  48. {
  49. if (curCtrl == null) break;
  50. if (curCtrl.Parent is T)
  51. {
  52. result = (T)curCtrl.Parent;
  53. break;
  54. }
  55. else
  56. {
  57. curCtrl = curCtrl.Parent;
  58. }
  59. }
  60. return result;
  61. }
  62. public static bool IsDesignMode(this Control ctrl)
  63. {
  64. bool returnFlag = false;
  65. #if DEBUG
  66. if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
  67. {
  68. returnFlag = true;
  69. }
  70. else if (Process.GetCurrentProcess().ProcessName == "devenv")
  71. {
  72. returnFlag = true;
  73. }
  74. #endif
  75. return returnFlag;
  76. }
  77. }
  78. }