gpt4 book ai didi

c# - 在无边框窗体上使用 Windows 动画

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:04 25 4
gpt4 key购买 nike

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

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

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

最佳答案

似乎不可能在无边框窗体上产生动画效果。但是,有两种可能的解决方法。

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

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

下面是一些示例代码:

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

关于c# - 在无边框窗体上使用 Windows 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31473489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com