Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Created August 8, 2012 04:25
Show Gist options
  • Save ngyuki/3292015 to your computer and use it in GitHub Desktop.
Save ngyuki/3292015 to your computer and use it in GitHub Desktop.
フォームなどの Rectangle が画面内に収まるようにサイズや表示位置を調整する(マルチモニタ対応)
private Rectangle AdjustmentWindowBounds(Rectangle bounds)
{
var workarea = Screen.FromRectangle(bounds).WorkingArea;
if (workarea.Contains(bounds) == false)
{
if (bounds.Width > workarea.Width)
{
bounds.Width = workarea.Width;
}
if (bounds.Height > workarea.Height)
{
bounds.Height = workarea.Height;
}
if (bounds.Right > workarea.Right)
{
bounds.Offset(workarea.Right - bounds.Right, 0);
}
if (bounds.Bottom > workarea.Bottom)
{
bounds.Offset(0, workarea.Bottom - bounds.Bottom);
}
if (bounds.Left < workarea.Left)
{
bounds.Offset(workarea.Left - bounds.Left, 0);
}
if (bounds.Top < workarea.Top)
{
bounds.Offset(0, workarea.Top - bounds.Top);
}
}
return bounds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment