星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SplashScreen.cs 2.8KB

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