【问题标题】:Partially transparent splash screen in C#C# 中的部分透明闪屏
【发布时间】:2015-12-05 15:10:26
【问题描述】:

我正在尝试为我的应用程序制作启动画面。我有一个 .PNG 图像,其中有一些部分透明的部分。

我已使表单无边框并禁用了控制框。但是,背景颜色会导致问题。

首先,我将图像放在图片框中。然后我以这种方式使表单背景透明:

this.BackColor = Color.Magenta;
this.TransparencyKey = Color.Magenta;

它有点工作。屏幕的全透明部分是透明的,但在部分透明的部分可以看到洋红色。

然后我尝试将图像作为表单的背景图像。我尝试使用此代码使表单的背景透明:

private void Form1_Load(object sender, EventArgs e)
{
   this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
   this.BackColor = System.Drawing.Color.Transparent;
}

它也没有工作。背景仍然不透明。 我能做什么?

【问题讨论】:

标签: c# winforms


【解决方案1】:

为什么是洋红色?尝试使用白色。我将背景颜色设置为白色,透明度键设置为白色。在下图中,带有图片的随机框是我的飞溅。

【讨论】:

  • 那不行,因为部分透明的部分是阴影。
  • 可以发图片或图片吗?
  • 我决定使用这种方法。我从飞溅中移除了阴影,因此只剩下完整的透明度或没有透明度。不是部分透明。
【解决方案2】:

TransparencyKey 会将表单设置为完全不透明,除了指定的确切颜色的像素。 AFAIK 它通过实际定义WindowRegion 来做到这一点,即基于与颜色匹配的像素的窗口形状 - 这些像素不再是您窗口的一部分,可以点击。

听起来你的 PNG 有一个 alpha 层,即透明度级别 0..255,它与你的背景颜色(洋红色)混合,之后只有纯洋红色像素变得透明。

你可以试试:

  • 选择一种颜色,例如Magenta 未在您的启动画面中使用,并将其放入您的启动画面 PNG 中,以代替 完全 透明区域作为不透明像素
  • 在窗口显示之前捕获桌面位图的副本,并将其放在启动画面下的 PictureBox 中。

然后,飞溅的部分透明区域将与背景图像混合,完全透明的区域将匹配TransparencyKey 并且是透明的。

但是,如果在启动画面显示时桌面的其余部分发生变化,如果您的启动画面移动等,您将获得视觉伪影。

编辑:似乎有一个更简单的解决方案 here 不使用 TransparencyKey

【讨论】:

    【解决方案3】:

    您可以使用layered windows 和UpdateLayeredWindow() Api 来完成它:

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

    并以您的形式load event:

    IntPtr memDc, hBmp, hOldBmp;
    
    private void Form1_Load(object sender, EventArgs e)
    {
        APIHelp.BLENDFUNCTION blend;
    
        //Only works with a 32bpp bitmap
        blend.BlendOp = APIHelp.AC_SRC_OVER;
        //Always 0
        blend.BlendFlags = 0;
        //Set to 255 for per-pixel alpha values
        blend.SourceConstantAlpha = 255;
        //Only works when the bitmap contains an alpha channel
        blend.AlphaFormat = APIHelp.AC_SRC_ALPHA;
    
        IntPtr screenDc;
    
        screenDc = APIHelp.GetDC(IntPtr.Zero);
    
        Bitmap bmp;
    
        using (bmp = (Bitmap)Bitmap.FromFile(@"C:\.......png")) //the image must be the same size as your form
        {
            memDc = APIHelp.CreateCompatibleDC(screenDc);
            hBmp = bmp.GetHbitmap(Color.FromArgb(0));
            hOldBmp = APIHelp.SelectObject(memDc, hBmp); //memDc is a device context that contains our image
        }
    
        APIHelp.DeleteDC(screenDc);
    
        APIHelp.Size newSize;
        APIHelp.Point newLocation;
        APIHelp.Point sourceLocation;
    
        newLocation.x = this.Location.X;
        newLocation.y = this.Location.Y;
    
        sourceLocation.x = 0;
        sourceLocation.y = 0;
    
        newSize.cx = this.Width;
        newSize.cy = this.Height;
    
        APIHelp.UpdateLayeredWindow(Handle, IntPtr.Zero, ref newLocation, ref newSize, memDc, ref sourceLocation, 
               0, ref blend, APIHelp.ULW_ALPHA);
    }
    

    类 API 帮助:

    class APIHelp
    {
        public const Int32 WS_EX_LAYERED = 524288;
        public const Int32 ULW_ALPHA = 2;
        public const byte AC_SRC_OVER = 0;
        public const byte AC_SRC_ALPHA = 1;
    
        [StructLayout(LayoutKind.Sequential)] 
        public struct Point
        {
            public Int32 x;
            public Int32 y;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct Size
        {
            public Int32 cx;
            public Int32 cy;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct ARGB
        {
            public byte Blue;
            public byte Green;
            public byte Red;
            public byte Alpha;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        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 pptSrc, uint crKey,
           [In] ref BLENDFUNCTION pblend, uint dwFlags);
    
    
        [DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC", SetLastError = true)]
        public static extern IntPtr CreateCompatibleDC([In] IntPtr hdc);
    
        [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
        public static extern IntPtr SelectObject([In] IntPtr hdc, [In] IntPtr hgdiobj);
    
        [DllImport("user32.dll")]
        public static extern IntPtr GetDC(IntPtr hWnd);
    
        [DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
        public static extern bool DeleteDC([In] IntPtr hdc);
    
        [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeleteObject([In] IntPtr hObject);
    }
    

    当表单关闭时释放资源:

    APIHelp.SelectObject(memDc, hOldBmp);
    APIHelp.DeleteObject(hBmp);
    APIHelp.DeleteDC(memDc);
    

    【讨论】:

    • 六年后,这段代码就像一个魅力!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2017-04-04
    • 1970-01-01
    • 2010-12-30
    • 2012-02-24
    • 2010-10-26
    • 2010-12-05
    相关资源
    最近更新 更多