Search code examples
c#windowfullscreen

C#: How to make window go really fullscreen


Hello I would like to ask how to make window in C# application truly go fullscren and whether it is even possible ? I don't also mean the set maximized and undecorated approach but rather true fullscreen like in java by setting window to fullscreen( I mean so that I can switch resolution of screen in app and if I alt-tab out of the app it switches back to normal resolution[There should be winapi way of doing it at least since it is possible in java])


Solution

  • Simple, in Windows forms you need to play with FormBorderStyle and WindowState
    here is an example:

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 'F')
            {
                if (FormBorderStyle == FormBorderStyle.None)
                {
                    FormBorderStyle = FormBorderStyle.Sizable;
                    WindowState = FormWindowState.Normal;
                }
                else
                {
                    FormBorderStyle = FormBorderStyle.None;
                    WindowState = FormWindowState.Maximized;
                }
            }
        }
    

    That will alternate you from FullScreen and normal mode.

    And if you want to go back to normal mode when user alt-tab out of the app add code to the deactivate event of the form:

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            if (FormBorderStyle == FormBorderStyle.None)
            {
                FormBorderStyle = FormBorderStyle.Sizable;
                WindowState = FormWindowState.Normal;
            }
        }