1

我创建了一个顶级表单并将其 Region 属性更改为一个椭圆区域。然后,我使用 AnimateWindow 函数使表单淡入。当窗体淡入时,仍显示窗体的矩形形状。当形态结束褪色时,矩形消失,取而代之的是椭圆形。有没有人遇到同样的问题?下面是代码:

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());
        }
    }
}
4

0 回答 0