This is actually much more complicated than shown above. Firstly, the taskbar does not have to be on the main screen; you can drag it to any screen. On the other hand, in theory there may be something fixed on each edge of each given screen. The above code incorrectly assumes that finding something docked to one edge excludes all other edges.
The only way that the taskbar location could finally be obtained from the borders compared to the workspace would be if only one edge of all screens was attached to it.
The following function returns an array of rectangles, each of which represents a fixed taskbar, and writes the counter to its byref parameter. If this number is 1, element 0 of the returned array is a rectangle occupied by the taskbar. If greater than 1, time to guess?
Public Function FindDockedTaskBars(ByRef DockedRectCounter As Integer) As Rectangle() Dim TmpScrn As Screen = Nothing Dim LeftDockedWidth As Integer = 0 Dim TopDockedHeight As Integer = 0 Dim RightDockedWidth As Integer = 0 Dim BottomDockedHeight As Integer = 0 Dim DockedRects(Screen.AllScreens.Count * 4) As Rectangle DockedRectCounter = 0 For Each TmpScrn In Screen.AllScreens If Not TmpScrn.Bounds.Equals(TmpScrn.WorkingArea) Then LeftDockedWidth = Math.Abs(Math.Abs(TmpScrn.Bounds.Left) - Math.Abs(TmpScrn.WorkingArea.Left)) TopDockedHeight = Math.Abs(Math.Abs(TmpScrn.Bounds.Top) - Math.Abs(TmpScrn.WorkingArea.Top)) RightDockedWidth = (TmpScrn.Bounds.Width - LeftDockedWidth) - TmpScrn.WorkingArea.Width BottomDockedHeight = (TmpScrn.Bounds.Height - TopDockedHeight) - TmpScrn.WorkingArea.Height If LeftDockedWidth > 0 Then DockedRects(DockedRectCounter).X = TmpScrn.Bounds.Left DockedRects(DockedRectCounter).Y = TmpScrn.Bounds.Top DockedRects(DockedRectCounter).Width = LeftDockedWidth DockedRects(DockedRectCounter).Height = TmpScrn.Bounds.Height DockedRectCounter += 1 End If If RightDockedWidth > 0 Then DockedRects(DockedRectCounter).X = TmpScrn.WorkingArea.Right DockedRects(DockedRectCounter).Y = TmpScrn.Bounds.Top DockedRects(DockedRectCounter).Width = RightDockedWidth DockedRects(DockedRectCounter).Height = TmpScrn.Bounds.Height DockedRectCounter += 1 End If If TopDockedHeight > 0 Then DockedRects(DockedRectCounter).X = TmpScrn.WorkingArea.Left DockedRects(DockedRectCounter).Y = TmpScrn.Bounds.Top DockedRects(DockedRectCounter).Width = TmpScrn.WorkingArea.Width DockedRects(DockedRectCounter).Height = TopDockedHeight DockedRectCounter += 1 End If If BottomDockedHeight > 0 Then DockedRects(DockedRectCounter).X = TmpScrn.WorkingArea.Left DockedRects(DockedRectCounter).Y = TmpScrn.WorkingArea.Bottom DockedRects(DockedRectCounter).Width = TmpScrn.WorkingArea.Width DockedRects(DockedRectCounter).Height = BottomDockedHeight DockedRectCounter += 1 End If End If Next Return DockedRects End Function
Or for those of you who prefer C # ... (Note: this ported code has not been verified)
using System.Drawing; using System.Windows.Forms; public Rectangle[] FindDockedTaskBars(ref int DockedRectCounter) { int LeftDockedWidth = 0; int TopDockedHeight = 0; int RightDockedWidth = 0; int BottomDockedHeight = 0; Rectangle[] DockedRects = new Rectangle[Screen.AllScreens.Count() * 4]; DockedRectCounter = 0; foreach (Screen TmpScrn in Screen.AllScreens) { if (!TmpScrn.Bounds.Equals(TmpScrn.WorkingArea)) { LeftDockedWidth = Math.Abs(Math.Abs(TmpScrn.Bounds.Left) - Math.Abs(TmpScrn.WorkingArea.Left)); TopDockedHeight = Math.Abs(Math.Abs(TmpScrn.Bounds.Top) - Math.Abs(TmpScrn.WorkingArea.Top)); RightDockedWidth = (TmpScrn.Bounds.Width - LeftDockedWidth) - TmpScrn.WorkingArea.Width; BottomDockedHeight = (TmpScrn.Bounds.Height - TopDockedHeight) - TmpScrn.WorkingArea.Height; if (LeftDockedWidth > 0) { DockedRects[DockedRectCounter].X = TmpScrn.Bounds.Left; DockedRects[DockedRectCounter].Y = TmpScrn.Bounds.Top; DockedRects[DockedRectCounter].Width = LeftDockedWidth; DockedRects[DockedRectCounter].Height = TmpScrn.Bounds.Height; DockedRectCounter += 1; } if (RightDockedWidth > 0) { DockedRects[DockedRectCounter].X = TmpScrn.WorkingArea.Right; DockedRects[DockedRectCounter].Y = TmpScrn.Bounds.Top; DockedRects[DockedRectCounter].Width = RightDockedWidth; DockedRects[DockedRectCounter].Height = TmpScrn.Bounds.Height; DockedRectCounter += 1; } if (TopDockedHeight > 0) { DockedRects[DockedRectCounter].X = TmpScrn.WorkingArea.Left; DockedRects[DockedRectCounter].Y = TmpScrn.Bounds.Top; DockedRects[DockedRectCounter].Width = TmpScrn.WorkingArea.Width; DockedRects[DockedRectCounter].Height = TopDockedHeight; DockedRectCounter += 1; } if (BottomDockedHeight > 0) { DockedRects[DockedRectCounter].X = TmpScrn.WorkingArea.Left; DockedRects[DockedRectCounter].Y = TmpScrn.WorkingArea.Bottom; DockedRects[DockedRectCounter].Width = TmpScrn.WorkingArea.Width; DockedRects[DockedRectCounter].Height = BottomDockedHeight; DockedRectCounter += 1; } } } return DockedRects; }
Mark McGinty Mar 22 '12 at 16:13 2012-03-22 16:13
source share