如何设计一个很酷的半透明防溅屏?

时间:2014-04-10 05:20:51

标签: c# winforms visual-studio-2010 png

我正在尝试设计一个半透明的屏幕作为Adobe Photoshop&Visual Studio半透明欢迎闪屏同时打开应用程序。我将FormBorderStyle设置为none,然后我在其上放置了一个图片框,我添加了一个png图像,我将图片框颜色设置为透明,但我无法将Form的背面颜色设置为透明。以下是例子:

Photo shop start up splash screen

Visual studio start up splash screen

当我将表单的Back Color设置为透明时,它会显示错误

  

属性无效。控件不支持透明背景颜色。

我已经尝试了几个代码示例,如下所示:

    public Splash_Screen()
{
    this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    InitializeComponent();
    this.BackColor = Color.Red;
    BackColor = Color.Transparent;
}

    public Splash_Screen()
{
    this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    InitializeComponent();
}

但nothings对我有用..有什么建议吗?

2 个答案:

答案 0 :(得分:8)

将半透明背景图像设置为表单的BackgroundImage属性。然后在表单中设置BackColor,并将表单的TransparencyKey属性设置为您为表单BackColor设置的相同颜色。然后通过将表单的FormBorderStyle属性更改为None来删除表单的边框。那样做。

答案 1 :(得分:1)

这个问题很旧,但我决定加入,因为这是“透明启动画面C#”的第一个结果,并且Tolga的解决方案可以在图像的某些边缘周围产生TransparencyKey颜色的奇怪边界。如果其线条反锯齿化了半透明像素,则更为明显。

我从here复制了此代码。不幸的是,我和海报作者都不认识原始作者。

我还进行了一些修改,以支持更改表单的不透明度。

Splash.cs:

class Splash : Form {
    private float _opacity = 1.0f;
    public Bitmap BackgroundBitmap;

    public new float Opacity {
        get {
            return _opacity;
        }
        set {
            _opacity = value;
            SelectBitmap(BackgroundBitmap);
        }
    }

    public Splash(Bitmap bitmap) {
        // Window settings   
        this.TopMost = true;
        this.ShowInTaskbar = false;
        this.Size = bitmap.Size;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        // Must be called before setting bitmap   
        this.BackgroundBitmap = bitmap;
        this.SelectBitmap(BackgroundBitmap);
        this.BackColor = Color.Red;
    }

    // Sets the current bitmap
    public void SelectBitmap(Bitmap bitmap) {
        // Does this bitmap contain an alpha channel?   
        if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) {
            throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
        }
        // Get device contexts   
        IntPtr screenDc = APIHelp.GetDC(IntPtr.Zero);
        IntPtr memDc = APIHelp.CreateCompatibleDC(screenDc);
        IntPtr hBitmap = IntPtr.Zero;
        IntPtr hOldBitmap = IntPtr.Zero;
        try {
            // Get handle to the new bitmap and select it into the current device context      
            hBitmap = bitmap.GetHbitmap(Color.FromArgb(0));
            hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);
            // Set parameters for layered window update      
            APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height);
            // Size window to match bitmap      
            APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
            APIHelp.Point newLocation = new APIHelp.Point(this.Left, this.Top);
            // Same as this window      
            APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION();
            blend.BlendOp = APIHelp.AC_SRC_OVER;
            // Only works with a 32bpp bitmap      
            blend.BlendFlags = 0; // Always 0   
            blend.SourceConstantAlpha = (byte)(Opacity * 255); // Set to 255 for per-pixel alpha values
            blend.AlphaFormat = APIHelp.AC_SRC_ALPHA;
            // Only works when the bitmap contains an alpha channel      
            // Update the window      
            APIHelp.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize, memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
        } finally {
            // Release device context      
            APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
            if (hBitmap != IntPtr.Zero) {
                APIHelp.SelectObject(memDc, hOldBitmap);
                APIHelp.DeleteObject(hBitmap);
                // Remove bitmap resources      
            }
            APIHelp.DeleteDC(memDc);
        }
    }

    protected override CreateParams CreateParams {
        get {
            // Add the layered extended style (WS_EX_LAYERED) to this window      
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= APIHelp.WS_EX_LAYERED;
            return createParams;
        }
    }

    // Let Windows drag this window for us (thinks its hitting the title bar of the window)
    protected override void WndProc(ref Message message) {
        if (message.Msg == APIHelp.WM_NCHITTEST) {
            // Tell Windows that the user is on the title bar (caption)      
            message.Result = (IntPtr)APIHelp.HTCAPTION;
        } else {
            base.WndProc(ref message);
        }
    }
}

APIHelp.cs:

// Class to assist with Win32 API calls
class APIHelp { 
    public const Int32 WS_EX_LAYERED = 0x80000;
    public const Int32 HTCAPTION = 0x02;
    public const Int32 WM_NCHITTEST = 0x84;
    public const Int32 ULW_ALPHA = 0x02;
    public const byte AC_SRC_OVER = 0x00;
    public const byte AC_SRC_ALPHA = 0x01;

    public enum Bool {
        False = 0, True = 1
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct Point {
        public Int32 x;
        public Int32 y;
        public Point(Int32 x, Int32 y) {
            this.x = x;
            this.y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct Size {
        public Int32 cx;
        public Int32 cy;
        public Size(Int32 cx, Int32 cy) {
            this.cx = cx;
            this.cy = cy;
        }
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    struct ARGB {
        public byte Blue;
        public byte Green;
        public byte Red;
        public byte Alpha;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct BLENDFUNCTION {
        public byte BlendOp;
        public byte BlendFlags;
        public byte SourceConstantAlpha;
        public byte AlphaFormat;
    }

    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern IntPtr GetDC(IntPtr hWnd);

    [DllImport("user32.dll", ExactSpelling = true)]
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern Bool DeleteDC(IntPtr hdc);

    [DllImport("gdi32.dll", ExactSpelling = true)]
    public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    public static extern Bool DeleteObject(IntPtr hObject);
}