简体   繁体   English

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

[英]C# Restore a form property value that has been changed

I have a main form. 我有一个主要形式。 Using the KeyDown event I've been able to make the form go Full Screen at the user's request (by pressing CTRL + ALT + ENTER). 使用KeyDown事件,我已经能够根据用户的要求(通过按CTRL + ALT + ENTER)使表单进入“全屏”状态。

So, I need: 1) when the form is not at "Full Screen mode" and the user presses CTRL + ALT + ENTER, as expected, the form goes Full Screen, and 2) when the form is already at "Full Screen mode" and user presses CTRL + ALT + ENTER, the form should go back the way it was before. 因此,我需要:1)当表单不在“全屏模式”下并且用户按预期按CTRL + ALT + ENTER时,表单进入“全屏”状态;以及2)当表单已经在“全屏模式”下”,然后用户按CTRL + ALT + ENTER,该表格应返回之前的状态。

Turning the form to FullScreen is done. 将表格转到FullScreen。 The problem is, now I have to determine whether the properties of size, location, and form border style (any of them) where changed, and then restore them to whatever values they had before I pressed the keys, so I can undone those property changes. 问题是,现在我必须确定大小,位置和表单边框样式(其中的任何一个)的属性是否已更改,然后将其恢复为按下键之前的值,以便可以撤消这些属性。变化。

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

How could this be achieved? 如何做到这一点? Is there a class / method I could use? 我可以使用一个类/方法吗? (I think PropertyChanged might be the one, but I can't still find how to restore the properties I want) Thank you. (我认为PropertyChanged可能是其中之一,但是我仍然找不到如何还原我想要的属性的方法)谢谢。

PS: If you're wondering why I didn't just set the window as "maximized" and the form border as none, and be done with it, turns out the professor just doesn't like that solution and want us to 'fullscreen' it for real and not 'quickly make it look like it'. PS:如果您想知道为什么我不只是将窗口设置为“最大化”,而将窗体边框设置为“没有”,然后将其完成,结果是教授不喜欢这种解决方案,而是希望我们“全屏显示”它是真实的,而不是“迅速使它看起来像它”。 The only thing I'm having trouble with is to make the form go back to the previous state before it was 'fullscreened'. 我唯一遇到的麻烦是使表单在“全屏显示”之前返回到之前的状态。

You can achieve this without using the Size parameters. 您无需使用Size参数即可实现此目的。 The only thing you are doing to make it truly full screen is removing the form border. 要使其真正全屏显示,唯一要做的就是删除表格边框。

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

If you still want to maintain the state information, you could save them to member variables and use their values, when you want to reverse them. 如果您仍然想维护状态信息,则可以将它们保存到成员变量中,并在想要反转它们时使用它们的值。

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

Please check for nulls or variables not being assigned. 请检查是否有空值或未分配的变量。

First set the form to normal mode and design its dimensions. 首先将表单设置为normal模式并设计其尺寸。 Latter place a button to maximize the form.On clicking button to maximize check the property of the form whether maximise/normal and change the size mode of the form. 最近放置一个按钮以最大化表单。单击按钮以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 唯一要记住的是,如果焦点不在窗体上,您的快捷键将触发系统快捷键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM