带图像的透明Winform

时间:2009-04-23 13:57:03

标签: winforms transparency

我有一个图像(PNG文件),其alpha通道设置为50%不透明。当我尝试在TransparencyKey设置为白色并且背景颜色设置为白色的表单上绘制图像时,我希望图像被绘制为50%透视。然而,它首先与表单混合颜色混合,因此它完全不透明。有没有办法解决?我不想设置窗体的不透明属性,因为窗体上的某些图像需要是半透明的,有些需要是不透明的。

3 个答案:

答案 0 :(得分:1)

我最终使用了一个分层窗口,使用WS_EX_LAYERED扩展窗口样式。

http://msdn.microsoft.com/en-us/library/ms997507.aspx

答案 1 :(得分:0)

我认为你不能。我们有一个启动画面,我们做了类似的事情,但我们最终捕获了屏幕并将其设置为表单的背景图像。显然这似乎只是工作,如果屏幕改变,表单的背景不会和事情看起来很奇怪。如果你找到了更好的方法,我很想知道它。

以下是捕获屏幕的代码,只需将ScreenRect设置为窗体屏幕坐标并调用Process():

using System;
using System.Drawing;
using System.Runtime.InteropServices;

namespace TourFactory.Core.Drawing
{
    public class CaptureScreenCommand
    {

        #region Initialization and Destruction

        public CaptureScreenCommand()
        {
        }

        #endregion

        #region Fields and Properties

        // BitBlt is a multipurpose function that takes a ROP (Raster OPeration) code
        // that controls exactly what it does. 0xCC0020 is the ROP code SRCCOPY, i.e.
        // do a simple copy from the source to the destination.
        private const int cRasterOp_SrcCopy = 0xCC0020; // 13369376;

        private Rectangle mScreenRect;
        /// <summary>
        /// Gets or sets the screen coordinates to capture.
        /// </summary>
        public Rectangle ScreenRect
        {
            get { return mScreenRect; }
            set { mScreenRect = value; }
        }

        #endregion

        #region Methods

        public Image Process()
        {
            // use the GDI call and create a DC to the whole display
            var dc1 = CreateDC("DISPLAY", null, 0, 0);
            var g1 = Graphics.FromHdc(dc1);

            // create a compatible bitmap the size of the form
            var bmp = new Bitmap(mScreenRect.Width, mScreenRect.Height, g1);
            var g2 = Graphics.FromImage(bmp);

            // Now go retrace our steps and get the device contexts for both the bitmap and the screen
            // Note: Apparently you have to do this, and can't go directly from the aquired dc or exceptions are thrown
            // when you try to release the dcs
            dc1 = g1.GetHdc();
            var dc2 = g2.GetHdc();

            // Bit Blast the screen into the Bitmap
            BitBlt(dc2, 0, 0, mScreenRect.Width, mScreenRect.Height, dc1, mScreenRect.Left, mScreenRect.Top, cRasterOp_SrcCopy);

            // Remember to release the dc's, otherwise problems down the road
            g1.ReleaseHdc(dc1);
            g2.ReleaseHdc(dc2);

            // return bitmap
            return bmp;
        }

        #endregion

        #region gdi32.dll

        [DllImport("gdi32")]
        private static extern IntPtr CreateDC(string lpDriverName, string lpDeviceName, int lpOutput, int lpInitData);

        [DllImport("gdi32")]
        private static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int width, int height, IntPtr hdcSrc, int xSrc, int ySrc, int dwRop);

        #endregion

    }
}

答案 2 :(得分:0)

尼斯。不要忘记Vista有桌面窗口管理器来创建半透明窗口(又名Areo)http://msdn.microsoft.com/en-us/magazine/cc163435.aspx