在无边框形式上使用 Windows 动画 [英] Use windows animations on borderless form

查看:30
本文介绍了在无边框形式上使用 Windows 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我(主要是出于好奇)做了一个无边界的表格.制作我自己的标题栏,包括标题和三个按钮(最小化、最大化和关闭),就像每个普通的 Windows 程序一样.我还为这些按钮制作了代码(直接问你是否想看代码).

Recently I made (mostly out of curiosity) a borderless form. After making my own title bar which includes the title, and the three buttons(minimize, maximize and close), just like every normal Windows program. I also made the code for these buttons (just ask if you want to see the code).

但是,我注意到没有动画.我的意思是,例如如果我点击最小化按钮,没有动画,程序立即消失(它没有关闭,按钮工作,但没有动画).在所有情况下都会发生这种情况:当我打开程序时它会立即出现,当我关闭它时它会立即消失.

However, I've noticed that there are no animations. What I mean is that, e.g. if I click the minimize button, there is no animation, the program instantly disappears (it doesn't close, the button works, but without an animation). This happens in all cases: When I open the program it instantly appears, when I close it, it instantly disappears.

是否有某种方法可以使用标准 Windows 程序使用的这些动画?

Is there some sort of way to use these animations that standard Windows programs use?

推荐答案

在无边框表单上似乎不可能有动画效果.但是,有两种可能的解决方法.

It doesn't seem possible to have the animation effect on a borderless form. However, there are two possible workarounds.

  1. 在最小化或还原之前将 FormBorderStyle 设置回 Sizable,然后再设置回无.

  1. Set the FormBorderStyle back to Sizable just before a Minimize or Restore, and then back to none aftewards.

改用 AnimateWindow 函数.动画往往发生在窗口当前所在的位置.这些函数可以应用于任何Control,而不仅仅是顶级窗口.

Use the AnimateWindow function instead. The animations tend to happen where the window is currently located. The functions can be applied to any Control, not just top level windows.

这是一些示例代码:

    class FormA : Form {

        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_MINIMIZE = 0xF020;
        private const int SC_RESTORE = 0xF120; 
        protected override void WndProc(ref Message m) {
            switch (m.Msg) {
                case WM_SYSCOMMAND:
                    int command = m.WParam.ToInt32();
                    if (command == SC_RESTORE) {
                        this.FormBorderStyle = FormBorderStyle.Sizable;
                        this.ControlBox = true;
                    }
                break;
            }
            base.WndProc(ref m);
        }
    }

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

private const int AW_VER_POSITIVE = 0x00000004;
private const int AW_VER_NEGATIVE = 0x00000008;
private const int AW_SLIDE =        0x00040000;
private const int AW_HIDE = 0x00010000;


            [STAThread]
            static void Main() {
                Application.EnableVisualStyles();
                Form f = new FormA();
                f.ControlBox = false;
                f.FormBorderStyle = FormBorderStyle.None;

                bool isMinimizing = false;
                var mb = new Button { Text = "Min" };
                mb.Click += delegate {
                    isMinimizing = true;
                    f.FormBorderStyle = FormBorderStyle.Sizable;
                    f.ControlBox = true;
                    f.WindowState = FormWindowState.Minimized;
                    f.FormBorderStyle = FormBorderStyle.None;
                    isMinimizing = false;
                    //AnimateWindow(f.Handle, 300, AW_SLIDE | AW_VER_POSITIVE | AW_HIDE);

                };
                f.SizeChanged += delegate {
                    if (isMinimizing)
                        return;
                    if (f.WindowState != FormWindowState.Minimized)
                        f.FormBorderStyle = FormBorderStyle.None;
                };

                f.Controls.Add(mb);
                Application.Run(f);
        }

这篇关于在无边框形式上使用 Windows 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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