星火微课系统客户端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SplashScreen.cs 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Reflection;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. namespace Common.system
  6. {
  7. /// <summary>
  8. /// 解决闪屏 加载等待
  9. /// 创建人:赵耀
  10. /// 创建时间:2018年11月27日
  11. /// </summary>
  12. public class SplashScreen
  13. {
  14. private static object _obj = new object();
  15. private static Form _splashForm;
  16. private static Thread _splashThread;
  17. private delegate void ChangeFormTextdelegate(string s);
  18. public static void Show(Type splashFormType)
  19. {
  20. if (_splashThread != null)
  21. {
  22. return;
  23. }
  24. if (splashFormType == null)
  25. {
  26. throw (new Exception());
  27. }
  28. _splashThread = new Thread(
  29. delegate()
  30. {
  31. CreateInstance(splashFormType);
  32. Application.Run(_splashForm);
  33. }
  34. ) { IsBackground = true };
  35. _splashThread.SetApartmentState(ApartmentState.STA);
  36. _splashThread.Start();
  37. }
  38. public static void ChangeTitle(string status)
  39. {
  40. ChangeFormTextdelegate de = ChangeText;
  41. _splashForm.Invoke(de, status);
  42. }
  43. public static void Close()
  44. {
  45. if (_splashThread == null || _splashForm == null)
  46. {
  47. return;
  48. }
  49. try
  50. {
  51. _splashForm.Invoke(new MethodInvoker(_splashForm.Close));
  52. }
  53. catch (Exception)
  54. {
  55. // ignored
  56. }
  57. _splashThread = null;
  58. _splashForm = null;
  59. }
  60. private static void ChangeText(string title)
  61. {
  62. _splashForm.Text = title;
  63. }
  64. private static void CreateInstance(Type formType)
  65. {
  66. if (_splashForm == null)
  67. {
  68. lock (_obj)
  69. {
  70. object obj = formType.InvokeMember(
  71. null,
  72. BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
  73. null,
  74. null,
  75. null
  76. );
  77. _splashForm = obj as Form;
  78. if (_splashForm != null)
  79. {
  80. _splashForm.TopMost = true;
  81. _splashForm.ShowInTaskbar = false;
  82. _splashForm.BringToFront();
  83. _splashForm.StartPosition = FormStartPosition.CenterScreen;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }