简体   繁体   中英

Maximizing form on secondary screen with different resolution?

I am developping a simple windows application (in C#) and I want it to display a form that is maximized in my second monitor. To do so, I am doing the following on the "Load" event of the form:

private void FormTest_Load(object sender, EventArgs e)
    {
        Screen[] screens = Screen.AllScreens;
        this.WindowState = FormWindowState.Normal;
        this.Location = screens[1].WorkingArea.Location;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;                    
    }

The problem I have is that when I execute this, the form only takes part of the screen. My primary screen has a resolution of 1024x768 and my secondary screen has a resolution of 1920x1080, and it seems that the form is taking the size of the primary screen in my secondary screen.

Also, I have a button that runs the following code to maximize the screen or turn it back to normal:

private void ChangeSize() {
    if (this.WindowState == System.Windows.Forms.FormWindowState.Maximized)
    {
        this.WindowState = System.Windows.Forms.FormWindowState.Normal;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
    }
    else
    {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
    }
}

When I click on the button twice (first to de-maximize the form, and then to maximize it again) the form does cover the entire secondary screen perfectly, but if I try to just run that function in code twice (for the sake of testing) right after the code in "FormTest_Load", the screen will still not cover correctly the entire screen.

I am probably making a noob mistake here but I have struggled with this for some time, so I would really appreciate if anyone can shed some light on what is wrong with my code.

I've tried to get the same result as you describe (changing resolutions, etc), but all works fine in my computer and screens. May be... what is exactly the variable screens ? I've assumed that is Screen.AllScreens , like this:

    protected override void OnLoad(EventArgs e)
    {
        Screen[] screens = Screen.AllScreens;
        int screenNumber = 1;

        this.WindowState = FormWindowState.Normal;
        this.Location = screens[screenNumber].WorkingArea.Location;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
    }

As I said, it works fine for me. Also, you can check the screen settings of your computer: is the size of text and apps set to 100% (recommended value) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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