Semi Transparent PNG as Splash Screen

2019-01-28 19:28发布

问题:

I'm trying to make a Splash Screen 4 an Win application.

my setup:

form border style is set to none. start position is screen center. background image of the form is set to a PNG file, with rounded edges and a "build in" drop shadow.

In code I've set:

this.SetStyle( ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle( ControlStyles.UserPaint, true);
this.SetStyle( ControlStyles.DoubleBuffer, true);
this.SetStyle( ControlStyles.SupportsTransparentBackColor, true);

this.AllowTransparency = true;
this.BackColor = Color.Transparent;

but when i test, it says that the form can't have a transparent background color.

i DO NOT want to set a transparency key, cuz it causes trouble with the dropschadow ( semi transparent part of the png )

also i dont want to set opacity to 0%, cuz it also effects my PNG.

in fact i just want ONLY my png shown as the window. additionaly there will be some dynamic text on top of it and a process bar in the future...

Any ideas? how to tell the form that is CAN have transparent background like the splash screen of ADOBE PHOTOSHOP CS5

回答1:

I spent a few hours looking for a way to do this in Win Forms as well so I thought I would share my solution.

My splash screen image is a .png with a transparent background and various shadows that extend over the transparent background. Using uncommon colors as the background of the control along with a transparency key left ugly patches underneath the semi-transparent shadows.

I was able to get the desired result by setting the background image of the form to the image I wanted to display and overriding the OnPaintBackground function like so:

    bool painted = false
    protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
    {
        if (painted) return;
        e.Graphics.DrawImage(BackgroundImage, new System.Drawing.Point(0, 0));
        painted = true;
    }

I'm only bumping this old thread because it's the top Google result for a few different keyword combos that I tried.

See also Transparent Splash Screen which is where I found this solution from another SO post.



回答2:

Here is a simple example of a WPF splash screen. This is all in XAML. I didn't write a line of C# to make it work.

<Window x:Class="WpfSplashScreen.SplashScreen"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStyle="None" Background="Transparent" AllowsTransparency="True"
        ShowInTaskbar="False" SizeToContent="WidthAndHeight"
        WindowStartupLocation="CenterScreen">
    <Image Source="logo.png" Stretch="None" />
</Window>

This is just an example, and would need some more code to make it useful, but that is how easy a splash screen is in WPF.