Maximize form on both screens (dual screen monitor)

12,663

Solution 1

this may be late, but here is an easy way to do it. It sets the size of the form from getting the size of resolution. then it places the form so it can be visible.

        int screenLeft = SystemInformation.VirtualScreen.Left;
        int screenTop = SystemInformation.VirtualScreen.Top;
        int screenWidth = SystemInformation.VirtualScreen.Width;
        int screenHeight = SystemInformation.VirtualScreen.Height;            

        this.Size = new System.Drawing.Size(screenWidth, screenHeight);
        this.Location = new System.Drawing.Point(screenLeft, screenTop);

Solution 2

Combine the 2 screen sizes and set your form to that resolution. Something like:

int height = 0;
int width = 0;
foreach (screen in System.Windows.Forms.Screen.AllScreens)
{
  //take smallest height
  height = (screen.Bounds.Height <= height) ? screen.Bounds.Height: height;
  width += screen.Bounds.Width;
}

Form1.Size = new System.Drawing.Size(width, height);

To override the maximize button you can check via WndProc for the maximize event

const int WM_SYSCOMMAND= 0x0112;
const int SC_MAXIMIZE= 0xF030;
protected override void WndProc(ref Message m)
{
    if(m.Msg==WM_SYSCOMMAND)
    {
        if((int)m.WParam==SC_MAXIMIZE)
        {
            MessageBox.Show("Maximized!!");
            return;
        }
    }
    base.WndProc (ref m);
}

or register to the Resize event of the form (you should check if it's resize or an maximize) (MSDN)

Solution 3

I know this thread is very old - but I found a very useful and working solution from here after trying and having problems with every solution listed here.

What worked for me was

Rectangle r = new Rectangle();
        foreach (Screen s in Screen.AllScreens)
        {
            if (s != Screen.FromControl(this)) // Blackout only the secondary screens
                r = Rectangle.Union(r, s.Bounds);
        }

        this.Top = r.Top;
        this.Left = r.Left;
        this.Width = r.Width;
        this.Height = r.Height;
Share:
12,663
jodie
Author by

jodie

Updated on June 13, 2022

Comments

  • jodie
    jodie about 2 years

    Iam looking for some hint or solution regarding following problem.

    I have a .NET 2.0 WinForm dialog which operates in Dual Screen environment. The Working area is set by .NET Framework to reflect the Primary screen. I want to MAXIMIZE Form to BOTH screens but after clicking the "maximize button", dialog is maximized only to "active" screen (active I mean screen on which dialog is currently placed).

    Iam not interested in bounds solution, this works, but when clicked Maximize button it forces the dialog back to one of 2 screens.

    I would be gratefull for any help or hints.

  • jodie
    jodie almost 14 years
    The USER have requested such feature! That's why Iam seeking solution for it!
  • jodie
    jodie almost 14 years
    Iam looking for universal solution which is not card specific... Some applications (closed source) work as I describe, but I cant find principle how they do it...
  • jodie
    jodie almost 14 years
    yep ... just what I was going to say... My scenario goes like this: 1. User clicks MAXIMIZE 2. Form is maximized to BOTH screens Please observe that MAXIMIZE operations causes several things except changing size: 1) FIXED position (cant move) 2) FIXED size (cant resize) 3) Maximize button responds to "rollback to last size" action As I can simulate 1) and 2) I don't know how to simulate 3) (Maximize button is allowing to revert to last size) etc..
  • Will Dean
    Will Dean almost 14 years
    The problem with this sort of advice is that you just don't have enough context to know if it's appropriate. There are lots of situations where application authors need lots of control over complete systems - they're not the desktop/office applications you seem to be thinking of, but things like kiosks and industrial control.
  • Lemon
    Lemon almost 14 years
    Also, as far as I can see, it looks wrong. If you have two screens that are 640x480, you will end up with a form with the size 1280x960. Should have been 1280x480 or 640x960 depending on how the screens are set up.
  • jodie
    jodie almost 14 years
    I assume that primary screen is smaller or equal so maximize show smaller window on the bigger (second screen). For now I assume that user have two screens with same res
  • Tim Robinson
    Tim Robinson almost 14 years
    There isn't a universal solution. Windows itself maximizes windows to fill one monitor. Card vendors achieve this by making two physical monitors appear as one big one.
  • jodie
    jodie almost 14 years
    I assume for now that they are equal size. Iam considering to run your solution, but It would be great If I know how to force MAXIMIZE button to display other glyph
  • CoreModule
    CoreModule almost 14 years
    fixed height (width is only interesting). If you have two screen with 2 different resolutions then maximizing over 2 screens is going to be a problem.
  • TomTom
    TomTom almost 14 years
    The problem is tha this is not even simple to answer IF needed - which is also ONE reason MS did not include it. Two screens? CAN you 100% assume both are "side by side" and have the same size / resolution? If not - things get REALLY ugly. Industrial Control can work with EITHER using ATI Eyefinity / Nvidia equivalent to make multiple screens appear as one for the OS (so the problem does not exist) or the user making the window large manually (which is what my current main contractor does on a 4 LARGE screens control wall).
  • Gaeburider
    Gaeburider over 10 years
    great code, just tested the first one. you should initialize the height with a value bigger than 0 (as example the maximum value of integer). The problem is that height is 0 and no screen will have a lower height. So height will still be 0
  • V-SHY
    V-SHY over 9 years
    I have error "An object reference is required for the nonstatic field..." when I tried to set the Form1.Size.
  • V-SHY
    V-SHY over 9 years
    cool man, I successfully maximized my windows in dual monitors by putting your code in starting part of Form1_Load(),thanks
  • Hekkaryk
    Hekkaryk over 5 years
    I don't know what if (s != Screen.FromControl(this)) is for (setting form to cover all monitors works both with and without this for me), but thank you Sir! You just helped me adding dual-screen different-resolution area capture to my own screenshot saver :)