星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SplashScreen.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = null;
  16. private static Thread _SplashThread = null;
  17. private delegate void ChangeFormTextdelegate(string s);
  18. public static void Show(Type splashFormType)
  19. {
  20. if (_SplashThread != null)
  21. return;
  22. if (splashFormType == null)
  23. {
  24. throw (new Exception());
  25. }
  26. _SplashThread = new Thread(new ThreadStart(delegate ()
  27. {
  28. CreateInstance(splashFormType);
  29. Application.Run(_SplashForm);
  30. }));
  31. _SplashThread.IsBackground = true;
  32. _SplashThread.SetApartmentState(ApartmentState.STA);
  33. _SplashThread.Start();
  34. }
  35. public static void ChangeTitle(string status)
  36. {
  37. ChangeFormTextdelegate de = new ChangeFormTextdelegate(ChangeText);
  38. _SplashForm.Invoke(de, status);
  39. }
  40. public static void Close()
  41. {
  42. if (_SplashThread == null || _SplashForm == null) return;
  43. try
  44. {
  45. _SplashForm.Invoke(new MethodInvoker(_SplashForm.Close));
  46. }
  47. catch (Exception)
  48. {
  49. }
  50. _SplashThread = null;
  51. _SplashForm = null;
  52. }
  53. private static void ChangeText(string title)
  54. {
  55. _SplashForm.Text = title.ToString();
  56. }
  57. private static void CreateInstance(Type FormType)
  58. {
  59. if (_SplashForm == null)
  60. {
  61. lock (_obj)
  62. {
  63. object obj = FormType.InvokeMember(null,
  64. BindingFlags.DeclaredOnly |
  65. BindingFlags.Public | BindingFlags.NonPublic |
  66. BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
  67. _SplashForm = obj as Form;
  68. _SplashForm.TopMost = true;
  69. _SplashForm.ShowInTaskbar = false;
  70. _SplashForm.BringToFront();
  71. _SplashForm.StartPosition = FormStartPosition.CenterScreen;
  72. if (_SplashForm == null)
  73. {
  74. throw (new Exception());
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }