Truly Transparent Shaped Form

5 posts, 2 answers
  1. Brad
    Brad avatar
    30 posts
    Member since:
    Sep 2010

    Posted 02 Nov 2012 Link to this post

    I've already read this post:  http://www.telerik.com/community/forums/winforms/forms-and-dialogs/transparent-background-for-rad-shapedform.aspx   It is close, but not quite what I need.

    How do we use a PNG file with tranparency to make a truly transparent form?  Any time I try this, truly empty areas in the PNG work fine, but partially transparent areas (like shadows) are underlayed with the "transparent" color.

    I've attached three images:
    - Screenshot of the intended effect.  (The welcome/splash screen from Visual Studio.)
    - Screenshot of setting the shaped form control background and transparent color to blue with my transparent PNG as the background.
    - The form background image I am using to test.

    Any help you can offer is greatly appreciated.

    Thanks,

    - Brad
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 07 Nov 2012 Link to this post

    Hello Brad,

    Thank you for writing.

    The scenario is not possible to achieve in all cases due to the native windows implementation which causes some rendering issues when the transparent window is repainted. This is related for all Forms (including Microsoft Form control).

    I hope that you find this information useful.

    Regards,
    Svett
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Brad
    Brad avatar
    30 posts
    Member since:
    Sep 2010

    Posted 07 Nov 2012 Link to this post

    I'll continue to look; someone knows how to do this.  There are too many applications (including Visual Studio) with transparent splash screens.

    It seems this would be a prime candidate for a feature to add to the shaped form control.

    Thanks for your reply.
  5. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 12 Nov 2012 Link to this post

    Hi Brad,

    As a starting point of your research you can use the following approach:

    public partial class TransparentShapedForm : ShapedForm
        {
            public TransparentShapedForm()
            {
                InitializeComponent();
     
                this.FormBorderStyle = FormBorderStyle.None;
                this.StartPosition = FormStartPosition.CenterScreen;
            }
     
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    cp.ExStyle |= NativeMethods.WS_EX_TRANSPARENT;
     
                    return cp;
                }
            }
     
     
            protected override void OnPaintBackground(PaintEventArgs e)
            {
                // Don't paint background
            }
     
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
     
                Image image = this.BackgroundImage;
     
                if (image == null)
                {
                    return;
                }
     
                Graphics graphics = e.Graphics;
     
                graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     
                graphics.DrawImage(image, Point.Empty);
            }
        }

    Notice that the code above has some glitches when the form is overlapped by another form. If you can rid of them, you will complete your scenario. It seems that the windows messages and rendering do not deal well with the custom form. The glitches occur with the standard Form and ShapedForm and are not related to the Telerik products. All the best,
    Svett
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  6. Answer
    Brad
    Brad avatar
    30 posts
    Member since:
    Sep 2010

    Posted 12 Nov 2012 Link to this post

    http://www.codeproject.com/Articles/1822/Per-Pixel-Alpha-Blend-in-C

    For anyone else trying the same thing, I grabbed code from the link above and incorporated that into my form to accomplish what I needed.

Back to Top
UI for WinForms is Visual Studio 2017 Ready