C# 恢复已更改的表单 属性 值

C# Restore a form property value that has been changed

我有一个主窗体。使用 KeyDown 事件,我已经能够根据用户的请求使表单全屏显示(通过按 CTRL + ALT + ENTER)。

因此,我需要:1) 当表单不在 "Full Screen mode" 并且用户按下 CTRL + ALT + ENTER 时,正如预期的那样,表单进入全屏,以及 2) 当表单已经在 "Full Screen mode" 并且用户按下 CTRL + ALT + ENTER 时,表单应该返回到之前的状态。

将表单转为全屏已完成。问题是,现在我必须确定大小、位置和窗体边框样式(其中任何一个)的属性是否发生了变化,然后将它们恢复到我按下这些键之前的任何值,这样我就可以撤消那些 属性 变化。

private bool IsFullScreen() //Is form at "FullScreen Mode"?
{
    return (this.Height == Screen.PrimaryScreen.Bounds.Height 
    && this.Width == Screen.PrimaryScreen.Bounds.Width &&                         
    this.FormBorderStyle == FormBorderStyle.None);
}

private void FullScreen(Object sender, KeyEventArgs e)
{
    if (e.Alt & e.Control & e.KeyCode == Keys.Enter) 
    {
        if (!IsFullScreen()) //Form is resized to FullScreen, only if it's not already 'fullscreened'
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.Location = new Point(0, 0);
            this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        }
        else
        {
            /*Form goes back to whatever size, location, and form border style 
            it had before I pressed CTRL + ALT + ENTER*/
        }    
    }
}

这是如何实现的?我可以使用 class / 方法吗? (我认为 PropertyChanged 可能是那个,但我仍然找不到如何恢复我想要的属性)谢谢。

P.S:如果你想知道为什么我不直接将 window 设置为 "maximized" 并将表单边框设置为 none,然后完成事实证明教授只是不喜欢那个解决方案,并希望我们 'fullscreen' 它是真实的而不是 'quickly make it look like it'。 我唯一遇到的麻烦是让表单回到 'fullscreened' 之前的状态。

您可以在不使用 Size 参数的情况下实现此目的。要使其 真正 全屏,您唯一要做的就是删除表单边框。

if (!IsFullScreen()) //Form is resized to FullScreen, only if it's not already 'fullscreened'
    {
        this.FormBorderStyle = FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
    }
    else
    {
        /*Form goes back to whatever size, location, and form border style 
        it had before I pressed CTRL + ALT + ENTER*/
        this.FormBorderStyle = FormBorderStyle.Single;
        this.WindowState = FormWindowState.Normal;
    }   

如果您仍然想维护状态信息,可以将它们保存到成员变量中并在您想要反转它们时使用它们的值。

    private Point previousLocation;
private Size previousSize; 
private void FullScreen()
{

    if (!IsFullScreen()) //Form is resized to FullScreen, only if it's not already 'fullscreened'
    {
        previousLocation = this.Location;
        previousSize = this.Size;
        this.FormBorderStyle = FormBorderStyle.None;
        this.Location = new Point(0, 0);
        this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    }
    else
    {
        /*Form goes back to whatever size, location, and form border style 
        it had before I pressed CTRL + ALT + ENTER*/
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.Location = previousLocation;
        this.Size = previousSize;
    }
}

请检查是否存在未赋值的空值或变量。

首先将表单设置为normal模式并设计其尺寸。 后面放置一个按钮最大化form.On点击按钮maximize检查表格的属性是否maximise/normal并改变表格的大小模式

The only thing to keep in mind is if the focus is not on the form your shortcut keys will trigger system shortcuts