Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

c# - Use windows animations on borderless form

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

  2. 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.

Here is some sample code:

    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);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...