Exemple #1
0
        // Sets the current bitmap
        private static void SelectBitmap(Bitmap bitmap)
        {
            // Does this bitmap contain an alpha channel?
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
            }

            // Get device contexts
            IntPtr screenDc   = APIHelp.GetDC(IntPtr.Zero);
            IntPtr memDc      = APIHelp.CreateCompatibleDC(screenDc);
            IntPtr hBitmap    = IntPtr.Zero;
            IntPtr hOldBitmap = IntPtr.Zero;

            try
            {
                // Get handle to the new bitmap and select it into the current device context
                hBitmap    = bitmap.GetHbitmap(Color.FromArgb(0));
                hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);

                // Set parameters for layered window update
                APIHelp.Size  newSize        = new APIHelp.Size(bitmap.Width, bitmap.Height);               // Size window to match bitmap
                APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
                APIHelp.Point newLocation    = new APIHelp.Point(ms_frmSplash.Left - 22, ms_frmSplash.Top); // Same as this window, offset by 22 to compensate
                // for transparent border
                APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION
                {
                    BlendOp             = APIHelp.AC_SRC_OVER,
                    BlendFlags          = 0,
                    SourceConstantAlpha = 255,
                    AlphaFormat         = APIHelp.AC_SRC_ALPHA
                };

                // Update the window
                APIHelp.UpdateLayeredWindow(ms_frmSplash.Handle, screenDc, ref newLocation, ref newSize,
                                            memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
            }
            finally
            {
                // Release device context
                APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    APIHelp.SelectObject(memDc, hOldBitmap);
                    APIHelp.DeleteObject(hBitmap);                                                                                              // Remove bitmap resources
                }
                APIHelp.DeleteDC(memDc);
            }
        }
Exemple #2
0
        // Sets the current bitmap
        private static void SelectBitmap(Bitmap bitmap)
        {
            // Does this bitmap contain an alpha channel?
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
            }

            // Get device contexts
            IntPtr screenDc = APIHelp.GetDC(IntPtr.Zero);
            IntPtr memDc = APIHelp.CreateCompatibleDC(screenDc);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr hOldBitmap = IntPtr.Zero;

            try
            {
                // Get handle to the new bitmap and select it into the current device context
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);

                // Set parameters for layered window update
                APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height);	// Size window to match bitmap
                APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
                APIHelp.Point newLocation = new APIHelp.Point(ms_frmSplash.Left - 22, ms_frmSplash.Top);		    // Same as this window, offset by 22 to compensate
                                                                                        // for transparent border
                APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION
                {
                    BlendOp = APIHelp.AC_SRC_OVER,
                    BlendFlags = 0,
                    SourceConstantAlpha = 255,
                    AlphaFormat = APIHelp.AC_SRC_ALPHA
                };

                // Update the window
                APIHelp.UpdateLayeredWindow(ms_frmSplash.Handle, screenDc, ref newLocation, ref newSize,
                    memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
            }
            finally
            {
                // Release device context
                APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    APIHelp.SelectObject(memDc, hOldBitmap);
                    APIHelp.DeleteObject(hBitmap);										// Remove bitmap resources
                }
                APIHelp.DeleteDC(memDc);
            }
        }