在Form_Load中使用AnimateWindow() [英] Using AnimateWindow() at Form_Load

查看:61
本文介绍了在Form_Load中使用AnimateWindow()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无边界的表单,并且我使用AnimateWindow()方法来创建用于打开,关闭等表单的动画.我使用以下代码:

I have a borderless form and I use the AnimateWindow() method to create animations for opening, closing, etc my Form. I use this code:

[Flags]
enum AnimateWindowFlags
{
    AW_HOR_POSITIVE = 0x0000000
    AW_HOR_NEGATIVE = 0x00000002,
    AW_VER_POSITIVE = 0x00000004,
    AW_VER_NEGATIVE = 0x00000008,
    AW_CENTER = 0x00000010,
    AW_HIDE = 0x00010000,
    AW_ACTIVATE = 0x00020000,
    AW_SLIDE = 0x00040000,
    AW_BLEND = 0x00080000
}

[DllImport("user32.dll")]
static  extern bool AnimateWindow(IntPtr hWnd, int time, AnimateWindowFlags flags);

在关闭表单时,此代码似乎有效:

When it comes to closing the form, this code seems to work:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    AnimateWindow(this.Handle, 100, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);
}

但是,使用以下代码打开表单时:

However, when opening the form with this code:

private void Form1_Load(object sender, EventArgs e)
{
    AnimateWindow(this.Handle, 100, AnimateWindowFlags.AW_BLEND);
}

似乎什么都没发生.经过一些猜测和测试后,我发现使用AnimateWindow()方法使表格淡出有效,但是使用它使表格 fade in 无效任何东西(与 time 参数无关).

Nothing seems to happen. After doing some guesses and tests I figured that using the AnimateWindow() method to make the form fade out works, but using it to make the form fade in doesn't do anything (regardless of the time parameter).

有什么想法吗?

推荐答案

这很难正确完成,其中涉及大量代码,因此很难进行推理.在创建自己的窗体时,由Application类为启动窗体和Show()方法设置的Visible属性在Winforms中非常重要.在典型的.NET方式下,本机窗口的创建是懒惰的,当球滚动时,会发生很多事情.

This is pretty difficult to do correctly, there is an enormous amount of code involved that is very tricky to reason through. The Visible property, set by the Application class for the startup form and the Show() method when you create your own is a very big deal in Winforms. The native window creation is lazy in typical .NET fashion, lots and lots of stuff happens when the ball gets rolling.

必须在调用Show()方法和Winforms可以调用ShowWindow()的时间之间注入AnimateWindow()调用.当您在OnLoad()中尝试动画效果时,后一个调用会破坏动画效果,事件触发得太晚了.

The AnimateWindow() call must be injected in between the time the Show() method is called and Winforms gets a chance to pinvoke ShowWindow(). It is the latter call that ruins the animation effect when you try it in OnLoad(), the event fires too late.

您可以尝试以下代码,将其粘贴到Form类中:

You can try this code, paste it into your Form class:

    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            NativeMethods.AnimateWindow(this.Handle, 100, AnimateWindowFlags.AW_BLEND);
        }
        base.SetVisibleCore(value);
    }

    protected override void OnShown(EventArgs e) {
        this.BringToFront();
        base.OnShown(e);
    }

但是我不能保证它会在所有可能的情况下工作,并且尚未对其进行广泛的测试.必须调用BringToFront()已经是不愉快的事情.不要在MDI子窗体上尝试,因为这样做不太可能成功.

But I cannot promise it will work in all possible cases and have not tested it extensively. Having to call BringToFront() was already an unpleasant hack. Don't try it on an MDI child form, not likely to come to a good end.

这篇关于在Form_Load中使用AnimateWindow()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆