Question

I created a top level form and changed its Region property to an eliptical region. Then, I made the form to fade in using the AnimateWindow function. When the form was fading in, the rectangular shape of the form was still displayed. When the form finished fading, the rectangular shape disappeared and was replaced by the eliptical shape. Does anyone encounter the same problem? Below is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace AnimateWindowSample
{
    public class MainForm : Form
    {
        private Button button_Show;
        private ChildForm childForm;
        private bool childFormShown = false;

        public MainForm()
        {
            this.button_Show = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button_Show
            // 
            this.button_Show.Location = new System.Drawing.Point(12, 12);
            this.button_Show.Name = "button_Show";
            this.button_Show.Size = new System.Drawing.Size(105, 23);
            this.button_Show.TabIndex = 0;
            this.button_Show.Text = "&Show/Hide";
            this.button_Show.UseVisualStyleBackColor = true;
            this.button_Show.Click += new EventHandler(button_Show_Click);
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button_Show);
            this.Name = "MainForm";
            this.Text = "MainForm";
            this.ResumeLayout(false);
        }

        void button_Show_Click(object sender, EventArgs e)
        {
            if (this.childForm == null)
            {
                this.childForm = new ChildForm();
            }

            if (this.childFormShown)
            {
                this.childForm.FadeOut();
            }
            else
            {
                this.childForm.FadeIn();
            }

            this.childFormShown = !this.childFormShown;
        }
    }

    public class ChildForm : Form
    {
        const int CS_DROPSHADOW = 0x00020000;
        const int WS_EX_TOPMOST = 0x00000008;
        const int AW_HIDE = 0x00010000;
        const int AW_BLEND = 0x00080000;

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        static extern bool AnimateWindow(IntPtr hWnd, uint dwTime, uint dwFlags);

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;

                cp.ClassStyle |= CS_DROPSHADOW;
                cp.ExStyle |= WS_EX_TOPMOST;

                return cp;
            }
        }

        protected override bool ShowWithoutActivation
        {
            get
            {
                return true;
            }
        }

        public ChildForm()
        {
            this.FormBorderStyle = FormBorderStyle.None;

            GraphicsPath path = new GraphicsPath();

            path.AddEllipse(this.Bounds);

            Region region = new Region(path);

            this.Region = region;
        }

        public void FadeIn()
        {
            AnimateWindow(this.Handle, 300, AW_BLEND);
        }

        public void FadeOut()
        {
            AnimateWindow(this.Handle, 300, AW_BLEND | AW_HIDE);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Rectangle bounds = this.ClientRectangle;

            e.Graphics.FillRectangle(Brushes.Black, bounds);
            e.Graphics.FillEllipse(Brushes.Blue, bounds);
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top