Advertisement
Guest User

Untitled

a guest
Oct 18th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.81 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.Windows.Forms;
  5. using System.Runtime.InteropServices;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8.  
  9. namespace mynamespace
  10. {
  11.     class MyPerPixelAlphaForm : Form
  12.     {
  13.         IntPtr hBitmap = IntPtr.Zero;
  14.         IntPtr oldBitmap = IntPtr.Zero;
  15.         IntPtr screenDc = IntPtr.Zero;
  16.         IntPtr memDc = IntPtr.Zero;
  17.         Bitmap mbitmap;
  18.  
  19.         private List<Label> dynamiclabel = new List<Label>();
  20.  
  21.         // Let Windows drag this form for us
  22.         public MyPerPixelAlphaForm()
  23.         {
  24.             // This form should not have a border or else Windows will clip it.
  25.             FormBorderStyle = FormBorderStyle.None;
  26.         }
  27.  
  28.  
  29.         /// <para>Changes the current bitmap.</para>
  30.         public void SetBitmap(Bitmap bitmap)
  31.         {
  32.             SetBitmap(bitmap, 255);
  33.         }
  34.  
  35.         /// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
  36.         public void SetBitmap(Bitmap bitmap, byte opacity)
  37.         {
  38.             if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
  39.                 throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
  40.  
  41.             // The ideia of this is very simple,
  42.             // 1. Create a compatible DC with screen;
  43.             // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
  44.             // 3. Call the UpdateLayeredWindow.
  45.  
  46.             hBitmap = IntPtr.Zero;
  47.             oldBitmap = IntPtr.Zero;
  48.             screenDc = Win32.GetDC(IntPtr.Zero);
  49.             memDc = Win32.CreateCompatibleDC(screenDc);
  50.             mbitmap = bitmap;
  51.  
  52.             try
  53.             {
  54.                 hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
  55.                 oldBitmap = Win32.SelectObject(memDc, hBitmap);
  56.  
  57.                 Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
  58.                 Win32.Point pointSource = new Win32.Point(0, 0);
  59.                 Win32.Point topPos = new Win32.Point(Left, Top);
  60.                 Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
  61.                 blend.BlendOp = Win32.AC_SRC_OVER;
  62.                 blend.BlendFlags = 0;
  63.                 blend.SourceConstantAlpha = opacity;
  64.                 blend.AlphaFormat = Win32.AC_SRC_ALPHA;
  65.                
  66.                 Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
  67.             }
  68.             finally
  69.             {
  70.                 Win32.ReleaseDC(IntPtr.Zero, screenDc);
  71.                 if (hBitmap != IntPtr.Zero)
  72.                 {
  73.                     Win32.SelectObject(memDc, oldBitmap);
  74.                     //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.
  75.                     Win32.DeleteObject(hBitmap);
  76.                 }
  77.                 Win32.DeleteDC(memDc);
  78.             }
  79.         }
  80.  
  81.         // can also edit existing images if id exists
  82.         public void add_Image(Bitmap img, int posx, int posy, int id)
  83.         {
  84.         }
  85.  
  86.         // can also edit existing text if id exists
  87.         public void add_Text(String text, int posx, int posy, Color col, Font mfont, int id)
  88.         {
  89.             if (id < 0)
  90.             {
  91.                 Label temp = new Label();
  92.                 temp.Text = text;
  93.                 temp.Location = new Point(posx, posy);
  94.                 temp.Font = mfont;
  95.                 temp.ForeColor = col;
  96.  
  97.                 this.Controls.Add(temp);
  98.                 temp.BringToFront();
  99.  
  100.                 dynamiclabel.Add(temp);
  101.             }
  102.             else
  103.             {
  104.                 dynamiclabel[id].Text = text;
  105.                 dynamiclabel[id].Location = new Point(posx, posy);
  106.                 dynamiclabel[id].Font = mfont;
  107.                 dynamiclabel[id].ForeColor = col;
  108.             }
  109.         }
  110.  
  111.         protected override CreateParams CreateParams
  112.         {
  113.             get
  114.             {
  115.                 CreateParams cp = base.CreateParams;
  116.                 cp.ExStyle |= 0x00080000; // This form has to have the WS_EX_LAYERED extended style
  117.                 return cp;
  118.             }
  119.         }
  120.     }
  121.  
  122.     class Win32
  123.     {
  124.         public enum Bool
  125.         {
  126.             False = 0,
  127.             True
  128.         };
  129.  
  130.  
  131.         [StructLayout(LayoutKind.Sequential)]
  132.         public struct Point
  133.         {
  134.             public Int32 x;
  135.             public Int32 y;
  136.  
  137.             public Point(Int32 x, Int32 y) { this.x = x; this.y = y; }
  138.         }
  139.  
  140.  
  141.         [StructLayout(LayoutKind.Sequential)]
  142.         public struct Size
  143.         {
  144.             public Int32 cx;
  145.             public Int32 cy;
  146.  
  147.             public Size(Int32 cx, Int32 cy) { this.cx = cx; this.cy = cy; }
  148.         }
  149.  
  150.  
  151.         [StructLayout(LayoutKind.Sequential, Pack = 1)]
  152.         struct ARGB
  153.         {
  154.             public byte Blue;
  155.             public byte Green;
  156.             public byte Red;
  157.             public byte Alpha;
  158.         }
  159.  
  160.  
  161.         [StructLayout(LayoutKind.Sequential, Pack = 1)]
  162.         public struct BLENDFUNCTION
  163.         {
  164.             public byte BlendOp;
  165.             public byte BlendFlags;
  166.             public byte SourceConstantAlpha;
  167.             public byte AlphaFormat;
  168.         }
  169.  
  170.  
  171.         public const Int32 ULW_COLORKEY = 0x00000001;
  172.         public const Int32 ULW_ALPHA = 0x00000002;
  173.         public const Int32 ULW_OPAQUE = 0x00000004;
  174.  
  175.         public const byte AC_SRC_OVER = 0x00;
  176.         public const byte AC_SRC_ALPHA = 0x01;
  177.  
  178.  
  179.         [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  180.         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);
  181.  
  182.         [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  183.         public static extern IntPtr GetDC(IntPtr hWnd);
  184.  
  185.         [DllImport("user32.dll", ExactSpelling = true)]
  186.         public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  187.  
  188.         [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  189.         public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
  190.  
  191.         [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  192.         public static extern Bool DeleteDC(IntPtr hdc);
  193.  
  194.         [DllImport("gdi32.dll", ExactSpelling = true)]
  195.         public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
  196.  
  197.         [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
  198.         public static extern Bool DeleteObject(IntPtr hObject);
  199.     }
  200. }
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement