使用.NET WinForms表单调用AnimateWindow的正确方法?

时间:2011-12-12 14:54:42

标签: winforms animatewindow

我正在尝试调用AnimateWindow来动画显示并隐藏WinForms窗口。

以下是the win32 translation的副本:

private static class NativeMethods
{
   public const int AW_ACTIVATE = 0x20000;
   public const int AW_HIDE = 0x10000;
   public const int AW_BLEND = 0x80000;
   public const int AW_CENTER = 0x00000010;
   public const int AW_SLIDE = 0X40000;
   public const int AW_HOR_POSITIVE = 0x1;
   public const int AW_HOR_NEGATIVE = 0X2;

   [DllImport("user32.dll", CharSet = CharSet.Auto)]
   public static extern int AnimateWindow(IntPtr hwand, int dwTime, int dwFlags);
}

但问题是如何调用AnimateWindow调用WinForms方案。 One person suggests OnLoad

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    AnimateWindow(this.Handle, 200, AW_ACTIVATE | AW_HOR_NEGATIVE | AW_SLIDE);
}

OnClosing

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    base.OnClosing(e);
    if (e.Cancel == false)
    {
        AnimateWindow(this.Handle, 200, AW_HIDE | AW_HOR_POSITIVE | AW_SLIDE);
    }
}

除非它不起作用。

  • 表格在出现时不使用任何动画
  • 在隐藏期间,表单会将其水平滑动动画从屏幕上移动,然后重新出现,然后隐藏正常方式

AnimateWindow与WinForms混合的正确方式是什么?


另见

  • .NET AnimateWindow:这家伙问了同样的问题。但由于它试图实现其他目标,人们解决了他的问题,而不是回答他的问题。
  • C# WinForms AnimateWindow issue:这家伙有兴趣将AnimateWindow与子控件一起使用,而不是顶级窗口。

Bonus Chatter

当我发现这个错误时,我perusing通过Form -> Show -> Visible -> SetVisibleCore

protected virtual void SetVisibleCore(bool value)
{
   try
   {
      HandleCollector.SuspendCollect();
      //...snip...
   }  
   finally
   {
      HandleCollector.ResumeCollect();
   }
}

很高兴知道每个人都可以介绍这些微妙的错误。

2 个答案:

答案 0 :(得分:2)

认为 AnimateWindow有正确的工作限制。例如,它与Aero不能很好地配合,因此要为滑动窗体设置动画,您需要将BorderStyle设置为无。另外,请确保StartPosition设置为手动。

简单示例:

public partial class Form1 : Form {

  public const int AW_ACTIVATE = 0x20000;
  public const int AW_HIDE = 0x10000;
  public const int AW_BLEND = 0x80000;
  public const int AW_CENTER = 0x00000010;
  public const int AW_SLIDE = 0X40000;
  public const int AW_HOR_POSITIVE = 0x1;
  public const int AW_HOR_NEGATIVE = 0X2;

  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern int AnimateWindow(IntPtr hwand, int dwTime, int dwFlags);

  public Form1() {
    InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e) {
    Form toastForm = new Form();
    toastForm.ShowInTaskbar = false;
    toastForm.StartPosition = FormStartPosition.Manual;
    toastForm.FormBorderStyle = FormBorderStyle.None;
    toastForm.Size = new Size(256, 64);
    toastForm.Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - toastForm.Width, 
                                   Screen.PrimaryScreen.WorkingArea.Bottom - toastForm.Height);

    Button closeButton = new Button();
    closeButton.Text = "Close";
    toastForm.Controls.Add(closeButton);
    closeButton.Click += delegate { toastForm.Close(); };

    AnimateWindow(toastForm.Handle, 200, AW_ACTIVATE | AW_HOR_NEGATIVE | AW_SLIDE);
    toastForm.Show();
  }
}

答案 1 :(得分:0)

我不确定您的AnimateWindow电话是做什么的,但是当您需要更改基本的本地电话时#'为了处理Windows窗体,我总是使用CreateParams()覆盖。你可以 为你想要达到的目标找到一个类似的功能。

这是一个透明工具窗口的示例,在显示时不会激活。

Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim baseParams As Windows.Forms.CreateParams = MyBase.CreateParams

            baseParams.ExStyle = baseParams.ExStyle Or NativeMethods.ExtendedWindowsStyles.WS_EX_LAYERED Or NativeMethods.ExtendedWindowsStyles.WS_EX_TRANSPARENT Or NativeMethods.ExtendedWindowsStyles.WS_EX_NOACTIVATE Or NativeMethods.ExtendedWindowsStyles.WS_EX_TOOLWINDOW

            Return baseParams
        End Get
    End Property