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

时间:2015-04-03 05:10:39

标签: c# winforms properties propertychanged

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

所以,我需要:1)当表格处于"全屏模式时#34;并且用户按下CTRL + ALT + ENTER,按预期,表格进入全屏,2)当表格已经处于"全屏模式"并且用户按下CTRL + ALT + ENTER,表单应该以之前的方式返回。

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

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*/
        }    
    }
}

如何实现这一目标?有没有我可以使用的类/方法? (我认为PropertyChanged可能是那个,但我仍然无法找到如何恢复我想要的属性)谢谢。

P.S:如果你想知道我为什么不将窗口设置为"最大化"并且表格边框为无,并且完成后,原来教授不喜欢这个解决方案,并希望我们全屏#39;它是真实的,而不是快速使它看起来像它#。 我唯一遇到的麻烦就是让表单在完全筛选之前恢复到之前的状态。

2 个答案:

答案 0 :(得分:1)

您可以在不使用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;
    }
}

请检查空值或未分配的变量。

答案 1 :(得分:0)

首先将表单设置为normal模式并设计其尺寸。 后来放置一个按钮以最大化表单。单击按钮maximize检查表单的属性是否maximise/normal并更改表单的大小模式。

  

唯一要记住的是,如果焦点不在你的形式上   快捷键将触发系统快捷键