Windows Taskbar Location

I was working on a form window today that pops up from windows tray area and I wanted it to have a different location depending on taskbar dock location.

So again I find lots of people using API (SHAppBarMessage) and other sloppy solutions and came up with my own idea to simply compare Screen.PrimaryScreen.WorkingArea with Screen.PrimaryScreen.Bounds.

Here is my code:
        private enum TaskBarLocation { Left, Right, Top, Bottom, Hidden };
        private TaskBarLocation GetLocation()
        {
            Rectangle taskBarRect = Rectangle.Intersect(Screen.PrimaryScreen.WorkingArea, Screen.PrimaryScreen.Bounds);

            if (taskBarRect.Width == Screen.PrimaryScreen.Bounds.Width)
            {
                return taskBarRect.Y == 0 ? TaskBarLocation.Bottom : TaskBarLocation.Top;
            }
            else if (taskBarRect.Height == Screen.PrimaryScreen.Bounds.Height)
            {
                return taskBarRect.X == 0 ? TaskBarLocation.Right : TaskBarLocation.Left;
            }
            else
            {
                return TaskBarLocation.Hidden;
            }
        }
Then to set window location:
                switch (GetLocation())
                {
                    case TaskBarLocation.Left:
                        this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
                        break;
                    case TaskBarLocation.Top:
                        this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Top);
                        break;
                    case TaskBarLocation.Right:
                    case TaskBarLocation.Bottom:
                    default:
                        this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
                        break;
                }


Enjoy!

0 kommentarer:

Post a Comment