private static RECT PlaceOnScreen(RECT monitorRect, RECT windowRect) { int monitorWidth = monitorRect.Right - monitorRect.Left; int monitorHeight = monitorRect.Bottom - monitorRect.Top; if (windowRect.Right < monitorRect.Left) { // Off left side int width = windowRect.Right - windowRect.Left; if (width > monitorWidth) { width = monitorWidth; } windowRect.Left = monitorRect.Left; windowRect.Right = windowRect.Left + width; } else if (windowRect.Left > monitorRect.Right) { // Off right side int width = windowRect.Right - windowRect.Left; if (width > monitorWidth) { width = monitorWidth; } windowRect.Right = monitorRect.Right; windowRect.Left = windowRect.Right - width; } if (windowRect.Bottom < monitorRect.Top) { // Off top int height = windowRect.Bottom - windowRect.Top; if (height > monitorHeight) { height = monitorHeight; } windowRect.Top = monitorRect.Top; windowRect.Bottom = windowRect.Top + height; } else if (windowRect.Top > monitorRect.Bottom) { // Off bottom int height = windowRect.Bottom - windowRect.Top; if (height > monitorHeight) { height = monitorHeight; } windowRect.Bottom = monitorRect.Bottom; windowRect.Top = windowRect.Bottom - height; } return windowRect; }
private static bool RectanglesIntersect(RECT a, RECT b) { if (a.Left > b.Right || a.Right < b.Left) { return false; } if (a.Top > b.Bottom || a.Bottom < b.Top) { return false; } return true; }