【问题标题】:Fade in from side of screen从屏幕一侧淡入
【发布时间】:2014-09-11 08:41:02
【问题描述】:

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

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

希望有人可以帮助我解决这个问题。应该很简单。

【问题讨论】:

  • 这是一种糟糕的做法。尝试在计时器中执行此操作。你不应该像那样阻塞 ui 线程(或者使用 DoEvents)。

标签: c# animation slide


【解决方案1】:

我认为您关于淡入淡出缩放表格的问题,如果是,您可以使用此类:

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

然后您可以编写一些代码来为表单设置动画,如下所示:

WinAPI.AnimateWindow (this.Handle, animationTime, flags);

如果您想查看使用此代码的简单演示,请从here下载此开源代码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多