Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在表单加载时使用AnimateWindow()_C#_Winforms_Borderless_Animatewindow - Fatal编程技术网

C# 在表单加载时使用AnimateWindow()

C# 在表单加载时使用AnimateWindow(),c#,winforms,borderless,animatewindow,C#,Winforms,Borderless,Animatewindow,我有一个无边界表单,我使用AnimateWindow()方法创建打开、关闭表单等的动画。我使用以下代码: [Flags] enum AnimateWindowFlags { AW_HOR_POSITIVE = 0x0000000 AW_HOR_NEGATIVE = 0x00000002, AW_VER_POSITIVE = 0x00000004, AW_VER_NEGATIVE = 0x00000008, AW_CENTER = 0x00000010,

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

[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);
private void Form1_Load(object sender, EventArgs e)
{
    AnimateWindow(this.Handle, 100, AnimateWindowFlags.AW_BLEND);
}
在关闭表单时,此代码似乎有效:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    AnimateWindow(this.Handle, 100, AnimateWindowFlags.AW_BLEND | AnimateWindowFlags.AW_HIDE);
}
但是,使用此代码打开表单时:

[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);
private void Form1_Load(object sender, EventArgs e)
{
    AnimateWindow(this.Handle, 100, AnimateWindowFlags.AW_BLEND);
}
似乎什么也没发生。在做了一些猜测和测试之后,我发现使用AnimateWindow()方法使表单淡入是可行的,但是使用它使表单淡入没有任何作用(不管
时间
参数)


有什么想法吗?

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

必须在调用Show()方法和Winforms有机会pinvoke ShowWindow()之间插入AnimateWindow()调用。在OnLoad()中尝试后一个调用会破坏动画效果,该事件激发得太晚

您可以尝试此代码,将其粘贴到表单类中:

    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子窗体上尝试它,这样可能不会有好的结果。

它可能在加载事件中无法正常工作,因为此时窗体可能还没有句柄。在展示的活动中尝试一下,看看它是否有效。嗯,尝试一下,似乎什么都没做。然而,我尝试使用AnimateWindow()隐藏(淡出)我的表单,结果成功了。我想我用了一种错误的方法,但根据这个方法,它应该是有效的…+1的努力,但它不工作的项目,我的工作。奇怪的是,当我做了一个新项目(只是一个空的无边界表单)时,我的和你的方式都起了作用!然后我添加了一些组件,比如几个按钮和一个选项卡控件(因为我的程序实际上有一些这样的控件),它仍然可以工作,所以我的项目中可能有一些东西。我会看一看,然后让你知道。好吧,我刚刚发现谁犯了这一切:
this.MaximizedBounds=Screen.FromControl(this.workingrea)。我使用它是为了使主窗体(无边框)完全适合屏幕(这样它就不会覆盖任务栏,并防止某些部分超出屏幕)。简单的移除似乎可以解决问题……当然,正是这种声明导致了我所警告的麻烦。任何依赖于窗体大小或位置的代码,或隐式使用诸如Screen.FromControl()之类的句柄属性的代码,都应该移动到加载事件处理程序中。通过在AnimateWindow()调用上设置断点,您可以在调试器中看到一些东西,但您永远不希望在调用堆栈跟踪中看到窗体的构造函数调用。