コード例 #1
0
 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
ファイル: LayeredWindow.cs プロジェクト: Answeror/focus
        public void UpdateWindow(Bitmap image, byte opacity, int width, int height, Point pos)
        {
            IntPtr hdcWindow = Win32.GetWindowDC(this.Handle);
            IntPtr hDC = Win32.CreateCompatibleDC(hdcWindow);
            IntPtr hBitmap = image.GetHbitmap(Color.FromArgb(0));
            IntPtr hOld = Win32.SelectObject(hDC, hBitmap);
            Size size = new Size(0,0);
            Point zero = new Point(0, 0);

            if (width == -1 || height == -1) {
                //No width and height specified, use the size of the image
                size.Width = image.Width;
                size.Height = image.Height;
            } else {
                //Use whichever size is smallest, so that the image will
                //be clipped if necessary
                size.Width = Math.Min(image.Width, width);
                size.Height = Math.Min(image.Height, height);
            }
            m_rect.Size = size;
            m_rect.Location = pos;

            Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
            blend.BlendOp = (byte)Win32.BlendOps.AC_SRC_OVER;
            blend.SourceConstantAlpha = opacity;
            blend.AlphaFormat = (byte)Win32.BlendOps.AC_SRC_ALPHA;
            blend.BlendFlags = (byte)Win32.BlendFlags.None;

            Win32.UpdateLayeredWindow(this.Handle, hdcWindow, ref pos, ref size, hDC, ref zero, 0, ref blend, Win32.BlendFlags.ULW_ALPHA);

            Win32.SelectObject(hDC, hOld);
            Win32.DeleteObject(hBitmap);
            Win32.DeleteDC(hDC);
            Win32.ReleaseDC(this.Handle, hdcWindow);
        }
コード例 #3
0
ファイル: LayeredWindow.cs プロジェクト: Lilacist/Vacation
    public void UpdateWindow(Bitmap image, byte opacity, int width, int height, Point pos)
    {
        IntPtr hdcWindow = Win32.GetWindowDC(this.Handle);
        IntPtr hDC       = Win32.CreateCompatibleDC(hdcWindow);
        IntPtr hBitmap   = image.GetHbitmap(Color.FromArgb(0));
        IntPtr hOld      = Win32.SelectObject(hDC, hBitmap);
        Size   size      = new Size(0, 0);
        Point  zero      = new Point(0, 0);

        if (width == -1 || height == -1)
        {
            size.Width  = image.Width;
            size.Height = image.Height;
        }
        else
        {
            size.Width  = Math.Min(image.Width, width);
            size.Height = Math.Min(image.Height, height);
        }
        m_rect.Size     = size;
        m_rect.Location = pos;
        Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
        blend.BlendOp             = (byte)Win32.BlendOps.AC_SRC_OVER;
        blend.SourceConstantAlpha = opacity;
        blend.AlphaFormat         = (byte)Win32.BlendOps.AC_SRC_ALPHA;
        blend.BlendFlags          = (byte)Win32.BlendFlags.None;
        Win32.UpdateLayeredWindow(this.Handle, hdcWindow, ref pos, ref size, hDC, ref zero, 0, ref blend, Win32.BlendFlags.ULW_ALPHA);
        Win32.SelectObject(hDC, hOld);
        Win32.DeleteObject(hBitmap);
        Win32.DeleteDC(hDC);
        Win32.ReleaseDC(this.Handle, hdcWindow);
    }
コード例 #4
0
ファイル: FormOSD.cs プロジェクト: nus-ii/AudioSwitch
        /// <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);
            }
        }
コード例 #5
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 { }
            }
コード例 #6
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);
                            
            }
                    
        }
コード例 #7
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);
            }
        }
コード例 #8
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
     {
     }
 }
コード例 #9
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);
            }
        }
コード例 #10
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);
            }
        }
コード例 #11
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);
            }
        }
コード例 #12
0
ファイル: Form1.cs プロジェクト: wpmyj/csharp
        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);
                }
            }
        }
コード例 #13
0
ファイル: PerPixelAlphaForm.cs プロジェクト: tiqq111/Reflecta
    /// <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);
        }
    }
コード例 #14
0
        /// <summary>
        /// Sets the given bitmap as window content with transparent parts via the
        /// layered windows API.
        /// </summary>
        /// <param name="bitmap">The bitmap to set.</param>
        /// <param name="opacity">The overall opacity (255 = opaque).</param>
        public static void SetBitmap(Control control, Bitmap bitmap, int 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(control.Left, control.Top);
                Win32.BLENDFUNCTION blend       = new Win32.BLENDFUNCTION();
                blend.BlendOp             = Win32.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = (byte)opacity;
                blend.AlphaFormat         = Win32.AC_SRC_ALPHA;

                if (!control.IsDisposed && !control.Disposing)
                {
                    Win32.UpdateLayeredWindow(control.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);
            }
        }
コード例 #15
0
ファイル: SkinForm.cs プロジェクト: zimengyu1992/WorkLogForm
        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);
                }
            }
        }
コード例 #16
0
ファイル: FlashForm.cs プロジェクト: luowei98/CubePrimer
        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);
            }
        }
コード例 #17
0
ファイル: FormX.cs プロジェクト: NewYoungCode/LSkinCShape
        //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);
            }
        }
コード例 #18
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);
            }
        }
コード例 #19
0
ファイル: BackForm.cs プロジェクト: liuzhonglei/JadeUI
        /// <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);
            }
        }
コード例 #20
0
    protected override void WndProc(ref Message m)
    {
        if (this.DesignMode)
        {
            base.WndProc(ref m);
            return;
        }
        Win32.Message msgId   = (Win32.Message)m.Msg;
        bool          UseBase = true;

        switch (msgId)
        {
        case Win32.Message.WM_LBUTTONUP:
        {
            if (Win32.GetCapture() != IntPtr.Zero)
            {
                Win32.ReleaseCapture();
            }
        }
        break;

        case Win32.Message.WM_ENTERSIZEMOVE:
        {
        }
        break;

        case Win32.Message.WM_EXITSIZEMOVE:
        {
            m_isDown.Left = false;
            m_moving      = false;

            if (m_enhanced)
            {
                this.SuspendLayout();
                foreach (Control ctrl in m_hiddenControls)
                {
                    ctrl.Visible = true;
                }
                m_hiddenControls.Clear();
                this.ResumeLayout();
                updateLayeredBackground(this.ClientSize.Width, this.ClientSize.Height, m_layeredWnd.LayeredPos, false);
            }
        }
        break;

        case Win32.Message.WM_MOUSEMOVE:
            if (Win32.GetCapture() != IntPtr.Zero && m_moving)
            {
                if (m_enhanced)
                {
                    IntPtr hdcScreen = Win32.GetWindowDC(m_layeredWnd.Handle);
                    IntPtr windowDC  = Win32.GetDC(this.Handle);
                    IntPtr memDC     = Win32.CreateCompatibleDC(windowDC);
                    IntPtr BmpMask   = Win32.CreateBitmap(this.ClientSize.Width,
                                                          this.ClientSize.Height, 1, 1, IntPtr.Zero);

                    Bitmap backImage = m_useBackgroundEx ? m_backgroundFull : m_background;
                    IntPtr BmpBack   = backImage.GetHbitmap(Color.FromArgb(0));

                    Win32.SelectObject(memDC, BmpMask);
                    uint oldCol = Win32.SetBkColor(windowDC, 0x00FF00FF);
                    Win32.BitBlt(memDC, 0, 0, this.ClientSize.Width, this.ClientSize.Height, windowDC, 0, 0, Win32.TernaryRasterOperations.SRCCOPY);
                    Win32.SetBkColor(windowDC, oldCol);

                    Win32.SelectObject(memDC, BmpBack);
                    IntPtr brush = Win32.CreateSolidBrush(0x00FFFFFF);
                    Win32.SelectObject(memDC, brush);
                    Win32.MaskBlt(memDC, 0, 0, backImage.Width, backImage.Height, windowDC, 0, 0, BmpMask, 0, 0, 0xCFAA0020);

                    Point zero = new Point(0, 0);
                    Size  size = m_layeredWnd.LayeredSize;
                    Point pos  = m_layeredWnd.LayeredPos;
                    Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
                    blend.AlphaFormat         = (byte)Win32.BlendOps.AC_SRC_ALPHA;
                    blend.BlendFlags          = (byte)Win32.BlendFlags.None;
                    blend.BlendOp             = (byte)Win32.BlendOps.AC_SRC_OVER;
                    blend.SourceConstantAlpha = (byte)(this.Opacity * 255);

                    Win32.UpdateLayeredWindow(m_layeredWnd.Handle, hdcScreen, ref pos, ref size, memDC, ref zero, 0, ref blend, Win32.BlendFlags.ULW_ALPHA);
                    Win32.ReleaseDC(IntPtr.Zero, hdcScreen);
                    Win32.ReleaseDC(this.Handle, windowDC);
                    Win32.DeleteDC(memDC);
                    Win32.DeleteObject(brush);
                    Win32.DeleteObject(BmpMask);
                    Win32.DeleteObject(BmpBack);

                    this.SuspendLayout();
                    foreach (Control ctrl in this.Controls)
                    {
                        if (ctrl.Visible)
                        {
                            m_hiddenControls.Add(ctrl);
                            ctrl.Visible = false;
                        }
                    }
                    this.ResumeLayout();
                }

                Win32.ReleaseCapture();
                Win32.SendMessage(this.Handle, (int)Win32.Message.WM_NCLBUTTONDOWN, (int)Win32.Message.HTCAPTION, 0);
            }
            break;

        case Win32.Message.WM_SIZE:
        {
            int width  = m.LParam.ToInt32() & 0xFFFF;
            int height = m.LParam.ToInt32() >> 16;
            this.updateLayeredSize(width, height);
        }
        break;

        case Win32.Message.WM_WINDOWPOSCHANGING:
        {
            Win32.WINDOWPOS      posInfo   = (Win32.WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(Win32.WINDOWPOS));
            Win32.WindowPosFlags move_size = Win32.WindowPosFlags.SWP_NOMOVE | Win32.WindowPosFlags.SWP_NOSIZE;
            if ((posInfo.flags & move_size) != move_size)
            {
                if (posInfo.hwndInsertAfter != this.Handle)
                {
                    IntPtr hwdp = Win32.BeginDeferWindowPos(2);
                    if (hwdp != IntPtr.Zero)
                    {
                        hwdp = Win32.DeferWindowPos(hwdp, m_layeredWnd.Handle, this.Handle, posInfo.x + m_offX, posInfo.y + m_offY,
                                                    0, 0, (uint)(posInfo.flags | Win32.WindowPosFlags.SWP_NOSIZE | Win32.WindowPosFlags.SWP_NOZORDER));
                    }
                    if (hwdp != IntPtr.Zero)
                    {
                        hwdp = Win32.DeferWindowPos(hwdp, this.Handle, this.Handle, posInfo.x, posInfo.y, posInfo.cx, posInfo.cy,
                                                    (uint)(posInfo.flags | Win32.WindowPosFlags.SWP_NOZORDER));
                    }
                    if (hwdp != IntPtr.Zero)
                    {
                        Win32.EndDeferWindowPos(hwdp);
                    }

                    m_layeredWnd.LayeredPos = new Point(posInfo.x + m_offX, posInfo.y + m_offY);
                    posInfo.flags          |= Win32.WindowPosFlags.SWP_NOMOVE;
                    Marshal.StructureToPtr(posInfo, m.LParam, true);
                }

                if ((posInfo.flags & Win32.WindowPosFlags.SWP_NOSIZE) == 0)
                {
                    int diffX = posInfo.cx - this.Size.Width;
                    int diffY = posInfo.cy - this.Size.Height;
                    if (diffX != 0 || diffY != 0)
                    {
                        this.updateLayeredSize(this.ClientSize.Width + diffX, this.ClientSize.Height + diffY);
                    }
                }
                UseBase = false;
            }
        }
        break;


        case Win32.Message.WM_ACTIVATE:
        {
            if (m.WParam != IntPtr.Zero)
            {
                IntPtr prevWnd = Win32.GetWindow(m_layeredWnd.Handle, Win32.GetWindow_Cmd.GW_HWNDPREV);
                while (prevWnd != IntPtr.Zero)
                {
                    if (Win32.IsWindowVisible(prevWnd))
                    {
                        break;
                    }

                    prevWnd = Win32.GetWindow(prevWnd, Win32.GetWindow_Cmd.GW_HWNDPREV);
                }

                if (prevWnd != this.Handle)
                {
                    Win32.SetWindowPos(m_layeredWnd.Handle, this.Handle, 0, 0, 0, 0, (uint)(Win32.WindowPosFlags.SWP_NOSENDCHANGING | Win32.WindowPosFlags.SWP_NOACTIVATE | Win32.WindowPosFlags.SWP_NOSIZE | Win32.WindowPosFlags.SWP_NOMOVE));
                }
            }
        }
        break;
        }

        if (UseBase)
        {
            base.WndProc(ref m);
        }
    }
コード例 #21
0
ファイル: Win32.cs プロジェクト: yikuyirong/sdrddoseen
 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);
コード例 #22
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);
			}
		}
コード例 #23
0
    public void SetBits(Bitmap bitmap, byte opacity)
    {
        if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
            throw new ApplicationException("The bitmap must be 32 bits per pixel with an alpha channel.");

        IntPtr oldBits = IntPtr.Zero;
        IntPtr screenDC = Win32.GetDC(IntPtr.Zero);
        IntPtr hBitmap = IntPtr.Zero;
        IntPtr memDc = Win32.CreateCompatibleDC(screenDC);
        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);
        blendFunc.BlendOp = Win32.AC_SRC_OVER;
        blendFunc.SourceConstantAlpha = opacity;
        blendFunc.AlphaFormat = Win32.AC_SRC_ALPHA;
        blendFunc.BlendFlags = 0;

        try
        {
            hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
            oldBits = Win32.SelectObject(memDc, hBitmap);
            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);
        }
    }
コード例 #24
0
        /// <summary>
        /// Override for the main forms WndProc method
        /// This is used to intercept the forms movement so that we can move
        /// blended background form at the same time, as well as check for
        /// when the form is activated so we can ensure the Z-order of our
        /// forms remains intact.
        /// </summary>
        /// <param name="m">Windows Message</param>
        protected override void WndProc(ref Message m)
        {
            if (this.DesignMode)
            {
                base.WndProc(ref m);
                return;
            }

            Win32.Message msgId   = (Win32.Message)m.Msg;
            bool          UseBase = true;

            switch (msgId)
            {
            case Win32.Message.WM_LBUTTONUP:
            {
                //Just in case
                if (Win32.GetCapture() != IntPtr.Zero)
                {
                    Win32.ReleaseCapture();
                }
            }
            break;

            case Win32.Message.WM_ENTERSIZEMOVE:
            {
            }
            break;

            case Win32.Message.WM_EXITSIZEMOVE:
            {
                //We've stopped dragging the form, so lets make sure that our values are correct
                m_isDown.Left = false;
                m_moving      = false;

                if (m_enhanced)
                {
                    this.SuspendLayout();
                    foreach (Control ctrl in m_hiddenControls)
                    {
                        ctrl.Visible = true;
                    }
                    m_hiddenControls.Clear();
                    this.ResumeLayout();
                    updateLayeredBackground(this.ClientSize.Width, this.ClientSize.Height, m_layeredWnd.LayeredPos, false);
                }
            }
            break;

            case Win32.Message.WM_MOUSEMOVE:
                //It's unlikely that we will get here unless this we really have captured the mouse
                //because the entire thing is transparent, but we check anyway just to make sure
                if (Win32.GetCapture() != IntPtr.Zero && m_moving)
                {
                    //In enhanced mode we draw the main window to the layered window and then hide the main
                    //window. This is so that we can have perfectly smooth motion when dragging the form, as
                    //we cannot gurantee that the forms will ever be moved together otherwise
                    if (m_enhanced)
                    {
                        //Setup the device contexts we are going to use
                        IntPtr hdcScreen = Win32.GetWindowDC(m_layeredWnd.Handle);                                      //Screen DC that the layered window will draw to
                        IntPtr windowDC  = Win32.GetDC(this.Handle);                                                    //Window DC that we are going to copy
                        IntPtr memDC     = Win32.CreateCompatibleDC(windowDC);                                          //Temporary DC that we draw to
                        IntPtr BmpMask   = Win32.CreateBitmap(this.ClientSize.Width,
                                                              this.ClientSize.Height, 1, 1, IntPtr.Zero);               //Mask bitmap so that we only draw areas of the form that are visible

                        Bitmap backImage = m_useBackgroundEx ? m_backgroundFull : m_background;
                        IntPtr BmpBack   = backImage.GetHbitmap(Color.FromArgb(0));                                     //Background Image

                        //Create mask
                        Win32.SelectObject(memDC, BmpMask);
                        uint oldCol = Win32.SetBkColor(windowDC, 0x00FF00FF);
                        Win32.BitBlt(memDC, 0, 0, this.ClientSize.Width, this.ClientSize.Height, windowDC, 0, 0, Win32.TernaryRasterOperations.SRCCOPY);
                        Win32.SetBkColor(windowDC, oldCol);

                        //Blit window to background image using mask
                        //We need to use the SPno raster operation with a white brush to combine our window
                        //with a black backround before putting it onto the 32-bit background image, otherwise
                        //we end up with blending issues (source and destination colours are ANDed together)
                        Win32.SelectObject(memDC, BmpBack);
                        IntPtr brush = Win32.CreateSolidBrush(0x00FFFFFF);
                        Win32.SelectObject(memDC, brush);
                        Win32.MaskBlt(memDC, 0, 0, backImage.Width, backImage.Height, windowDC, 0, 0, BmpMask, 0, 0, 0xCFAA0020);
                        //Win32.BitBlt(memDC, 0, 0, backImage.Width, backImage.Height, windowDC, m_offX, m_offY, Win32.TernaryRasterOperations.SRCCOPY);

                        Point zero = new Point(0, 0);
                        Size  size = m_layeredWnd.LayeredSize;
                        Point pos  = m_layeredWnd.LayeredPos;
                        Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
                        blend.AlphaFormat         = (byte)Win32.BlendOps.AC_SRC_ALPHA;
                        blend.BlendFlags          = (byte)Win32.BlendFlags.None;
                        blend.BlendOp             = (byte)Win32.BlendOps.AC_SRC_OVER;
                        blend.SourceConstantAlpha = (byte)(this.Opacity * 255);

                        Win32.UpdateLayeredWindow(m_layeredWnd.Handle, hdcScreen, ref pos, ref size, memDC, ref zero, 0, ref blend, Win32.BlendFlags.ULW_ALPHA);

                        //Clean up
                        Win32.ReleaseDC(IntPtr.Zero, hdcScreen);
                        Win32.ReleaseDC(this.Handle, windowDC);
                        Win32.DeleteDC(memDC);
                        Win32.DeleteObject(brush);
                        Win32.DeleteObject(BmpMask);
                        Win32.DeleteObject(BmpBack);

                        //Hide controls that are visible
                        this.SuspendLayout();
                        foreach (Control ctrl in this.Controls)
                        {
                            if (ctrl.Visible)
                            {
                                m_hiddenControls.Add(ctrl);
                                ctrl.Visible = false;
                            }
                        }
                        this.ResumeLayout();
                    }

                    //If we do not release the mouse then Windows will not start dragging the form, also
                    //it will mess up mouse input to any border on our form and other windows on the desktop
                    Win32.ReleaseCapture();
                    Win32.SendMessage(this.Handle, (int)Win32.Message.WM_NCLBUTTONDOWN, (int)Win32.Message.HTCAPTION, 0);
                }
                break;

            case Win32.Message.WM_SIZE:
            {
                //The updateLayeredSize function will check the width and height
                //we pass in, so we don't need to worry about updating the window
                //with the same size it already had
                int width  = m.LParam.ToInt32() & 0xFFFF;
                int height = m.LParam.ToInt32() >> 16;
                this.updateLayeredSize(width, height);
            }
            break;

            case Win32.Message.WM_WINDOWPOSCHANGING:
            {
                Win32.WINDOWPOS posInfo = (Win32.WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(Win32.WINDOWPOS));

                //We will cancel this movement, and send our own messages to position both forms
                //this way we can ensure that both forms are moved together and that
                //the Z order is unchanged.
                Win32.WindowPosFlags move_size = Win32.WindowPosFlags.SWP_NOMOVE | Win32.WindowPosFlags.SWP_NOSIZE;
                if ((posInfo.flags & move_size) != move_size)
                {
                    //Check for my own messages, which I do by setting to hwndInsertAfter to our
                    //own window, which from what I can gather only happens when you resize your
                    //window, never when you move it
                    if (posInfo.hwndInsertAfter != this.Handle)
                    {
                        IntPtr hwdp = Win32.BeginDeferWindowPos(2);
                        if (hwdp != IntPtr.Zero)
                        {
                            hwdp = Win32.DeferWindowPos(hwdp, m_layeredWnd.Handle, this.Handle, posInfo.x + m_offX, posInfo.y + m_offY,
                                                        0, 0, (uint)(posInfo.flags | Win32.WindowPosFlags.SWP_NOSIZE | Win32.WindowPosFlags.SWP_NOZORDER));
                        }
                        if (hwdp != IntPtr.Zero)
                        {
                            hwdp = Win32.DeferWindowPos(hwdp, this.Handle, this.Handle, posInfo.x, posInfo.y, posInfo.cx, posInfo.cy,
                                                        (uint)(posInfo.flags | Win32.WindowPosFlags.SWP_NOZORDER));
                        }
                        if (hwdp != IntPtr.Zero)
                        {
                            Win32.EndDeferWindowPos(hwdp);
                        }

                        m_layeredWnd.LayeredPos = new Point(posInfo.x + m_offX, posInfo.y + m_offY);

                        //Update the flags so that the form will not move with this message
                        posInfo.flags |= Win32.WindowPosFlags.SWP_NOMOVE;
                        Marshal.StructureToPtr(posInfo, m.LParam, true);
                    }

                    if ((posInfo.flags & Win32.WindowPosFlags.SWP_NOSIZE) == 0)
                    {
                        //Form was also resized
                        int diffX = posInfo.cx - this.Size.Width;
                        int diffY = posInfo.cy - this.Size.Height;
                        if (diffX != 0 || diffY != 0)
                        {
                            this.updateLayeredSize(this.ClientSize.Width + diffX, this.ClientSize.Height + diffY);
                        }
                    }

                    UseBase = false;
                }
            }
            break;


            case Win32.Message.WM_ACTIVATE:
            {
                //If WParam is Zero then the form is deactivating and we don't need to do anything
                //Otherwise we need to make sure that the background form is positioned just behind
                //the main form.
                if (m.WParam != IntPtr.Zero)
                {
                    //Check for any visible windows between the background and main windows
                    IntPtr prevWnd = Win32.GetWindow(m_layeredWnd.Handle, Win32.GetWindow_Cmd.GW_HWNDPREV);
                    while (prevWnd != IntPtr.Zero)
                    {
                        //If we find a visiable window, we stop
                        if (Win32.IsWindowVisible(prevWnd))
                        {
                            break;
                        }

                        prevWnd = Win32.GetWindow(prevWnd, Win32.GetWindow_Cmd.GW_HWNDPREV);
                    }

                    //If the visible window isn't ours reset the position of the background form
                    if (prevWnd != this.Handle)
                    {
                        Win32.SetWindowPos(m_layeredWnd.Handle, this.Handle, 0, 0, 0, 0, (uint)(Win32.WindowPosFlags.SWP_NOSENDCHANGING | Win32.WindowPosFlags.SWP_NOACTIVATE | Win32.WindowPosFlags.SWP_NOSIZE | Win32.WindowPosFlags.SWP_NOMOVE));
                    }
                }
            }
            break;
            }

            if (UseBase)
            {
                base.WndProc(ref m);
            }
        }
コード例 #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;
        }
コード例 #26
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);
            }
        }
コード例 #27
0
		/// <summary>
		/// Override for the main forms WndProc method
		/// This is used to intercept the forms movement so that we can move
		/// blended background form at the same time, as well as check for 
		/// when the form is activated so we can ensure the Z-order of our
		/// forms remains intact.
		/// </summary>
		/// <param name="m">Windows Message</param>
		protected override void WndProc(ref Message m)
		{
			if (this.DesignMode)
			{
				base.WndProc(ref m);
				return;
			}

			Win32.Message msgId = (Win32.Message)m.Msg;
			bool UseBase = true;
			switch (msgId)
			{
				case Win32.Message.WM_LBUTTONUP:
					{
						//Just in case
						if (Win32.GetCapture() != IntPtr.Zero)
							Win32.ReleaseCapture();
					}
					break;

				case Win32.Message.WM_ENTERSIZEMOVE:
					{

					}
					break;

				case Win32.Message.WM_EXITSIZEMOVE:
					{
						//We've stopped dragging the form, so lets make sure that our values are correct
						m_isDown.Left = false;
						m_moving = false;

						if (m_enhanced)
						{
							this.SuspendLayout();
							foreach (Control ctrl in m_hiddenControls)
								ctrl.Visible = true;
							m_hiddenControls.Clear();
							this.ResumeLayout();
							updateLayeredBackground(this.ClientSize.Width, this.ClientSize.Height, m_layeredWnd.LayeredPos, false);
						}
					}
					break;

				case Win32.Message.WM_MOUSEMOVE:
					//It's unlikely that we will get here unless this we really have captured the mouse
					//because the entire thing is transparent, but we check anyway just to make sure
					if (Win32.GetCapture() != IntPtr.Zero && m_moving)
					{
						//In enhanced mode we draw the main window to the layered window and then hide the main
						//window. This is so that we can have perfectly smooth motion when dragging the form, as
						//we cannot gurantee that the forms will ever be moved together otherwise
						if (m_enhanced)
						{
							//Setup the device contexts we are going to use
							IntPtr hdcScreen = Win32.GetWindowDC(m_layeredWnd.Handle);	//Screen DC that the layered window will draw to
							IntPtr windowDC = Win32.GetDC(this.Handle);					//Window DC that we are going to copy
							IntPtr memDC = Win32.CreateCompatibleDC(windowDC);			//Temporary DC that we draw to
							IntPtr BmpMask = Win32.CreateBitmap(this.ClientSize.Width, 
											this.ClientSize.Height, 1, 1, IntPtr.Zero);	//Mask bitmap so that we only draw areas of the form that are visible

							Bitmap backImage = m_useBackgroundEx ? m_backgroundFull : m_background;
							IntPtr BmpBack = backImage.GetHbitmap(Color.FromArgb(0));	//Background Image

							//Create mask
							Win32.SelectObject(memDC, BmpMask);
							uint oldCol = Win32.SetBkColor(windowDC, 0x00FF00FF);
							Win32.BitBlt(memDC, 0, 0, this.ClientSize.Width, this.ClientSize.Height, windowDC, 0, 0, Win32.TernaryRasterOperations.SRCCOPY);
							Win32.SetBkColor(windowDC, oldCol);

							//Blit window to background image using mask
							//We need to use the SPno raster operation with a white brush to combine our window
							//with a black backround before putting it onto the 32-bit background image, otherwise
							//we end up with blending issues (source and destination colours are ANDed together)
							Win32.SelectObject(memDC, BmpBack);
							IntPtr brush = Win32.CreateSolidBrush(0x00FFFFFF);
							Win32.SelectObject(memDC, brush);
							Win32.MaskBlt(memDC, 0, 0, backImage.Width, backImage.Height, windowDC, 0, 0, BmpMask, 0, 0, 0xCFAA0020);
							//Win32.BitBlt(memDC, 0, 0, backImage.Width, backImage.Height, windowDC, m_offX, m_offY, Win32.TernaryRasterOperations.SRCCOPY);

							Point zero = new Point(0, 0);
							Size size = m_layeredWnd.LayeredSize;
							Point pos = m_layeredWnd.LayeredPos;
							Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
							blend.AlphaFormat = (byte)Win32.BlendOps.AC_SRC_ALPHA;
							blend.BlendFlags = (byte)Win32.BlendFlags.None;
							blend.BlendOp = (byte)Win32.BlendOps.AC_SRC_OVER;
							blend.SourceConstantAlpha = (byte)(this.Opacity * 255);

							Win32.UpdateLayeredWindow(m_layeredWnd.Handle, hdcScreen, ref pos, ref size, memDC, ref zero, 0, ref blend, Win32.BlendFlags.ULW_ALPHA);

							//Clean up
							Win32.ReleaseDC(IntPtr.Zero, hdcScreen);
							Win32.ReleaseDC(this.Handle, windowDC);
							Win32.DeleteDC(memDC);
							Win32.DeleteObject(brush);
							Win32.DeleteObject(BmpMask);
							Win32.DeleteObject(BmpBack);

							//Hide controls that are visible
							this.SuspendLayout();
							foreach (Control ctrl in this.Controls) {
								if (ctrl.Visible) {
									m_hiddenControls.Add(ctrl);
									ctrl.Visible = false;
								}
							}
							this.ResumeLayout();
						}

						//If we do not release the mouse then Windows will not start dragging the form, also
						//it will mess up mouse input to any border on our form and other windows on the desktop
						Win32.ReleaseCapture();
						Win32.SendMessage(this.Handle, (int)Win32.Message.WM_NCLBUTTONDOWN, (int)Win32.Message.HTCAPTION, 0);

					}
					break;

				case Win32.Message.WM_SIZE:
					{
						//The updateLayeredSize function will check the width and height
						//we pass in, so we don't need to worry about updating the window
						//with the same size it already had
						int width = m.LParam.ToInt32() & 0xFFFF;
						int height = m.LParam.ToInt32() >> 16;
						this.updateLayeredSize(width, height);
					}
					break;

				case Win32.Message.WM_WINDOWPOSCHANGING:
					{
						Win32.WINDOWPOS posInfo = (Win32.WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(Win32.WINDOWPOS));

						//We will cancel this movement, and send our own messages to position both forms
						//this way we can ensure that both forms are moved together and that
						//the Z order is unchanged.
						Win32.WindowPosFlags move_size = Win32.WindowPosFlags.SWP_NOMOVE | Win32.WindowPosFlags.SWP_NOSIZE;
						if ((posInfo.flags & move_size) != move_size)
						{
							//Check for my own messages, which I do by setting to hwndInsertAfter to our 
							//own window, which from what I can gather only happens when you resize your
							//window, never when you move it
							if (posInfo.hwndInsertAfter != this.Handle)
							{
								IntPtr hwdp = Win32.BeginDeferWindowPos(2);
								if (hwdp != IntPtr.Zero)
									hwdp = Win32.DeferWindowPos(hwdp, m_layeredWnd.Handle, this.Handle, posInfo.x + m_offX, posInfo.y + m_offY, 
											0, 0, (uint)(posInfo.flags | Win32.WindowPosFlags.SWP_NOSIZE | Win32.WindowPosFlags.SWP_NOZORDER));
								if (hwdp != IntPtr.Zero)
									hwdp = Win32.DeferWindowPos(hwdp, this.Handle, this.Handle, posInfo.x, posInfo.y, posInfo.cx, posInfo.cy, 
											(uint)(posInfo.flags | Win32.WindowPosFlags.SWP_NOZORDER));
								if (hwdp != IntPtr.Zero)
									Win32.EndDeferWindowPos(hwdp);

								m_layeredWnd.LayeredPos = new Point(posInfo.x + m_offX, posInfo.y + m_offY);

								//Update the flags so that the form will not move with this message
								posInfo.flags |= Win32.WindowPosFlags.SWP_NOMOVE;
								Marshal.StructureToPtr(posInfo, m.LParam, true);
							}

							if ((posInfo.flags & Win32.WindowPosFlags.SWP_NOSIZE) == 0)
							{
								//Form was also resized
								int diffX = posInfo.cx - this.Size.Width;
								int diffY = posInfo.cy - this.Size.Height;
								if(diffX != 0 || diffY != 0)
									this.updateLayeredSize(this.ClientSize.Width + diffX, this.ClientSize.Height + diffY);
							}

							UseBase = false;
						}
					}
					break;


				case Win32.Message.WM_ACTIVATE:
					{
						//If WParam is Zero then the form is deactivating and we don't need to do anything
						//Otherwise we need to make sure that the background form is positioned just behind
						//the main form.
						if (m.WParam != IntPtr.Zero)
						{
							//Check for any visible windows between the background and main windows
							IntPtr prevWnd = Win32.GetWindow(m_layeredWnd.Handle, Win32.GetWindow_Cmd.GW_HWNDPREV);
							while (prevWnd != IntPtr.Zero)
							{
								//If we find a visiable window, we stop
								if (Win32.IsWindowVisible(prevWnd))
									break;

								prevWnd = Win32.GetWindow(prevWnd, Win32.GetWindow_Cmd.GW_HWNDPREV);
							}

							//If the visible window isn't ours reset the position of the background form
							if (prevWnd != this.Handle)
								Win32.SetWindowPos(m_layeredWnd.Handle, this.Handle, 0, 0, 0, 0, (uint)(Win32.WindowPosFlags.SWP_NOSENDCHANGING | Win32.WindowPosFlags.SWP_NOACTIVATE | Win32.WindowPosFlags.SWP_NOSIZE | Win32.WindowPosFlags.SWP_NOMOVE));
						}
					}
					break;
			}

			if (UseBase)
				base.WndProc(ref m);
		}
コード例 #28
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();
             * }*/
        }
コード例 #29
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);
        }
    }
コード例 #30
0
ファイル: PopupMenu.cs プロジェクト: bbriggs/FieldWorks
		protected void UpdateLayeredWindow(Point point, Size size)
		{
			// Create bitmap for drawing onto
			using (Bitmap memoryBitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb))
			using(Graphics g = Graphics.FromImage(memoryBitmap))
			{
				Rectangle area = new Rectangle(0, 0, size.Width, size.Height);

				// Draw the background area
				DrawBackground(g, area);

				// Draw the actual menu items
				DrawAllCommands(g);

				// Get hold of the screen DC
				IntPtr hDC = WindowsAPI.GetDC(IntPtr.Zero);

				// Create a memory based DC compatible with the screen DC
				IntPtr memoryDC = WindowsAPI.CreateCompatibleDC(hDC);

				// Get access to the bitmap handle contained in the Bitmap object
				IntPtr hBitmap = memoryBitmap.GetHbitmap(Color.FromArgb(0));

				// Select this bitmap for updating the window presentation
				IntPtr oldBitmap = WindowsAPI.SelectObject(memoryDC, hBitmap);

				// New window size
				Win32.SIZE ulwsize;
				ulwsize.cx = size.Width;
				ulwsize.cy = size.Height;

				// New window position
				Win32.POINT topPos;
				topPos.x = point.X;
				topPos.y = point.Y;

				// Offset into memory bitmap is always zero
				Win32.POINT pointSource;
				pointSource.x = 0;
				pointSource.y = 0;

				// We want to make the entire bitmap opaque
				Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
				blend.BlendOp             = (byte)Win32.AlphaFlags.AC_SRC_OVER;
				blend.BlendFlags          = 0;
				blend.SourceConstantAlpha = 255;
				blend.AlphaFormat         = (byte)Win32.AlphaFlags.AC_SRC_ALPHA;

				// Tell operating system to use our bitmap for painting
				WindowsAPI.UpdateLayeredWindow(Handle, hDC, ref topPos, ref ulwsize,
										   memoryDC, ref pointSource, 0, ref blend,
										   (int)Win32.UpdateLayeredWindowsFlags.ULW_ALPHA);

				// Put back the old bitmap handle
				WindowsAPI.SelectObject(memoryDC, oldBitmap);

				// Cleanup resources
				WindowsAPI.ReleaseDC(IntPtr.Zero, hDC);
				WindowsAPI.DeleteObject(hBitmap);
				WindowsAPI.DeleteDC(memoryDC);
			}
		}
コード例 #31
0
        private void UpdateLayeredWindow(Rectangle rect)
        {
            // Must have a visible rectangle to render
            if ((rect.Width > 0) && (rect.Height > 0))
            {
                // Create bitmap for drawing onto
                Bitmap memoryBitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);

                using (Graphics g = Graphics.FromImage(memoryBitmap))
                {
                    Rectangle area = new Rectangle(0, 0, rect.Width, rect.Height);

                    // Draw the background area
                    DrawShadow(g, area);

                    // Get hold of the screen DC
                    IntPtr hDC = User32.GetDC(IntPtr.Zero);

                    // Create a memory based DC compatible with the screen DC
                    IntPtr memoryDC = Gdi32.CreateCompatibleDC(hDC);

                    // Get access to the bitmap handle contained in the Bitmap object
                    IntPtr hBitmap = memoryBitmap.GetHbitmap(Color.FromArgb(0));

                    // Select this bitmap for updating the window presentation
                    IntPtr oldBitmap = Gdi32.SelectObject(memoryDC, hBitmap);

                    // New window size
                    Win32.SIZE ulwsize;
                    ulwsize.cx = rect.Width;
                    ulwsize.cy = rect.Height;

                    // New window position
                    Win32.POINT topPos;
                    topPos.x = rect.Left;
                    topPos.y = rect.Top;

                    // Offset into memory bitmap is always zero
                    Win32.POINT pointSource;
                    pointSource.x = 0;
                    pointSource.y = 0;

                    // We want to make the entire bitmap opaque
                    Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION();
                    blend.BlendOp             = (byte)Win32.AlphaFlags.AC_SRC_OVER;
                    blend.BlendFlags          = 0;
                    blend.SourceConstantAlpha = 255;
                    blend.AlphaFormat         = (byte)Win32.AlphaFlags.AC_SRC_ALPHA;

                    // Tell operating system to use our bitmap for painting
                    User32.UpdateLayeredWindow(Handle, hDC, ref topPos, ref ulwsize,
                                               memoryDC, ref pointSource, 0, ref blend,
                                               (int)Win32.UpdateLayeredWindowsFlags.ULW_ALPHA);

                    // Put back the old bitmap handle
                    Gdi32.SelectObject(memoryDC, oldBitmap);

                    // Cleanup resources
                    User32.ReleaseDC(IntPtr.Zero, hDC);
                    Gdi32.DeleteObject(hBitmap);
                    Gdi32.DeleteDC(memoryDC);
                }
            }
        }