public static void fSetBackground(System.Windows.Forms.Control control, Bitmap bitmap)
        {
            // Set our control's size to be the same as the bitmap
            control.Width = bitmap.Width;
            control.Height = bitmap.Height;

            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("must be 32 bits per pixel");
            }

            IntPtr hBitmap = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;
            IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
            IntPtr memDc = Win32.CreateCompatibleDC(screenDc);

            try
            {
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBitmap = Win32.SelectObject(memDc, hBitmap);
                Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.Point pointSource = new Win32.Point(0, 0);
                Win32.Point topPos = new Win32.Point(control.Left, control.Top);
                Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
                blend.BlendOp = 0;
                blend.BlendFlags = 0;
                blend.SourceConstantAlpha = byte.MaxValue;
                blend.AlphaFormat = 1;
                Win32.UpdateLayeredWindow(control.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, 2);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }
Beispiel #2
0
        public static void fSetBackground(System.Windows.Forms.Control control, Bitmap bitmap)
        {
            // Set our control's size to be the same as the bitmap
            control.Width  = bitmap.Width;
            control.Height = bitmap.Height;

            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("must be 32 bits per pixel");
            }

            IntPtr hBitmap   = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;
            IntPtr screenDc  = Win32.GetDC(IntPtr.Zero);
            IntPtr memDc     = Win32.CreateCompatibleDC(screenDc);

            try
            {
                hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBitmap = Win32.SelectObject(memDc, hBitmap);
                Win32.Size          size        = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.Point         pointSource = new Win32.Point(0, 0);
                Win32.Point         topPos      = new Win32.Point(control.Left, control.Top);
                Win32.BLENDFUNCTION blend       = new Win32.BLENDFUNCTION();
                blend.BlendOp             = 0;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = byte.MaxValue;
                blend.AlphaFormat         = 1;
                Win32.UpdateLayeredWindow(control.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, 2);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }