-1

I have the below code which I use which includes hiding the taskbar by my form. It works well if there are two screens connected to my computer but if there is only one screen the taskbar shows, I am not sure why?

        FormBorderStyle = FormBorderStyle.None;
        await Task.Delay(500);
        this.WindowState = FormWindowState.Normal;
        this.ActiveControl = textBox1;
        StartPosition = FormStartPosition.Manual;
        Location = new Point(0, 0);
        var height = Screen.AllScreens.Max(x => x.WorkingArea.Height + x.WorkingArea.Y);
        var width = Screen.AllScreens.Max(x => x.WorkingArea.Width + x.WorkingArea.X);
        Size = new Size(width, height);
        this.BringToFront();
        this.AcceptButton = button1;
        this.ControlBox = false;
        this.TopMost = true;
        this.Size = Size;
        Rectangle ru = Rectangle.Union(Screen.AllScreens[0].Bounds , Screen.AllScreens[1].Bounds);
        Bounds = ru;
Tak
  • 3,536
  • 11
  • 51
  • 93
  • Not sure why you deleted your other question so soon? Have you tried `var rectUnions = Screen.AllScreens.Select(screen => screen.Bounds).Aggregate(Rectangle.Union);` – Rufus L Apr 06 '20 at 20:27
  • @RufusL Because I got 6 downvotes, not sure why :/ – Tak Apr 06 '20 at 20:28
  • downvotes usually mean the question needs clarification. if you improve the question, the downvotes are often removed. i think in that case it wasn't clear what was wrong with the code that was presented – Rufus L Apr 06 '20 at 20:53
  • See the [VirtualScreen](https://learn.microsoft.com/en-us/windows/win32/gdi/the-virtual-screen) feature and [SystemInformation.VirtualScreen](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.systeminformation.virtualscreen). Some notes about these [here](https://stackoverflow.com/a/53026765/7444103) (if you read this, don't skip the DpiAwareness part). – Jimi Apr 06 '20 at 22:24
  • @Jimi It's hard for me to understand this, is it possible that you explain to me what I am doing wrong? and why it works when multiple screens but not when only one screen is connected? – Tak Apr 08 '20 at 20:39
  • You don't need `Rectangle.Union()`. `SystemInformation.VirtualScreen` already reports this measure. See the notes I linked to understand how the screen layout composition works, so you won't be surprised if you get negative values. Then, you may have multiple screens even if just one Display (Monitor) is physically connected. Verify the `VirtualScreen` size and the `Screens` count. – Jimi Apr 08 '20 at 20:45
  • @Jimi If I remove this two lines `Rectangle ru = Rectangle.Union(Screen.AllScreens[0].Bounds , Screen.AllScreens[1].Bounds); Bounds = ru;` it will even not work when multiple screens. So I don't know why it works with multiple and not with single. – Tak Apr 08 '20 at 20:48
  • `SystemInformation.VirtualScreen` is already a Rectangle. Which includes the overall Size of all Screens. Have you checked this value? Note that Screens on the (virtual) right of the Primary Screen have positive coordinates values, while Screens on the (virtual) left of the Primary have negative coordinates values. – Jimi Apr 08 '20 at 20:51
  • @Jimi the value of `SystemInformation.VirtualScreen` is `{X = 0 Y = 0 Width = 1920 Height = 1080}` and I tried this `Bounds = SystemInformation.VirtualScreen;` but still the task bar is appearing. – Tak Apr 08 '20 at 23:19
  • If you have just ONE Screen and you set `this.FormBorderStyle = FormBorderStyle.None; this.TopMost = true; this.Bounds = SystemInformation.VirtualScreen;` in `Form.Load`. the borderless Form will fill the entire desktop. This, of course, if your Application is DpiAware. With more Displays attached, it depends: all Screens need to have the same Size. The current Screen (where the Form is first shown) size is prevalent. – Jimi Apr 08 '20 at 23:56
  • @Jimi Kindly find here the only code which worked for me on both a single screen and multiple screen...I really don't know why....I feel it can be much improved and written in a better way, so if you could please check it out? https://pastebin.com/Gv4CDmEr – Tak Apr 09 '20 at 17:47
  • @Jimi Did you get the chance to check my code? Thanks in advance. – Tak Apr 12 '20 at 16:03

1 Answers1

0

You're getting an "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" error because of AllScreens[1] when you only have one screen connected there is only one value in the AllScreen array so index[1] is out of bound.

Change;

Rectangle ru = Rectangle.Union(Screen.AllScreens[0].Bounds , Screen.AllScreens[1].Bounds);

To;

var ru = Screen.AllScreens.Select(a => a.Bounds).Aggregate(Rectangle.Union);

Now because you're using a Select you wil only getting AllScreens[0] instead of guessing what indexes well be there this code now also works is you have three monitors or any other configuration of monitors.

Edit

Also, as a small side note if you're trying to block a user from doing anything outside of your program. This will not block them fully actions like pressing the windows key will still bring up the taskbar or give control back to the user.