You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

QRCodeSplashForm.cs 10 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Drawing.Imaging;
  9. using System.Runtime.InteropServices;
  10. using System.Diagnostics;
  11. namespace Shadowsocks.View
  12. {
  13. public class QRCodeSplashForm : Form
  14. {
  15. public Rectangle TargetRect;
  16. public QRCodeSplashForm()
  17. {
  18. FormBorderStyle = FormBorderStyle.None;
  19. this.Load += QRCodeSplashForm_Load;
  20. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  21. this.BackColor = System.Drawing.Color.White;
  22. this.ClientSize = new System.Drawing.Size(1, 1);
  23. this.ControlBox = false;
  24. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
  25. this.MaximizeBox = false;
  26. this.MinimizeBox = false;
  27. this.Name = "QRCodeSplashForm";
  28. this.ShowIcon = false;
  29. this.ShowInTaskbar = false;
  30. this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
  31. this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
  32. this.TopMost = true;
  33. }
  34. private Timer timer;
  35. private int flashStep;
  36. private static double FPS = 1.0 / 15 * 1000; // System.Windows.Forms.Timer resolution is 15ms
  37. private static double ANIMATION_TIME = 0.5;
  38. private static int ANIMATION_STEPS = (int)(ANIMATION_TIME * FPS);
  39. Stopwatch sw;
  40. int x;
  41. int y;
  42. int w;
  43. int h;
  44. Bitmap bitmap;
  45. Graphics g;
  46. Pen pen;
  47. SolidBrush brush;
  48. private void QRCodeSplashForm_Load(object sender, EventArgs e)
  49. {
  50. SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  51. this.BackColor = Color.Transparent;
  52. flashStep = 0;
  53. x = 0;
  54. y = 0;
  55. w = Width;
  56. h = Height;
  57. sw = Stopwatch.StartNew();
  58. timer = new Timer();
  59. timer.Interval = (int)(ANIMATION_TIME * 1000 / ANIMATION_STEPS);
  60. timer.Tick += timer_Tick;
  61. timer.Start();
  62. bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
  63. g = Graphics.FromImage(bitmap);
  64. pen = new Pen(Color.Red, 3);
  65. brush = new SolidBrush(Color.FromArgb(30, Color.Red));
  66. }
  67. void timer_Tick(object sender, EventArgs e)
  68. {
  69. double percent = (double)sw.ElapsedMilliseconds / 1000.0 / (double)ANIMATION_TIME;
  70. if (percent < 1)
  71. {
  72. // ease out
  73. percent = 1 - Math.Pow((1 - percent), 4);
  74. x = (int)(TargetRect.X * percent);
  75. y = (int)(TargetRect.Y * percent);
  76. w = (int)(TargetRect.Width * percent + this.Size.Width * (1 - percent));
  77. h = (int)(TargetRect.Height * percent + this.Size.Height * (1 - percent));
  78. //codeRectView.Location = new Point(x, y);
  79. //codeRectView.Size = new Size(w, h);
  80. pen.Color = Color.FromArgb((int)(255 * percent), Color.Red);
  81. brush.Color = Color.FromArgb((int)(30 * percent), Color.Red);
  82. g.Clear(Color.Transparent);
  83. g.FillRectangle(brush, x, y, w, h);
  84. g.DrawRectangle(pen, x, y, w, h);
  85. SetBitmap(bitmap);
  86. }
  87. else
  88. {
  89. if (flashStep == 0)
  90. {
  91. timer.Interval = 100;
  92. g.Clear(Color.Transparent);
  93. SetBitmap(bitmap);
  94. }
  95. else if (flashStep == 1)
  96. {
  97. timer.Interval = 50;
  98. g.FillRectangle(brush, x, y, w, h);
  99. g.DrawRectangle(pen, x, y, w, h);
  100. SetBitmap(bitmap);
  101. }
  102. else if (flashStep == 2)
  103. {
  104. g.Clear(Color.Transparent);
  105. SetBitmap(bitmap);
  106. }
  107. else if (flashStep == 3)
  108. {
  109. g.FillRectangle(brush, x, y, w, h);
  110. g.DrawRectangle(pen, x, y, w, h);
  111. SetBitmap(bitmap);
  112. }
  113. else if (flashStep == 4)
  114. {
  115. g.Clear(Color.Transparent);
  116. SetBitmap(bitmap);
  117. }
  118. else if (flashStep == 5)
  119. {
  120. g.FillRectangle(brush, x, y, w, h);
  121. g.DrawRectangle(pen, x, y, w, h);
  122. SetBitmap(bitmap);
  123. }
  124. else
  125. {
  126. sw.Stop();
  127. timer.Stop();
  128. pen.Dispose();
  129. brush.Dispose();
  130. bitmap.Dispose();
  131. this.Close();
  132. }
  133. flashStep++;
  134. }
  135. }
  136. // PerPixelAlphaForm.cs
  137. // http://www.codeproject.com/Articles/1822/Per-Pixel-Alpha-Blend-in-C
  138. // Rui Lopes
  139. protected override CreateParams CreateParams
  140. {
  141. get
  142. {
  143. CreateParams cp = base.CreateParams;
  144. cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
  145. return cp;
  146. }
  147. }
  148. public void SetBitmap(Bitmap bitmap)
  149. {
  150. SetBitmap(bitmap, 255);
  151. }
  152. /// <para>Changes the current bitmap with a custom opacity level. Here is where all happens!</para>
  153. public void SetBitmap(Bitmap bitmap, byte opacity)
  154. {
  155. if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
  156. throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
  157. // The idea of this is very simple,
  158. // 1. Create a compatible DC with screen;
  159. // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
  160. // 3. Call the UpdateLayeredWindow.
  161. IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
  162. IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
  163. IntPtr hBitmap = IntPtr.Zero;
  164. IntPtr oldBitmap = IntPtr.Zero;
  165. try
  166. {
  167. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
  168. oldBitmap = Win32.SelectObject(memDc, hBitmap);
  169. Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
  170. Win32.Point pointSource = new Win32.Point(0, 0);
  171. Win32.Point topPos = new Win32.Point(Left, Top);
  172. Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
  173. blend.BlendOp = Win32.AC_SRC_OVER;
  174. blend.BlendFlags = 0;
  175. blend.SourceConstantAlpha = opacity;
  176. blend.AlphaFormat = Win32.AC_SRC_ALPHA;
  177. Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
  178. }
  179. finally
  180. {
  181. Win32.ReleaseDC(IntPtr.Zero, screenDc);
  182. if (hBitmap != IntPtr.Zero)
  183. {
  184. Win32.SelectObject(memDc, oldBitmap);
  185. //Windows.DeleteObject(hBitmap); // The documentation says that we have to use the Windows.DeleteObject... but since there is no such method I use the normal DeleteObject from Win32 GDI and it's working fine without any resource leak.
  186. Win32.DeleteObject(hBitmap);
  187. }
  188. Win32.DeleteDC(memDc);
  189. }
  190. }
  191. }
  192. // class that exposes needed win32 gdi functions.
  193. class Win32
  194. {
  195. [StructLayout(LayoutKind.Sequential)]
  196. public struct Point
  197. {
  198. public Int32 x;
  199. public Int32 y;
  200. public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
  201. }
  202. [StructLayout(LayoutKind.Sequential)]
  203. public struct Size
  204. {
  205. public Int32 cx;
  206. public Int32 cy;
  207. public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
  208. }
  209. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  210. struct ARGB
  211. {
  212. public byte Blue;
  213. public byte Green;
  214. public byte Red;
  215. public byte Alpha;
  216. }
  217. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  218. public struct BLENDFUNCTION
  219. {
  220. public byte BlendOp;
  221. public byte BlendFlags;
  222. public byte SourceConstantAlpha;
  223. public byte AlphaFormat;
  224. }
  225. public const Int32 ULW_COLORKEY = 0x00000001;
  226. public const Int32 ULW_ALPHA = 0x00000002;
  227. public const Int32 ULW_OPAQUE = 0x00000004;
  228. public const byte AC_SRC_OVER = 0x00;
  229. public const byte AC_SRC_ALPHA = 0x01;
  230. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  231. public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
  232. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  233. public static extern IntPtr GetDC(IntPtr hWnd);
  234. [DllImport("user32.dll", ExactSpelling = true)]
  235. public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  236. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  237. public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  238. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  239. public static extern int DeleteDC(IntPtr hdc);
  240. [DllImport("gdi32.dll", ExactSpelling = true)]
  241. public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  242. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  243. public static extern int DeleteObject(IntPtr hObject);
  244. }
  245. }