c# - 如何用动画改变形式?

标签 c# winforms animation

如何在 win 应用程序中使用动画(如淡入淡出、Cutl Up...)改变形式?

我有两个表单 Form1,Form2。 我在 form1 中,上面有一个按钮,我想在单击按钮时更改表单。

//button click
Form2 f2=new Form2();
this.hide();
f2.show();

最佳答案

您可以使用 AnimateWindow() , 定义一个新的表单类并覆盖 OnLoad() 以显示具有您想要的效果的表单。

// Console Application
// Load System.Windows.Forms reference.

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);

class SlidingForm: System.Windows.Forms.Form 
{
    const int AW_ACTIVATE = 0X20000;
    const int AW_SLIDE = 0X40000;
    const int AW_HOR_POSITIVE = 0X1;

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

void Main()
{
    var frm = new SlidingForm().ShowDialog;
}

根据要求隐藏 PictureBox 控件的示例代码:

private void button1_Click(object sender, EventArgs e)
{
   const int AW_HIDE = 0x00010000;
   const int AW_SLIDE = 0x00040000;
   const int AW_HOR_NEGATIVE = 0x00000002;
   const int AW_HOR_POSITIVE = 0x00000001;
   // Toggle show/hide
   AnimateWindow(pictureBox1.Handle, 1000, (pictureBox1.Visible) ? (AW_HIDE | AW_HOR_NEGATIVE) : (AW_HOR_POSITIVE) | AW_SLIDE);
   pictureBox1.Visible ^= true;
}

关于c# - 如何用动画改变形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8079581/

相关文章:

c# - Quartz.net:FireAtTime 似乎没有按预期工作

c# - 在 Windows 10 中使用 CommonOpenFileDialog 选择文件夹但仍显示文件夹内的文件

c# - 为什么我会看到使用 native 代码的速度提高了约 20%?

javascript - 更改 jquery show()/hide() 动画?

javascript - 动画每个 ng-repeat 迭代

具有圆形或弧段的 CSS 剪辑/路径/蒙版/形状动画

c# - 是否有 Microsoft.Office.Interop.Excel 的替代库?

c# - ajax超时问题

c# - 如何在用户输入并显示错误消息时验证文本框值?

c# - AxWindowsMediaPlayer,播放 Midi 文件时未发生音量变化