public static Screen FromControl(Control control) { return(XplatUI.ScreenFromWindow(control.Handle)); }
private Point CalculateShowPoint(Point position, ToolStripDropDownDirection direction, Size size) { var parent = XplatUI.GetParent(Handle, true); var handle = parent == IntPtr.Zero ? Handle : parent; var bestScreen = XplatUI.ScreenFromWindow(handle); Point show_point = position; if (this is ContextMenuStrip) { // If we are going to go offscreen, adjust our direction so we don't... // X direction switch (direction) { case ToolStripDropDownDirection.AboveLeft: if (show_point.X - size.Width < bestScreen.WorkingArea.Left) { direction = ToolStripDropDownDirection.AboveRight; } break; case ToolStripDropDownDirection.BelowLeft: if (show_point.X - size.Width < bestScreen.WorkingArea.Left) { direction = ToolStripDropDownDirection.BelowRight; } break; case ToolStripDropDownDirection.Left: if (show_point.X - size.Width < bestScreen.WorkingArea.Left) { direction = ToolStripDropDownDirection.Right; } break; case ToolStripDropDownDirection.AboveRight: if (show_point.X + size.Width > bestScreen.WorkingArea.Right) { direction = ToolStripDropDownDirection.AboveLeft; } break; case ToolStripDropDownDirection.BelowRight: case ToolStripDropDownDirection.Default: if (show_point.X + size.Width > bestScreen.WorkingArea.Right) { direction = ToolStripDropDownDirection.BelowLeft; } break; case ToolStripDropDownDirection.Right: if (show_point.X + size.Width > bestScreen.WorkingArea.Right) { direction = ToolStripDropDownDirection.Left; } break; } // Y direction switch (direction) { case ToolStripDropDownDirection.AboveLeft: if (show_point.Y - size.Height < bestScreen.WorkingArea.Top) { direction = ToolStripDropDownDirection.BelowLeft; } break; case ToolStripDropDownDirection.AboveRight: if (show_point.Y - size.Height < bestScreen.WorkingArea.Top) { direction = ToolStripDropDownDirection.BelowRight; } break; case ToolStripDropDownDirection.BelowLeft: if (show_point.Y + size.Height > bestScreen.WorkingArea.Bottom && show_point.Y - size.Height > 0) { direction = ToolStripDropDownDirection.AboveLeft; } break; case ToolStripDropDownDirection.BelowRight: case ToolStripDropDownDirection.Default: if (show_point.Y + size.Height > bestScreen.WorkingArea.Bottom && show_point.Y - size.Height > 0) { direction = ToolStripDropDownDirection.AboveRight; } break; case ToolStripDropDownDirection.Left: if (show_point.Y + size.Height > bestScreen.WorkingArea.Bottom && show_point.Y - size.Height > 0) { direction = ToolStripDropDownDirection.AboveLeft; } break; case ToolStripDropDownDirection.Right: if (show_point.Y + size.Height > bestScreen.WorkingArea.Bottom && show_point.Y - size.Height > 0) { direction = ToolStripDropDownDirection.AboveRight; } break; } } switch (direction) { case ToolStripDropDownDirection.AboveLeft: show_point.Y -= size.Height; show_point.X -= size.Width; break; case ToolStripDropDownDirection.AboveRight: show_point.Y -= size.Height; break; case ToolStripDropDownDirection.BelowLeft: show_point.X -= size.Width; break; case ToolStripDropDownDirection.Left: show_point.X -= size.Width; break; case ToolStripDropDownDirection.Right: break; } const int gap = 4; // Fix offscreen horizontal positions if ((show_point.X + size.Width) > bestScreen.WorkingArea.Right) { show_point.X = bestScreen.WorkingArea.Right - size.Width - gap; } if (show_point.X < bestScreen.WorkingArea.Left) { show_point.X = bestScreen.WorkingArea.Left + gap; } // Fix offscreen vertical positions if ((show_point.Y + size.Height) > bestScreen.WorkingArea.Bottom) { show_point.Y = bestScreen.WorkingArea.Bottom - size.Height - gap; } if (show_point.Y < bestScreen.WorkingArea.Top) { show_point.Y = bestScreen.WorkingArea.Top + gap; } return(show_point); }