【问题标题】:The animation on window closing does not appear after SetWindowRgnSetWindowRgn 后不出现关闭窗口的动画
【发布时间】:2014-12-22 22:20:46
【问题描述】:

我想显示一个带有自定义区域和自定义外观的窗口。我挂钩一个窗口进程并通过 SetWindowRgn 函数在 WM_SIZE 消息上设置自定义区域。我还处理了一个 WM_NCCALCSIZE 以删除标准窗框。

    [SecuritySafeCritical]
    protected virtual IntPtr HwndSourceHookHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
        if(msg == WinApi.WM_NCCALCSIZE) {

            handled = true;
            return IntPtr.Zero;

        }
        if(msg == WinApi.WM_SIZE) {
            if(region != IntPtr.Zero)
                WinApi.DeleteObject(region);
            int width = lParam.ToInt32() & 0xFFFF;
            int height = lParam.ToInt32() >> 16;
            region = WinApi.CreateRoundRectRgn(0, 0, width, height, 40, 40);
            WinApi.SetWindowRgn(new WindowInteropHelper(this).Handle, region, true);
        }

        return IntPtr.Zero;
    }

该窗口出现在我的 Windows 8.1 操作系统上,具有预期的圆角,并且没有边框和窗口按钮。但我注意到在这种情况下,窗口会立即关闭而不执行关闭动画。如果我评论 SetWindowRgn,一切正常。有谁知道我做错了什么?

【问题讨论】:

    标签: wpf windows winapi


    【解决方案1】:

    动画正在Window 中执行。因此,您需要在按下关闭Button 时停止Window 实际关闭,然后开始动画。当动画完成后,那么你应该关闭Window。这可以通过以下方式实现:

    处理Window.ClosingWindow.Close 事件:

    private void AnimationWindow_Closing(object sender, CancelEventArgs e)
    {
        if (!isCloseAnimationComplete)
        {
            e.Cancel = true;
            CloseWindow();
        }
    }
    
    private void AnimationWindow_Close(object sender, RoutedEventArgs e)
    {
        CloseWindow();
    }
    
    public void CloseWindow()
    {
        // Set up animation
        animation.Completed += CloseAnimation_Completed;
        // Start animation
    }
    
    private void CloseAnimation_Completed(object sender, EventArgs e)
    {
        isCloseAnimationComplete = true;
        // Actually close Window here
        Close();
    }
    

    【讨论】:

    • 感谢您的回答!但我需要执行标准的关闭动画,因为它是在没有调用 SetWindowRgn 的情况下执行的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多