使用C# FormWindowState来恢复?

发布于 2024-08-17 03:56:15 字数 361 浏览 12 评论 0原文

我想检测我的应用程序在某些情况下是否最小化,如果是,则需要恢复窗口。我可以轻松地做到这一点,如下所示:

if(this.WindowState == FormWindowState.Minimized) {
    this.WindowState = FormWindowState.Normal;
}

但是,如果用户首先最大化表单,然后最小化它,会发生什么?我不知道是将 WindowState 设置为 FormWindowState.Normal 还是 FormWindowState.Maximized。我可以检查是否有方法或 API 调用来解决此问题?

I'd like to detect if my application is minimized under certain situations, and if it is, the window needs to be restored. I can do that easily as follows:

if(this.WindowState == FormWindowState.Minimized) {
    this.WindowState = FormWindowState.Normal;
}

However, what happens if the user first maximizes the form, then minimizes it? I don't know whether to set the WindowState to FormWindowState.Normal or FormWindowState.Maximized. Is there a method or API call I can check to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

辞旧 2024-08-24 03:56:15

下面显示的代码可以满足您的需要。顺便说一句,覆盖用户的选择是非常不明智的。

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        mLastState = this.WindowState;
    }
    FormWindowState mLastState;
    protected override void OnResize(EventArgs e) {
        base.OnResize(e);
        if (mLastState != this.WindowState) {
            if (this.WindowState == FormWindowState.Minimized) this.WindowState = mLastState;
            else mLastState = this.WindowState;
        }
    }
}

The code shown below does what you need. Overriding the user's choice is pretty unwise btw.

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        mLastState = this.WindowState;
    }
    FormWindowState mLastState;
    protected override void OnResize(EventArgs e) {
        base.OnResize(e);
        if (mLastState != this.WindowState) {
            if (this.WindowState == FormWindowState.Minimized) this.WindowState = mLastState;
            else mLastState = this.WindowState;
        }
    }
}
如果没结果 2024-08-24 03:56:15

我使用此解决方案以 MDI 形式恢复表单。首先,您必须定义:

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

private const int SW_RESTORE = 9;

当涉及到恢复时:

ShowWindowAsync(this.MdiChildren[i].Handle, this.SW_RESTORE);

这会将表单恢复到之前的状态,而无需使用其他状态持有者。
另外,您可能会发现这篇文章很有趣

I use this solution to restore forms in MDI form. First you have to define:

[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

private const int SW_RESTORE = 9;

and when it comes to restore:

ShowWindowAsync(this.MdiChildren[i].Handle, this.SW_RESTORE);

This will restore form to the previous state without using additional state holders.
Also you may find this article interesting

请别遗忘我 2024-08-24 03:56:15

我认为您应该能够调用 this.Show() 并且它将恢复到之前的(可见)状态。

I think you should be able to call this.Show() and it will restore to the previous (visible) state.

蝶舞 2024-08-24 03:56:15

这是一种利用表单的 OnResize 方法 的方法

Here's an approach that utilizes the OnResize method of the form

紫竹語嫣☆ 2024-08-24 03:56:15

https://stackoverflow.com/a/6837421/578731

不确定这对每个人都有效,但我今天遇到了这个问题,有人团队建议“您尝试过正常”吗?

事实证明他是对的。以下似乎可以很好地恢复您的窗口:

if (myWindow.WindowState == WindowState.Minimized)
    myWindow.WindowState = WindowState.Normal;

效果很好,如果需要的话可以将窗口恢复到最大化。首先检查最小化状态似乎至关重要,因为第二次调用 WindowState.Normal 会将窗口“恢复”到非最大化状态。

希望这有帮助。

https://stackoverflow.com/a/6837421/578731:

Not sure this will work for everybody, but I ran into this today and someone on the team suggested "have you tried Normal"?

Turns out he was right. The following seems to nicely restore your window:

if (myWindow.WindowState == WindowState.Minimized)
    myWindow.WindowState = WindowState.Normal;

That works just fine, restoring the window to Maximized if needed. It seems critical to check for the minimized state first as calling WindowState.Normal a second time will "restore" your window to its non-maximized state.

Hope this helps.

猫九 2024-08-24 03:56:15

我认为这是最好的选择(微软说):

this.WindowState = System.Windows.Forms.FormWindowState.Normal;

I think this is the best option (Microsoft said):

this.WindowState = System.Windows.Forms.FormWindowState.Normal;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文