我正在尝试制作一个简单的侧边栏,它可以从桌面的左侧滑入和滑出。这是我目前的代码,但它实际上并不起作用,我担心它可能效率低下。
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();
}
}
希望有人能够帮助我。它应该相当简单。
答案 0 :(得分:0)
我认为你关于缩放形式的问题有褪色,如果是,你可以使用这个类:
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
下载此开源代码