Mastrius
Mastrius

Reputation: 23

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.

Upvotes: 2

Views: 1153

Answers (2)

Corhal
Corhal

Reputation: 23

I had the same problem, only the resolution of the second monitor is smaller than the first.
The only solution I have found is through using WinAPI:

    [DllImport("user32.dll", SetLastError = true)]
    protected static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    protected static readonly IntPtr HWND_TOP = new IntPtr(0);
    protected const UInt32 SWP_NOMOVE = 0x0002;
    
    private void FormTest_Resize(object sender, EventArgs e)
    {
        if (WindowState == FormWindowState.Maximized)
        {
            Size size = Screen.FromControl(this).WorkingArea.Size;
    
            if (!Size.Equals(size))
            {
                SetWindowPos(Handle, HWND_TOP, 0, 0, size.Width, size.Height, SWP_NOMOVE);
            }
        }
        
    }

Upvotes: 1

Xavi Anguera
Xavi Anguera

Reputation: 105

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)?

Upvotes: 0

Related Questions