frmStarter.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. using System.Diagnostics;
  12. using HXX.Scanner.Common;
  13. namespace HXX.Scanner.Starter
  14. {
  15. public partial class frmStarter : Form
  16. {
  17. public frmStarter()
  18. {
  19. InitializeComponent();
  20. }
  21. bool haveHandle = false;
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. int frameX = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - HXX.Scanner.Starter.Properties.Resources.检测中.Width) / 2;
  25. int frameY = (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - HXX.Scanner.Starter.Properties.Resources.检测中.Height) / 2;
  26. this.SetDesktopLocation(frameX, frameY);
  27. SetBits(global::HXX.Scanner.Starter.Properties.Resources.检测中);
  28. UpdateBiz();
  29. }
  30. private async void UpdateBiz()
  31. {
  32. try
  33. {
  34. var checkInfo = await biz.get_version_info();
  35. //test
  36. //checkInfo.Data.data.allowFlag = 1;
  37. //checkInfo.Data.data.force = 0;
  38. //checkInfo.Data.data.version = "1.9.50";
  39. //checkInfo.Data.data.description = @"1. 优化扫描识别 \r\n2. 修复bug \r\n3. abcdef \r\n4. aabbcc \r\n5. kknd";
  40. //test
  41. if (checkInfo != null && checkInfo.Data != null && checkInfo.Data.data != null && checkInfo.Status == 1)
  42. {
  43. if (checkInfo.Data.data.allowFlag == 1)//强制开关,开关=1才允许进入升级流程
  44. {
  45. if (biz.check_new_version(checkInfo.Data.data))//版本对比
  46. {
  47. await Task.Run(() =>
  48. {
  49. this.Invoke(new Action(() =>
  50. {
  51. this.Hide();
  52. frmCheckUpdate frm = new frmCheckUpdate(checkInfo.Data.data);
  53. frm.Show();
  54. }));
  55. });
  56. }
  57. else
  58. {
  59. await go_to_main();
  60. }
  61. }
  62. else
  63. {
  64. await go_to_main();
  65. }
  66. }
  67. else //访问升级信息报错
  68. {
  69. this.Hide();
  70. MessageBox.Show(checkInfo.Message);
  71. await go_to_main();
  72. }
  73. }
  74. catch (Exception ee)
  75. {
  76. LogManager.WriteLog(ee);
  77. this.Hide();
  78. MessageBox.Show(ee.Message);
  79. await go_to_main();
  80. }
  81. }
  82. private async Task go_to_main()
  83. {
  84. await Task.Run(() =>
  85. {
  86. this.Invoke(new Action(() =>
  87. {
  88. try
  89. {
  90. Process.Start("HXXScannerClient.exe");
  91. Environment.Exit(0);
  92. }
  93. catch (Exception ee)
  94. {
  95. MessageBox.Show(ee.Message);
  96. Environment.Exit(0);
  97. }
  98. }));
  99. });
  100. }
  101. private void InitializeStyles()
  102. {
  103. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  104. SetStyle(ControlStyles.UserPaint, true);
  105. UpdateStyles();
  106. }
  107. protected override void OnClosing(CancelEventArgs e)
  108. {
  109. e.Cancel = true;
  110. base.OnClosing(e);
  111. haveHandle = false;
  112. }
  113. protected override void OnHandleCreated(EventArgs e)
  114. {
  115. InitializeStyles();
  116. base.OnHandleCreated(e);
  117. haveHandle = true;
  118. }
  119. protected override CreateParams CreateParams
  120. {
  121. get
  122. {
  123. CreateParams cParms = base.CreateParams;
  124. cParms.ExStyle |= 0x00080000; // WS_EX_LAYERED
  125. return cParms;
  126. }
  127. }
  128. public void SetBits(Bitmap bitmap)
  129. {
  130. if (!haveHandle) return;
  131. if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
  132. throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
  133. IntPtr oldBits = IntPtr.Zero;
  134. IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
  135. IntPtr hBitmap = IntPtr.Zero;
  136. IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
  137. try
  138. {
  139. Win32.Point topLoc = new Win32.Point(Left, Top);
  140. Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
  141. Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
  142. Win32.Point srcLoc = new Win32.Point(0, 0);
  143. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  144. oldBits = Win32.SelectObject(memDc, hBitmap);
  145. blendFunc.BlendOp = Win32.AC_SRC_OVER;
  146. blendFunc.SourceConstantAlpha = 255;
  147. blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
  148. blendFunc.BlendFlags = 0;
  149. Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
  150. }
  151. finally
  152. {
  153. if (hBitmap != IntPtr.Zero)
  154. {
  155. Win32.SelectObject(memDc, oldBits);
  156. Win32.DeleteObject(hBitmap);
  157. }
  158. Win32.ReleaseDC(IntPtr.Zero, screenDC);
  159. Win32.DeleteDC(memDc);
  160. }
  161. }
  162. }
  163. }