fork download
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6.  
  7. namespace alphatest
  8. {
  9. public class AlphaWindow : Form
  10. {
  11. private Timer t;
  12. private Bitmap b, ob;
  13. public AlphaWindow(Bitmap bitmap)
  14. {
  15. b = bitmap;
  16. TopMost = true;
  17. ShowInTaskbar = false;
  18. Size = bitmap.Size;
  19. StartPosition = FormStartPosition.CenterScreen;
  20. Show();
  21.  
  22. t = new Timer {Interval = 50};
  23. t.Tick += t_Tick;
  24. ob = new Bitmap(b.Width, b.Height);
  25. t.Start();
  26. }
  27.  
  28. private float op = 0.00001f;
  29. void t_Tick(object sender, EventArgs e)
  30. {
  31. Graphics g = Graphics.FromImage(ob);
  32. if(op<1)op *= 1.2f;
  33. ColorMatrix m = new ColorMatrix { Matrix33 = op };
  34.  
  35. ImageAttributes at = new ImageAttributes();
  36. at.SetColorMatrix(m, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  37. g.Clear(Color.Transparent);
  38. g.DrawImage(b, new Rectangle(0, 0, b.Width, b.Height), 0, 0, b.Width, b.Height, GraphicsUnit.Pixel, at);
  39. SelectBitmap(ob);
  40. g.Dispose();
  41. }
  42.  
  43. public void SelectBitmap(Bitmap bitmap)
  44. {
  45. if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
  46. {
  47. throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
  48. }
  49. IntPtr screenDc = APIHelp.GetDC(IntPtr.Zero);
  50. IntPtr memDc = APIHelp.CreateCompatibleDC(screenDc);
  51. IntPtr hBitmap = IntPtr.Zero;
  52. IntPtr hOldBitmap = IntPtr.Zero;
  53. try
  54. {
  55. // Get handle to the new bitmap and select it into the current device context
  56. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
  57. hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);
  58.  
  59. // Set parameters for layered window update
  60. APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height); // Size window to match bitmap
  61. APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
  62. APIHelp.Point newLocation = new APIHelp.Point(Left, Top); // Same as this window
  63. APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION
  64. {
  65. BlendOp = APIHelp.AC_SRC_OVER,
  66. BlendFlags = 0,
  67. SourceConstantAlpha = 255,
  68. AlphaFormat = APIHelp.AC_SRC_ALPHA
  69. };
  70.  
  71. // Update the window
  72. APIHelp.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize,
  73. memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
  74. }
  75. finally
  76. {
  77. // Release device context
  78. APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
  79. if (hBitmap != IntPtr.Zero)
  80. {
  81. APIHelp.SelectObject(memDc, hOldBitmap);
  82. APIHelp.DeleteObject(hBitmap); // Remove bitmap resources
  83. }
  84. APIHelp.DeleteDC(memDc);
  85. }
  86.  
  87. }
  88. Point p = new Point(-1, -1);
  89.  
  90.  
  91. protected override void OnMouseMove(MouseEventArgs e)
  92. {
  93. if (e.Button == MouseButtons.Left)
  94. {
  95. if (p.X == -1)
  96. p = e.Location;
  97. else
  98. Location = new Point(Location.X + (e.X - p.X), Location.Y + (e.Y - p.Y));
  99. }
  100. else
  101. {
  102. p = new Point(-1, -1);
  103. }
  104. base.OnMouseMove(e);
  105. }
  106.  
  107. protected override CreateParams CreateParams
  108. {
  109. get
  110. {
  111. // Add the layered extended style (WS_EX_LAYERED) to this window
  112. CreateParams createParams = base.CreateParams;
  113. createParams.ExStyle |= APIHelp.WS_EX_LAYERED;
  114. return createParams;
  115. }
  116. }
  117.  
  118. // Let Windows drag this window for us (thinks its hitting the title bar of the window)
  119. //protected override void WndProc(ref Message message)
  120. //{
  121. // if (message.Msg == APIHelp.WM_NCHITTEST)
  122. // {
  123. // // Tell Windows that the user is on the title bar (caption)
  124. // //message.Result = (IntPtr)APIHelp.HTCAPTION;
  125. // }
  126. // /*else if(message.Msg == 3)
  127. // {
  128. // return;
  129. // }*/
  130. // else
  131. // {
  132. // base.WndProc(ref message);
  133. // }
  134. //}
  135. private void InitializeComponent()
  136. {
  137. SuspendLayout();
  138. //
  139. // AlphaWindow
  140. //
  141. AutoValidate = AutoValidate.EnablePreventFocusChange;
  142. ClientSize = new Size(284, 262);
  143. FormBorderStyle = FormBorderStyle.None;
  144. Name = "AlphaWindow";
  145. ShowInTaskbar = false;
  146. Load += Splash_Load;
  147. MouseDown += AlphaWindow_MouseDown;
  148. MouseMove += AlphaWindow_MouseMove;
  149. ResumeLayout(false);
  150.  
  151. }
  152.  
  153. private void Splash_Load(object sender, EventArgs e)
  154. {
  155.  
  156. }
  157.  
  158. protected override void OnClick(EventArgs e)
  159. {
  160. base.OnClick(e);
  161. //Close();
  162. }
  163.  
  164. private void AlphaWindow_MouseDown(object sender, MouseEventArgs e)
  165. {
  166. OnDragOver(null);
  167. }
  168.  
  169. private void AlphaWindow_MouseMove(object sender, MouseEventArgs e)
  170. {
  171. }
  172.  
  173. }
  174.  
  175. // Class to assist with Win32 API calls
  176. class APIHelp
  177. {
  178. // Required constants
  179. public const Int32 WS_EX_LAYERED = 0x80000;
  180. public const Int32 HTCAPTION = 0x02;
  181. public const Int32 WM_NCHITTEST = 0x84;
  182. public const Int32 ULW_ALPHA = 0x02;
  183. public const byte AC_SRC_OVER = 0x00;
  184. public const byte AC_SRC_ALPHA = 0x01;
  185.  
  186. public enum Bool
  187. {
  188. False = 0,
  189. True = 1
  190. }
  191.  
  192. [StructLayout(LayoutKind.Sequential)]
  193. public struct Point
  194. {
  195. public Int32 x;
  196. public Int32 y;
  197.  
  198. public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
  199. }
  200.  
  201. [StructLayout(LayoutKind.Sequential)]
  202. public struct Size
  203. {
  204. public Int32 cx;
  205. public Int32 cy;
  206.  
  207. public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
  208. }
  209.  
  210. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  211. struct ARGB
  212. {
  213. public byte Blue;
  214. public byte Green;
  215. public byte Red;
  216. public byte Alpha;
  217. }
  218.  
  219. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  220. public struct BLENDFUNCTION
  221. {
  222. public byte BlendOp;
  223. public byte BlendFlags;
  224. public byte SourceConstantAlpha;
  225. public byte AlphaFormat;
  226. }
  227.  
  228. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  229. public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
  230.  
  231. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  232. public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  233.  
  234. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  235. public static extern IntPtr GetDC(IntPtr hWnd);
  236.  
  237. [DllImport("user32.dll", ExactSpelling = true)]
  238. public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  239.  
  240. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  241. public static extern Bool DeleteDC(IntPtr hdc);
  242.  
  243. [DllImport("gdi32.dll", ExactSpelling = true)]
  244. public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  245.  
  246. [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  247. public static extern Bool DeleteObject(IntPtr hObject);
  248.  
  249. }
  250. }
  251.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(5,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
prog.cs(9,32): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
prog.cs(11,17): error CS0246: The type or namespace name `Timer' could not be found. Are you missing `System.Threading' or `System.Timers' using directive?
prog.cs(91,45): error CS0246: The type or namespace name `MouseEventArgs' could not be found. Are you missing an assembly reference?
prog.cs(107,28): error CS0246: The type or namespace name `CreateParams' could not be found. Are you missing an assembly reference?
prog.cs(158,33): error CS0115: `alphatest.AlphaWindow.OnClick(System.EventArgs)' is marked as an override but no suitable method found to override
prog.cs(164,59): error CS0246: The type or namespace name `MouseEventArgs' could not be found. Are you missing an assembly reference?
prog.cs(169,59): error CS0246: The type or namespace name `MouseEventArgs' could not be found. Are you missing an assembly reference?
Compilation failed: 8 error(s), 0 warnings
stdout
Standard output is empty