/// <summary>
        /// Determines whether the specified System.Drawing.Rectangle is contained within the Windows 7 notification area fly-out.
        /// Note that this function will return false if the fly-out is closed, or if run on older versions of Windows.
        /// </summary>
        /// <param name="point">System.Drawing.Rectangle to test.</param>
        /// <returns>True if the notify icon is in the fly-out, false if not.</returns>
        public static bool IsRectangleInFlyOut(Rectangle rectangle)
        {
            if (!SysInfo.IsWindows7OrLater)
            {
                return(false);
            }

            Rectangle taskbarRect = Taskbar.GetTaskbarRectangle();

            // Don't use Rectangle.IntersectsWith since we want to check if it's ENTIRELY inside
            return(rectangle.Left > taskbarRect.Right || rectangle.Right < taskbarRect.Left ||
                   rectangle.Bottom < taskbarRect.Top || rectangle.Top > taskbarRect.Bottom);
        }
        /// <summary>
        /// Returns the optimum window position in relation to the specified notify icon.
        /// </summary>
        /// <param name="notifyicon">The notify icon that the window should be aligned to.</param>
        /// <param name="windowwidth">The width of the window.</param>
        /// <param name="windowheight">The height of the window.</param>
        /// <param name="dpi">The system's DPI (in relative units: 1.0 = 96 DPI, 1.25 = 120 DPI, etc.).</param>
        /// <param name="pinned">Whether the window is pinned open or not. Affects positioning in Windows 7 only.</param>
        /// <returns>A Point specifying the suggested location of the window (top left point).</returns>
        public static Point GetWindowPosition(NotifyIcon notifyicon, double windowwidth, double windowheight, double dpi, bool pinned)
        {
            // retrieve taskbar rect
            Rectangle taskbarRectangle = Taskbar.GetTaskbarRectangle();

            // retrieve notify icon location
            Rectangle nipositiontemp = NotifyIconHelpers.GetNotifyIconRectangle(notifyicon, true);

            // if our functions can't find the rectangle, align it to a corner of the screen
            Rectangle niposition;

            if (nipositiontemp == Rectangle.Empty)
            {
                switch (Taskbar.GetTaskbarEdge())
                {
                case Taskbar.TaskbarEdge.Bottom:     // bottom right corner
                    niposition = new Rectangle(taskbarRectangle.Right - 1, taskbarRectangle.Bottom - 1, 1, 1);
                    break;

                case Taskbar.TaskbarEdge.Top:     // top right corner
                    niposition = new Rectangle(taskbarRectangle.Right - 1, taskbarRectangle.Top, 1, 1);
                    break;

                case Taskbar.TaskbarEdge.Right:     // bottom right corner
                    niposition = new Rectangle(taskbarRectangle.Right - 1, taskbarRectangle.Bottom - 1, 1, 1);
                    break;

                case Taskbar.TaskbarEdge.Left:     // bottom left corner
                    niposition = new Rectangle(taskbarRectangle.Left, taskbarRectangle.Bottom - 1, 1, 1);
                    break;

                default:
                    goto case Taskbar.TaskbarEdge.Bottom;
                }
            }
            else
            {
                niposition = (Rectangle)nipositiontemp;
            }

            // check if notify icon is in the fly-out
            bool inflyout = NotifyIconHelpers.IsRectangleInFlyOut(niposition);

            // if the window is pinned open and in the fly-out (Windows 7 only),
            // we should position the window above the 'show hidden icons' button
            if (inflyout && pinned)
            {
                niposition = (Rectangle)NotifyArea.GetButtonRectangle();
            }

            // determine centre of notify icon
            Point nicentre = new Point(niposition.Left + (niposition.Width / 2), niposition.Top + (niposition.Height / 2));

            // get window offset from edge
            double edgeoffset = WindowEdgeOffset * dpi;

            // get working area bounds
            Rectangle workarea = GetWorkingArea(niposition);

            // calculate window position
            double windowleft = 0, windowtop = 0;

            switch (Taskbar.GetTaskbarEdge())
            {
            case Taskbar.TaskbarEdge.Bottom:
                // horizontally centre above icon
                windowleft = nicentre.X - (windowwidth / 2);
                if (inflyout)
                {
                    windowtop = niposition.Top - windowheight - edgeoffset;
                }
                else
                {
                    windowtop = taskbarRectangle.Top - windowheight - edgeoffset;
                }

                break;

            case Taskbar.TaskbarEdge.Top:
                // horizontally centre below icon
                windowleft = nicentre.X - (windowwidth / 2);
                if (inflyout)
                {
                    windowtop = niposition.Bottom + edgeoffset;
                }
                else
                {
                    windowtop = taskbarRectangle.Bottom + edgeoffset;
                }

                break;

            case Taskbar.TaskbarEdge.Left:
                // vertically centre to the right of icon (or above icon if in flyout and not pinned)
                if (inflyout && !pinned)
                {
                    windowleft = nicentre.X - (windowwidth / 2);
                    windowtop  = niposition.Top - windowheight - edgeoffset;
                }
                else
                {
                    windowleft = taskbarRectangle.Right + edgeoffset;
                    windowtop  = nicentre.Y - (windowheight / 2);
                }

                break;

            case Taskbar.TaskbarEdge.Right:
                // vertically centre to the left of icon (or above icon if in flyout and not pinned)
                if (inflyout && !pinned)
                {
                    windowleft = nicentre.X - (windowwidth / 2);
                    windowtop  = niposition.Top - windowheight - edgeoffset;
                }
                else
                {
                    windowleft = taskbarRectangle.Left - windowwidth - edgeoffset;
                    windowtop  = nicentre.Y - (windowheight / 2);
                }

                break;

            default:
                goto case Taskbar.TaskbarEdge.Bottom;     // should be unreachable
            }

            //// check that the window is within the working area
            //// if not, put it next to the closest edge

            if (windowleft + windowwidth + edgeoffset > workarea.Right) // too far right
            {
                windowleft = workarea.Right - windowwidth - edgeoffset;
            }
            else if (windowleft < workarea.Left) // too far left
            {
                windowleft = workarea.Left + edgeoffset;
            }

            if (windowtop + windowheight + edgeoffset > workarea.Bottom) // too far down
            {
                windowtop = workarea.Bottom - windowheight - edgeoffset;
            }
            //// the window should never be too far up, so we can skip checking for that

            return(new Point((int)windowleft, (int)windowtop));
        }