C# 从屏幕侧面淡入

C# 从屏幕侧面淡入,c#,animation,slide,C#,Animation,Slide,我正在尝试制作一个简单的侧边栏,它将从桌面左侧滑入滑出。这是我目前的代码,但它实际上不起作用,我担心它可能效率低下 private void fadeIn() { if (this.Width == 1) while (this.Size.Width < 36) { this.Size = new Size(this.Size.Width + 1, Screen.PrimaryS

我正在尝试制作一个简单的侧边栏,它将从桌面左侧滑入滑出。这是我目前的代码,但它实际上不起作用,我担心它可能效率低下

    private void fadeIn()
    {
        if (this.Width == 1)
            while (this.Size.Width < 36)
            {
                this.Size = new Size(this.Size.Width + 1, Screen.PrimaryScreen.Bounds.Width - this.Width);
                System.Threading.Thread.Sleep(2);
                this.Invalidate();
                Application.DoEvents();
            }
    }

    private void fadeOut()
    {
        if (this.Width == 36)
            while (this.Size.Width > 1)
            {
                this.Size = new Size(this.Size.Width - 1, Screen.PrimaryScreen.Bounds.Width - this.Width);
                System.Threading.Thread.Sleep(2);
                this.Invalidate();
                Application.DoEvents();
            }
    }

希望有人能帮我解决这个问题。它应该相当简单。

我想你的问题是关于用淡色缩放表单,如果是,你可以使用这个类:

using System;
using System.Runtime.InteropServices;

namespace formAnimation
{
    /// <summary>
    /// Win32 implementation to show / hide a window with animation.
    /// </summary>

    public class WinAPI
    {
        /// <summary>
        /// Animates the window from left to right. This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_HOR_POSITIVE = 0X1;
        /// <summary>
        /// Animates the window from right to left. This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_HOR_NEGATIVE = 0X2;
        /// <summary>
        /// Animates the window from top to bottom. This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_VER_POSITIVE = 0X4;
        /// <summary>
        /// Animates the window from bottom to top. This flag can be used with roll or slide animation.
        /// </summary>
        public const int AW_VER_NEGATIVE = 0X8;
        /// <summary>
        /// Makes the window appear to collapse inward if AW_HIDE is used or expand outward if the AW_HIDE is not used.
        /// </summary>
        public const int AW_CENTER = 0X10;
        /// <summary>
        /// Hides the window. By default, the window is shown.
        /// </summary>
        public const int AW_HIDE = 0X10000;
        /// <summary>
        /// Activates the window.
        /// </summary>
        public const int AW_ACTIVATE = 0X20000;
        /// <summary>
        /// Uses slide animation. By default, roll animation is used.
        /// </summary>
        public const int AW_SLIDE = 0X40000; 
        /// <summary>
        /// Uses a fade effect. This flag can be used only if hwnd is a top-level window.
        /// </summary>
        public const int AW_BLEND = 0X80000;

        /// <summary>
        /// Animates a window.
        /// </summary>
        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static  extern int AnimateWindow (IntPtr hwand , int dwTime , int dwFlags) ;     
    } 
}

如果您想看到使用此代码的简单演示,请从

下载此开源代码,这是一种糟糕的方法。试着用定时器来做。您不应该像那样阻塞ui线程,也不应该为此使用DoEvents。
WinAPI.AnimateWindow (this.Handle, animationTime, flags);