public static void SetBitmap(Bitmap bitmap, byte opacity, int left, int top, IntPtr handle)
 {
     if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
         throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
     IntPtr screenDc = Win32.GetDC(IntPtr.Zero);
     IntPtr memDc = Win32.CreateCompatibleDC(screenDc);
     IntPtr hBitmap = IntPtr.Zero;
     IntPtr oldBitmap = IntPtr.Zero;
     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(left, top);
         Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
         blend.BlendOp = Win32.AC_SRC_OVER;
         blend.BlendFlags = 0;
         blend.SourceConstantAlpha = opacity;
         blend.AlphaFormat = Win32.AC_SRC_ALPHA;
         Win32.UpdateLayeredWindow(handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
     }
     finally
     {
         Win32.ReleaseDC(IntPtr.Zero, screenDc);
         if (hBitmap != IntPtr.Zero)
         {
             Win32.SelectObject(memDc, oldBitmap);
             Win32.DeleteObject(hBitmap);
         }
         Win32.DeleteDC(memDc);
     }
 }
Пример #2
0
        /// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
        private void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (LastBMPApplied != null)
            {
                LastBMPApplied.Dispose();
                LastBMPApplied = null;
            }
            LastBMPApplied = bitmap;

            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            // The ideia of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.

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

            try
            {
                hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
                oldBitmap = Win32.SelectObject(memDc, hBitmap);

                var size        = new Win32.Size(bitmap.Width, bitmap.Height);
                var pointSource = new Win32.Point(0, 0);
                var topPos      = new Win32.Point(Left, Top);
                var blend       = new Win32.BLENDFUNCTION
                {
                    BlendOp             = Win32.AC_SRC_OVER,
                    BlendFlags          = 0,
                    SourceConstantAlpha = opacity,
                    AlphaFormat         = Win32.AC_SRC_ALPHA
                };

                try
                {
                    Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
                }
                catch { }
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    //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.
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }
Пример #3
0
            private void ShowBitmap(Image bitmap, byte opacity)
            {
                Bitmap copyBmp;

                // The idea of this is very simple,
                // 1. Create a compatible DC with screen;
                // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
                // 3. Call the UpdateLayeredWindow.

                try
                {
                    lock (_form)
                    {
                        // On copie l'image parce qu'avec l'invocation on sait pas trop quand ça va être executé et l'image aura peut être été détruite.
                        copyBmp = new Bitmap(bitmap);
                    }

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

                    try
                    {
                        hBitmap   = copyBmp.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
                        oldBitmap = Win32.SelectObject(memDc, hBitmap);

                        Win32.Size          size        = new Win32.Size(copyBmp.Width, copyBmp.Height);
                        Win32.Point         pointSource = new Win32.Point(0, 0);
                        Win32.Point         topPos      = new Win32.Point(Left, Top);
                        Win32.BLENDFUNCTION blend       = new Win32.BLENDFUNCTION();
                        blend.BlendOp             = Win32.AC_SRC_OVER;
                        blend.BlendFlags          = 0;
                        blend.SourceConstantAlpha = opacity;
                        blend.AlphaFormat         = Win32.AC_SRC_ALPHA;

                        this.InvokeAuto(() => Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA));
                    }
                    finally
                    {
                        Win32.ReleaseDC(IntPtr.Zero, screenDc);
                        if (hBitmap != IntPtr.Zero)
                        {
                            Win32.SelectObject(memDc, oldBitmap);
                            Win32.DeleteObject(hBitmap);
                        }
                        Win32.DeleteDC(memDc);

                        copyBmp.Dispose();
                    }
                }
                catch { }
            }
Пример #4
0
        public void SetBits(int x, int y)
        {
                                                                                  //绘制绘图层背景
                        Bitmap    bitmap         = new Bitmap(x, y);
                        Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20); //窗体光泽重绘边界
                        Graphics  g = Graphics.FromImage(bitmap);

                        g.SmoothingMode   = SmoothingMode.HighQuality;   //高质量
                        g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
            ImageDrawRect.DrawRect(g, Properties.Resources.bgbk2, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1);
             
                        if(!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
                            throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");

                        IntPtr oldBits  = IntPtr.Zero;
                        IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
                        IntPtr hBitmap  = IntPtr.Zero;
                        IntPtr memDc    = Win32.CreateCompatibleDC(screenDC);

             
                        try {
                                Win32.Point         topLoc     = new Win32.Point(Left, Top);
                                Win32.Size          bitMapSize = new Win32.Size(Width, Height);
                                Win32.BLENDFUNCTION blendFunc  = new Win32.BLENDFUNCTION();
                                Win32.Point         srcLoc     = new Win32.Point(0, 0);
                 
                                    hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));

                                oldBits = Win32.SelectObject(memDc, hBitmap);
                 
                                blendFunc.BlendOp = Win32.AC_SRC_OVER;

                                blendFunc.SourceConstantAlpha = Byte.Parse("255");
                                blendFunc.AlphaFormat         = Win32.AC_SRC_ALPHA;
                                blendFunc.BlendFlags          = 0;
                 
                                Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);

                            
            } finally {
                                if(hBitmap != IntPtr.Zero)
                {
                                        Win32.SelectObject(memDc, oldBits);
                                        Win32.DeleteObject(hBitmap);
                                    
                }
                                Win32.ReleaseDC(IntPtr.Zero, screenDC);
                                Win32.DeleteDC(memDc);
                            
            }
                    
        }
Пример #5
0
		/// <summary>
		/// Updates the content of this window with a Bitmap
		/// </summary>
		/// <param name="Bmp">Bitmap to update this window with</param>
		/// <param name="Rect">Rectangle where this window should be located</param>
		public void Update(Bitmap Bmp, Rectangle Rect)
		{
			if(Bmp==null)
				return;

			// wait until it's safe to proceed
			//mutex.WaitOne();
			/*
			 * This function blits a Bitmap to a Layered Window.
			 * ALL drawing functions that are for the transparent
			 * mode use this function.
			*/
			//System.Diagnostics.Debug.WriteLine("started getting hbitmap from bitmap");
			IntPtr Bmap=Bmp.GetHbitmap(Color.FromArgb(0));
			//System.Diagnostics.Debug.WriteLine("done!");
			IntPtr memDc = Win32.GDI.GDIAPI.CreateCompatibleDC(Win32.User32.User32API.GetDC(IntPtr.Zero));
			IntPtr oldBitmap = Win32.GDI.GDIAPI.SelectObject(memDc, Bmap);

			try
			{
				Win32.User32.BlendFunction blend = new Win32.User32.BlendFunction();
				blend.BlendOp             = Win32.User32.BlendOperation.SourceOver;
				blend.BlendFlags          = 0;
				blend.SourceConstantAlpha = 255;
				blend.AlphaFormat         = Win32.User32.AlphaFormat.SourceAlpha;

				Win32.Size size = new Win32.Size(Rect.Width, Rect.Height);
				Win32.Point topPos = new Win32.Point(Rect.X, Rect.Y);
				Win32.Point pointSource = new Win32.Point(0, 0);

				Win32.User32.User32API.UpdateLayeredWindow(this.Handle, IntPtr.Zero, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.User32.UpdateLayeredWindowFlags.Alpha);
			}
			finally
			{
				try
				{
					if (Bmap != IntPtr.Zero) 
					{
						Win32.GDI.GDIAPI.SelectObject(memDc, oldBitmap);
						Win32.GDI.GDIAPI.DeleteObject(Bmap);
					}
					Win32.GDI.GDIAPI.DeleteDC(memDc);
				}
				catch(Exception){}
				//Bmp.Dispose();
			}
			// release our mutex
			//mutex.ReleaseMutex();
		}
Пример #6
0
        public static void SetBits(int Width, int Height, int Left, int Top, Rectangle ClientRectangle)
        {
            //绘制绘图层背景
            Bitmap    bitmap         = new Bitmap(Width + 10, Height + 10);
            Rectangle _BacklightLTRB = new Rectangle(20, 20, 20, 20);//窗体光泽重绘边界
            Graphics  g = Graphics.FromImage(bitmap);

            g.SmoothingMode   = SmoothingMode.HighQuality;   //高质量
            g.PixelOffsetMode = PixelOffsetMode.HighQuality; //高像素偏移质量
            ImageDrawRect.DrawRect(g, Resources.main_light_bkg_top123, ClientRectangle, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), 1, 1);

            if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
            {
                throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
            }
            IntPtr oldBits  = IntPtr.Zero;
            IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
            IntPtr hBitmap  = IntPtr.Zero;
            IntPtr memDc    = Win32.CreateCompatibleDC(screenDC);

            try
            {
                Win32.Point         topLoc     = new Win32.Point(Left, Top);
                Win32.Size          bitMapSize = new Win32.Size(Width, Height);
                Win32.BLENDFUNCTION blendFunc  = new Win32.BLENDFUNCTION();
                Win32.Point         srcLoc     = new Win32.Point(0, 0);

                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBits = Win32.SelectObject(memDc, hBitmap);

                blendFunc.BlendOp             = Win32.AC_SRC_OVER;
                blendFunc.SourceConstantAlpha = Byte.Parse("255");
                blendFunc.AlphaFormat         = Win32.AC_SRC_ALPHA;
                blendFunc.BlendFlags          = 0;

                //Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
            }
            finally
            {
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBits);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.ReleaseDC(IntPtr.Zero, screenDC);
                Win32.DeleteDC(memDc);
            }
        }
Пример #7
0
 public void SetBits()
 {
     try
     {
         Bitmap    bitmap    = new Bitmap(base.Width + 10, base.Height + 10);
         Rectangle rectangle = new Rectangle(20, 20, 20, 20);
         Graphics  graphics  = Graphics.FromImage(bitmap);
         graphics.SmoothingMode   = SmoothingMode.HighQuality;
         graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
         ImageDrawRect.DrawRect(graphics, Resources.border, base.ClientRectangle, Rectangle.FromLTRB(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height), 1, 1);
         if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat))
         {
             throw new ApplicationException("图片必须是32位带Alhpa通道的图片");
         }
         IntPtr hObj    = IntPtr.Zero;
         IntPtr dC      = Win32.GetDC(IntPtr.Zero);
         IntPtr intPtr  = IntPtr.Zero;
         IntPtr intPtr2 = Win32.CreateCompatibleDC(dC);
         try
         {
             Win32.Point         point         = new Win32.Point(base.Left, base.Top);
             Win32.Size          size          = new Win32.Size(base.Width, base.Height);
             Win32.BLENDFUNCTION bLENDFUNCTION = default(Win32.BLENDFUNCTION);
             Win32.Point         point2        = new Win32.Point(0, 0);
             intPtr = bitmap.GetHbitmap(Color.FromArgb(0));
             hObj   = Win32.SelectObject(intPtr2, intPtr);
             bLENDFUNCTION.BlendOp             = 0;
             bLENDFUNCTION.SourceConstantAlpha = byte.Parse("255");
             bLENDFUNCTION.AlphaFormat         = 1;
             bLENDFUNCTION.BlendFlags          = 0;
             Win32.UpdateLayeredWindow(base.Handle, dC, ref point, ref size, intPtr2, ref point2, 0, ref bLENDFUNCTION, 2);
         }
         finally
         {
             if (intPtr != IntPtr.Zero)
             {
                 Win32.SelectObject(intPtr2, hObj);
                 Win32.DeleteObject(intPtr);
             }
             Win32.ReleaseDC(IntPtr.Zero, dC);
             Win32.DeleteDC(intPtr2);
         }
     }
     catch
     {
     }
 }
Пример #8
0
        public void SetBits(Bitmap bitmap)
        {
            if (!haveHandle)
            {
                return;
            }

            if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
            {
                throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
            }

            IntPtr oldBits  = IntPtr.Zero;
            IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
            IntPtr hBitmap  = IntPtr.Zero;
            IntPtr memDc    = Win32.CreateCompatibleDC(screenDC);

            try
            {
                Win32.Point         topLoc     = new Win32.Point(this.Location.X, this.Location.Y);
                Win32.Size          bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.BLENDFUNCTION blendFunc  = new Win32.BLENDFUNCTION();
                Win32.Point         srcLoc     = new Win32.Point(0, 0);

                // 将bitmap创建为一个GDI位图对象
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                // 将hBitmap绘制到memDc当中,并记录被替换下的Bits
                oldBits = Win32.SelectObject(memDc, hBitmap);

                blendFunc.BlendOp             = Win32.AC_SRC_OVER;
                blendFunc.SourceConstantAlpha = 255;
                blendFunc.AlphaFormat         = Win32.AC_SRC_ALPHA;
                blendFunc.BlendFlags          = 0;

                Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
            }
            finally
            {
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBits);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.ReleaseDC(IntPtr.Zero, screenDC);
                Win32.DeleteDC(memDc);
            }
        }
Пример #9
0
        public void SetBits(Bitmap bitmap)
        {
            //this.TopMost = true;
            if (!haveHandle)
            {
                return;
            }

            if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
            {
                throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
            }

            IntPtr oldBits  = IntPtr.Zero;
            IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
            IntPtr hBitmap  = IntPtr.Zero;
            IntPtr memDc    = Win32.CreateCompatibleDC(screenDC);

            try
            {
                Win32.Point         topLoc     = new Win32.Point(Left, Top);
                Win32.Size          bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.BLENDFUNCTION blendFunc  = new Win32.BLENDFUNCTION();
                Win32.Point         srcLoc     = new Win32.Point(0, 0);

                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBits = Win32.SelectObject(memDc, hBitmap);

                blendFunc.BlendOp             = Win32.AC_SRC_OVER;
                blendFunc.SourceConstantAlpha = 255;
                blendFunc.AlphaFormat         = Win32.AC_SRC_ALPHA;
                blendFunc.BlendFlags          = 0;

                Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
            }
            finally
            {
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBits);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.ReleaseDC(IntPtr.Zero, screenDC);
                Win32.DeleteDC(memDc);
            }
        }
Пример #10
0
        /// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            // The ideia of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.

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

            try
            {
                hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
                oldBitmap = Win32.SelectObject(memDc, hBitmap);

                Win32.Size          size        = new Win32.Size(this.Width, this.Height);
                Win32.Point         pointSource = new Win32.Point(0, 0);
                Win32.Point         topPos      = new Win32.Point(this.DesktopLocation.X, this.DesktopLocation.Y);
                Win32.BLENDFUNCTION blend       = new Win32.BLENDFUNCTION();
                blend.BlendOp             = Win32.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat         = Win32.AC_SRC_ALPHA;

                Win32.UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    //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.
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }
Пример #11
0
        public void SetBits()
        {
            if (BackgroundImage != null)
            {
                Bitmap bitmap = new Bitmap(BackgroundImage, Width, Height);

                if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
                {
                    throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
                }

                IntPtr oldBits  = IntPtr.Zero;
                IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
                IntPtr hBitmap  = IntPtr.Zero;
                IntPtr memDc    = Win32.CreateCompatibleDC(screenDC);

                try
                {
                    Win32.Point         topLoc     = new Win32.Point(Left, Top);
                    Win32.Size          bitMapSize = new Win32.Size(Width, Height);
                    Win32.BLENDFUNCTION blendFunc  = new Win32.BLENDFUNCTION();
                    Win32.Point         srcLoc     = new Win32.Point(0, 0);

                    hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                    oldBits = Win32.SelectObject(memDc, hBitmap);

                    blendFunc.BlendOp             = Win32.AC_SRC_OVER;
                    blendFunc.SourceConstantAlpha = 255;
                    blendFunc.AlphaFormat         = Win32.AC_SRC_ALPHA;
                    blendFunc.BlendFlags          = 0;

                    Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
                }
                finally
                {
                    if (hBitmap != IntPtr.Zero)
                    {
                        Win32.SelectObject(memDc, oldBits);
                        Win32.DeleteObject(hBitmap);
                    }
                    Win32.ReleaseDC(IntPtr.Zero, screenDC);
                    Win32.DeleteDC(memDc);
                }
            }
        }
Пример #12
0
    /// <summary>
    ///     Sets a Bitmap as background image for the current per-pixel alpha form.
    ///     An additional global opacity modifier is supported.
    ///     For best results, use .png file format images with 32 bits-per-pixel ARGB color data.
    /// </summary>
    /// <param name="bitmap">The background image for the current per-pixel alpha form.</param>
    /// <param name="opacity">
    ///     A global opacity modifier, range [0..255], with 0 being fully transparent and 255 being fully
    ///     opaque.
    /// </param>
    public void SetBitmap(Bitmap bitmap, byte opacity = byte.MaxValue)
    {
        if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
        {
            throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
        }

        //1. Create a compatible DC with screen;
        //2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
        //3. Call the UpdateLayeredWindow.

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

        try
        {
            hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0));
            oldBitmap = Win32.SelectObject(memDc, hBitmap);

            var size        = new Win32.Size(bitmap.Width, bitmap.Height);
            var pointSource = new Win32.Point(0, 0);
            var topPos      = new Win32.Point(Left, Top);
            var blend       = new Win32.BLENDFUNCTION();
            blend.BlendOp             = Win32.AC_SRC_OVER;
            blend.BlendFlags          = 0;
            blend.SourceConstantAlpha = opacity;
            blend.AlphaFormat         = Win32.AC_SRC_ALPHA;

            Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend,
                                      Win32.ULW_ALPHA);
        }
        finally
        {
            Win32.ReleaseDC(IntPtr.Zero, screenDc);
            if (hBitmap != IntPtr.Zero)
            {
                Win32.SelectObject(memDc, oldBitmap);
                Win32.DeleteObject(hBitmap);
            }
            Win32.DeleteDC(memDc);
        }
    }
Пример #13
0
        public void SetBits()
        {
            if (BackgroundImage != null)
            {
                //绘制绘图层背景
                Bitmap bitmap = new Bitmap(BackgroundImage, base.Width, base.Height);
                if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
                    throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
                IntPtr oldBits = IntPtr.Zero;
                IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
                IntPtr hBitmap = IntPtr.Zero;
                IntPtr memDc = Win32.CreateCompatibleDC(screenDC);

                try
                {
                    Win32.Point topLoc = new Win32.Point(Left, Top);
                    Win32.Size bitMapSize = new Win32.Size(Width, Height);
                    Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
                    Win32.Point srcLoc = new Win32.Point(0, 0);

                    hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                    oldBits = Win32.SelectObject(memDc, hBitmap);

                    blendFunc.BlendOp = Win32.AC_SRC_OVER;
                    blendFunc.SourceConstantAlpha = Byte.Parse(Main.SkinOpacity.ToString());
                    blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
                    blendFunc.BlendFlags = 0;

                    Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
                }
                finally
                {
                    if (hBitmap != IntPtr.Zero)
                    {
                        Win32.SelectObject(memDc, oldBits);
                        Win32.DeleteObject(hBitmap);
                    }
                    Win32.ReleaseDC(IntPtr.Zero, screenDC);
                    Win32.DeleteDC(memDc);
                }
            }
        }
Пример #14
0
        //private void T_Tick(object sender, EventArgs e)
        //{
        //    System.Windows.Forms.Timer t = sender as System.Windows.Forms.Timer;
        //    t.Stop();
        //    Action();
        //    return;

        //}

        private void NewMethod(Bitmap bitmap, IntPtr oldBits, IntPtr screenDC, IntPtr hBitmap, IntPtr memDc)
        {
            try
            {
                Win32.Point         topLoc     = new Win32.Point(Left, Top);
                Win32.Size          bitMapSize = new Win32.Size(Width, Height);
                Win32.BLENDFUNCTION blendFunc  = new Win32.BLENDFUNCTION();
                Win32.Point         srcLoc     = new Win32.Point(0, 0);

                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBits = Win32.SelectObject(memDc, hBitmap);

                blendFunc.BlendOp             = Win32.AC_SRC_OVER;
                blendFunc.SourceConstantAlpha = Byte.Parse("255");
                blendFunc.AlphaFormat         = Win32.AC_SRC_ALPHA;
                blendFunc.BlendFlags          = 0;


                try
                {
                    Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBits);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.ReleaseDC(IntPtr.Zero, screenDC);
                Win32.DeleteDC(memDc);
            }
        }
Пример #15
0
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

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

            try
            {
                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
                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(Left, Top);
                Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
                blend.BlendOp = Win32.AC_SRC_OVER;
                blend.BlendFlags = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat = Win32.AC_SRC_ALPHA;

                Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }
Пример #16
0
        /// <summary>
        /// 绘制位图
        /// </summary>
        /// <param name="bitmap">位图</param>
        /// <param name="opacity">透明度</param>
        /// <returns></returns>
        public void SetBitmap(Bitmap bitmap, byte opacity = 255)
        {
            //if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat))
            //{
            //    throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
            //}
            IntPtr hObj    = IntPtr.Zero;
            IntPtr dC      = Win32.GetDC(IntPtr.Zero);
            IntPtr intPtr  = IntPtr.Zero;
            IntPtr intPtr2 = Win32.CreateCompatibleDC(dC);

            try
            {
                Win32.Point         point         = new Win32.Point(base.Left, base.Top);
                Win32.Size          size          = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.BLENDFUNCTION bLENDFUNCTION = default(Win32.BLENDFUNCTION);
                Win32.Point         point2        = new Win32.Point(0, 0);
                intPtr = bitmap.GetHbitmap(Color.FromArgb(0));
                hObj   = Win32.SelectObject(intPtr2, intPtr);
                bLENDFUNCTION.BlendOp             = 0;
                bLENDFUNCTION.SourceConstantAlpha = opacity;
                bLENDFUNCTION.AlphaFormat         = 1;
                bLENDFUNCTION.BlendFlags          = 0;
                Win32.UpdateLayeredWindow(base.Handle, dC, ref point, ref size, intPtr2, ref point2, 0, ref bLENDFUNCTION, 2);
            }
            finally
            {
                if (intPtr != IntPtr.Zero)
                {
                    Win32.SelectObject(intPtr2, hObj);
                    Win32.DeleteObject(intPtr);
                }
                Win32.ReleaseDC(IntPtr.Zero, dC);
                Win32.DeleteDC(intPtr2);
            }
        }
Пример #17
0
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }
            IntPtr dC      = Win32.GetDC(IntPtr.Zero);
            IntPtr intPtr  = Win32.CreateCompatibleDC(dC);
            IntPtr intPtr2 = IntPtr.Zero;
            IntPtr hObj    = IntPtr.Zero;

            try
            {
                intPtr2 = bitmap.GetHbitmap(Color.FromArgb(0));
                hObj    = Win32.SelectObject(intPtr, intPtr2);
                Win32.Size          size          = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.Point         point         = new Win32.Point(0, 0);
                Win32.Point         point2        = new Win32.Point(base.Left, base.Top);
                Win32.BLENDFUNCTION bLENDFUNCTION = default(Win32.BLENDFUNCTION);
                bLENDFUNCTION.BlendOp             = 0;
                bLENDFUNCTION.BlendFlags          = 0;
                bLENDFUNCTION.SourceConstantAlpha = opacity;
                bLENDFUNCTION.AlphaFormat         = 1;
                Win32.UpdateLayeredWindow(base.Handle, dC, ref point2, ref size, intPtr, ref point, 0, ref bLENDFUNCTION, 2);
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, dC);
                if (intPtr2 != IntPtr.Zero)
                {
                    Win32.SelectObject(intPtr, hObj);
                    Win32.DeleteObject(intPtr2);
                }
                Win32.DeleteDC(intPtr);
            }
        }
Пример #18
0
 private void initBitmaps()
 {
     thisBitmap   = new Bitmap(thisWindow.Width, thisWindow.Height);
     thisGraphics = Graphics.FromImage(thisBitmap);
     bitMapSize   = new Win32.Size(thisBitmap.Width, thisBitmap.Height);
 }
Пример #19
0
        public void SetBits(Bitmap bitmap)
        {
            if (!haveHandle) return;

            if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
                throw new ApplicationException("The picture must be 32bit picture with alpha channel.");
            
            IntPtr oldBits = IntPtr.Zero;
            IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
            IntPtr hBitmap = IntPtr.Zero;
            IntPtr memDc = Win32.CreateCompatibleDC(screenDC);

            try
            {
                Win32.Point topLoc = new Win32.Point(Left, Top);
                Win32.Size bitMapSize = new Win32.Size(bitmap.Width, bitmap.Height);
                Win32.BLENDFUNCTION blendFunc = new Win32.BLENDFUNCTION();
                Win32.Point srcLoc = new Win32.Point(0, 0);

                hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBits = Win32.SelectObject(memDc, hBitmap);

                blendFunc.BlendOp = Win32.AC_SRC_OVER;
                blendFunc.SourceConstantAlpha = 255;
                blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
                blendFunc.BlendFlags = 0;

                Win32.UpdateLayeredWindow(Handle, screenDC, ref topLoc, ref bitMapSize, memDc, ref srcLoc, 0, ref blendFunc, Win32.ULW_ALPHA);
            }
            finally
            {
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBits);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.ReleaseDC(IntPtr.Zero, screenDC);
                Win32.DeleteDC(memDc);
            }
        }
Пример #20
0
 public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Win32.Point pptDst, ref Win32.Size psize, IntPtr hdcSrc, ref Win32.Point pptSrc, int crKey, ref Win32.BLENDFUNCTION pblend, int dwFlags);
Пример #21
0
		/// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
		public void SetBitmap(Bitmap bitmap, byte opacity)
		{
			if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
				throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

			// The ideia of this is very simple,
			// 1. Create a compatible DC with screen;
			// 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
			// 3. Call the UpdateLayeredWindow.

			Bitmap bmp = null;
			if (this.Controls.Count == 0) bmp = bitmap;
			else
			{
				bmp = new Bitmap(bitmap);
				foreach (Control item in this.Controls)
				{
					if (item.Visible) item.DrawToBitmap(bmp, item.Bounds);
				}
			}


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

			try
			{
				hBitmap = bmp.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
				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(Left, Top);
				Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
				blend.BlendOp = Win32.AC_SRC_OVER;
				blend.BlendFlags = 0;
				blend.SourceConstantAlpha = opacity;
				blend.AlphaFormat = Win32.AC_SRC_ALPHA;

				Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
			}
			finally
			{
				Win32.ReleaseDC(IntPtr.Zero, screenDc);
				if (hBitmap != IntPtr.Zero)
				{
					Win32.SelectObject(memDc, oldBitmap);
					Win32.DeleteObject(hBitmap);
				}
				Win32.DeleteDC(memDc);
			}
		}
Пример #22
0
        /// <summary>
        /// Alpha Form核心代码
        /// bitmap为一张带有Alpha通道的32位位图
        /// opacity指定窗体的透明度
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="opacity"></param>
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

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

            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(curPostion.X, curPostion.Y);
                Win32.BLENDFUNCTION blend       = new Win32.BLENDFUNCTION();

                /**/
                ////Construct Win32.BLENDFUNCTION
                blend.BlendOp             = Win32.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat         = Win32.AC_SRC_ALPHA;

                //The Alpha Form Core Function
                Win32.UpdateLayeredWindow(HWND, screenDc, ref topPos, ref size,
                                          memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
            }
            finally
            {
                //Release Resource
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    //Windows.DeleteObject(hBitmap);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }

        #endregion


        #region 事件处理代码

        //登录事件
        private void WTMainForm_Load(object sender, EventArgs e)
        {
            SetBitmap(leimu[0]);
            Thread t = new Thread(qiehuan);

            t.Start();
        }

        void qiehuan()
        {
            int lingshi = 1;

            while (true)
            {
                lingshi++;
                lingshi %= 50;
                SetBitmap(leimu[lingshi]);
                Thread.Sleep(60);
            }
        }

        private void WTMainForm_MouseDown(object sender, MouseEventArgs e)
        {
            isDrag       = true;
            oldPostion.X = e.X;
            oldPostion.Y = e.Y;
        }

        private void WTMainForm_MouseUp(object sender, MouseEventArgs e)
        {
            isDrag = false;
        }

        private void WTMainForm_MouseMove(object sender, MouseEventArgs e)
        {
            if ((e.Button == MouseButtons.Left) && (isDrag == true))
            {
                this.Left   += e.X - oldPostion.X;
                this.Top    += e.Y - oldPostion.Y;
                curPostion.X = this.Left;
                curPostion.Y = this.Top;
            }
        }

        private void WTMainForm_MouseEnter(object sender, EventArgs e)
        {
            if (isDownLoading == true)
            {
                isMouseEnter     = true;
                mouseEnterFinish = false;
                mouseEnterThread = new Thread(new ThreadStart(MouseEnterThreadFun));
                mouseEnterThread.Start();
            }
        }

        private void WTMainForm_MouseLeave(object sender, EventArgs e)
        {
            if (isDownLoading == true)
            {
                isMouseEnter     = false;
                mouseLeaveFinish = false;
                mouseLeaveThread = new Thread(new ThreadStart(MouseLeaveThreadFun));
                mouseLeaveThread.Start();
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            canColse = true;
            Environment.Exit(0);
        }

        private void 变大ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            /*
             * if (isDownLoading == false)
             * {
             *  isDownLoading = true;
             *  downLoadingThread = new Thread(new ThreadStart(DownLoadingThreadFun));
             *  downLoadingThread.Start();
             * }*/
        }
Пример #23
0
    /// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
    public void SetBitmap(Bitmap bitmap, byte opacity)
    {
        if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");

        // The ideia of this is very simple,
        // 1. Create a compatible DC with screen;
        // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
        // 3. Call the UpdateLayeredWindow.

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

        try {
            hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));  // grab a GDI handle from this GDI+ bitmap
            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(Left, Top);
            Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
            blend.BlendOp             = Win32.AC_SRC_OVER;
            blend.BlendFlags          = 0;
            blend.SourceConstantAlpha = opacity;
            blend.AlphaFormat         = Win32.AC_SRC_ALPHA;

            Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
        }
        finally {
            Win32.ReleaseDC(IntPtr.Zero, screenDc);
            if (hBitmap != IntPtr.Zero) {
                Win32.SelectObject(memDc, oldBitmap);
                //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.
                Win32.DeleteObject(hBitmap);
            }
            Win32.DeleteDC(memDc);
        }
    }
Пример #24
0
        /// <para>Changes the current bitmap with a custom opacity level.  Here is where all happens!</para>
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            // The ideia of this is very simple,
            // 1. Create a compatible DC with screen;
            // 2. Select the bitmap with 32bpp with alpha-channel in the compatible DC;
            // 3. Call the UpdateLayeredWindow.

            Bitmap bmp = null;

            if (this.Controls.Count == 0)
            {
                bmp = bitmap;
            }
            else
            {
                bmp = new Bitmap(bitmap);
                foreach (Control item in this.Controls)
                {
                    if (item.Visible)
                    {
                        item.DrawToBitmap(bmp, item.Bounds);
                    }
                }
            }


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

            try
            {
                hBitmap   = bmp.GetHbitmap(Color.FromArgb(0));                // grab a GDI handle from this GDI+ bitmap
                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(Left, Top);
                Win32.BLENDFUNCTION blend       = new Win32.BLENDFUNCTION();
                blend.BlendOp             = Win32.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat         = Win32.AC_SRC_ALPHA;

                Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA);
            }
            finally
            {
                Win32.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(memDc, oldBitmap);
                    Win32.DeleteObject(hBitmap);
                }
                Win32.DeleteDC(memDc);
            }
        }
Пример #25
0
        public SplashScreen(Bitmap splashImage) : base()
        {
            if (splashImage == null)
            {
                throw new ArgumentNullException();
            }
            if (splashImage.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32ppp with alpha-channel.");
            }

            this.UseFadeIn             = true;
            this.UseFadeOut            = true;
            this.AutoScaleDimensions   = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode         = System.Windows.Forms.AutoScaleMode.None;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.DoubleBuffered        = true;

            this.mySplashImage = splashImage;
            // This form should not have a border or else Windows will clip it.
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

            this.MaximizeBox        = false;
            this.Name               = "SplashScreen";
            this.DestinationOpacity = 0;
            this.opaSetTimes        = 0;
            this.closingTime        = false;
            this.codeclosing        = false;

            // Force it to be at Center of the screen

            /*var sizeofImage = Properties.Resources.splashimage.Size;
             * Rectangle myBound = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
             * if (myBound.Size.Width < sizeofImage.Width || myBound.Size.Height < sizeofImage.Height)
             * {
             *  this.DesktopBounds = new Rectangle(0, 0,
             *      myBound.Size.Width < sizeofImage.Width ? myBound.Size.Width : sizeofImage.Width,
             *      myBound.Size.Height < sizeofImage.Height ? myBound.Size.Height : sizeofImage.Height);
             * }
             * else
             *  this.DesktopBounds = new Rectangle(
             *      (myBound.Size.Width / 2) - (sizeofImage.Width / 2),
             *      (myBound.Size.Height / 2) - (sizeofImage.Height / 2),
             *      sizeofImage.Width,
             *      sizeofImage.Height
             *      );
             * //*/

            this.ClientSize = this.mySplashImage.Size;

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

            try
            {
                this.hBitmap   = this.mySplashImage.GetHbitmap(Color.FromArgb(0)); // grab a GDI handle from this GDI+ bitmap
                this.oldBitmap = Win32.SelectObject(this.memDc, this.hBitmap);

                this.drawsize          = new Win32.Size(this.mySplashImage.Width, this.mySplashImage.Height);
                this.pointSource       = new Win32.Point(0, 0);
                this.blend             = new Win32.BLENDFUNCTION();
                this.blend.BlendOp     = Win32.AC_SRC_OVER;
                this.blend.BlendFlags  = 0;
                this.blend.AlphaFormat = Win32.AC_SRC_ALPHA;
            }
            catch (Exception ex)
            {
                Win32.ReleaseDC(IntPtr.Zero, this.screenDc);
                if (this.hBitmap != IntPtr.Zero)
                {
                    Win32.SelectObject(this.memDc, this.oldBitmap);
                    // 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.
                    Win32.DeleteObject(this.hBitmap);
                }
                Win32.DeleteDC(this.memDc);
                throw ex;
            }

            this.myTimer          = new System.Windows.Forms.Timer();
            this.myTimer.Enabled  = false;
            this.myTimer.Tick    += MyTimer_Tick;
            this.myTimer.Interval = 20;
        }