开发者

Expand form to all screens

开发者 https://www.devze.com 2023-01-17 19:05 出处:网络
The question is simple: is there a way to expand a single form to all connected screens开发者_如何学运维? No, unless you are going to codein a reasonably low-level language, such as C and grab access

The question is simple: is there a way to expand a single form to all connected screens开发者_如何学运维?


No, unless you are going to code in a reasonably low-level language, such as C and grab access to graphics memory.

Generally it's the operating system that performs the layout for you, so unless you have low-level access, or API published by the vendor, it would be quite hard I guess.


Yes, all of the screens make up a giant virtual surface upon which you can place your form. You need to merely set the form's location to the top-left of this surface and its size to the size of this surface.

You can find the extent of this surface with the following code:

private static Rectangle GetVirtualDisplayBounds()
{
    Screen[] allScreens = Screen.AllScreens;

    Point topLeft = allScreens[0].Bounds.Location;
    Point bottomRight = topLeft + allScreens[0].Bounds.Size;

    foreach (Screen screen in allScreens.Skip(1))
    {
        topLeft = new Point(Math.Min(topLeft.X, screen.Bounds.X),
                            Math.Min(topLeft.Y, screen.Bounds.Y));

        bottomRight = new Point(Math.Max(bottomRight.X, screen.Bounds.Right),
                                Math.Max(bottomRight.Y, screen.Bounds.Bottom));
    }

    return new Rectangle(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y);
}

Then you can size your form appropriately:

var bounds = GetVirtualDisplayBounds();
form.Location = bounds.Location;
form.Size = bounds.Size;

You may also want to disable form borders:

form.FormBorderStyle = FormBorderStyle.None;

I have noticed though, that when you show your form it will pop back to a location of 0,0, which means that if you have any monitors positioned above or to the left of this point they will not be covered. To solve this you need to set the location after the form is shown.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号