Skip to content

Instantly share code, notes, and snippets.

@0x00000FF
Created February 24, 2020 06:00
Show Gist options
  • Save 0x00000FF/ae247104597c337e213a35aef4adac92 to your computer and use it in GitHub Desktop.
Save 0x00000FF/ae247104597c337e213a35aef4adac92 to your computer and use it in GitHub Desktop.
// CoreLoad.cs Ln 412から
private bool AreTheseClose(int cmp1, int cmp2)
{
return Math.Abs(cmp1 - cmp2) < 25;
}
// タイトルバーのドラッグ終了
private void TitleBar_MouseUp(object sender, MouseEventArgs e)
{
// マウスのボタンを離した位置にウィンドウを移動させ、ドラッグ終了
if (moving)
{
var x = this.Location.X < 0 ? 0 : this.Location.X;
var y = this.Location.Y < 0 ? 0 : this.Location.Y;
// Screen.FromControl(Control)で今のスクリーンを検索
var screenRect = System.Windows.Forms.Screen.FromControl(this)
.WorkingArea;
// スクリーンからはみ出ないように調整
// サイズ基準 -> Offset基準
if (x < screenRect.Left ||
AreTheseClose(x, screenRect.Left))
x = screenRect.Left;
else if (x + this.Size.Width > screenRect.Right ||
AreTheseClose(x + this.Size.Width, screenRect.Right))
x = screenRect.Right - this.Size.Width;
if (x < screenRect.Top ||
AreTheseClose(y, screenRect.Top))
y = screenRect.Top;
else if (y + this.Size.Height > screenRect.Bottom ||
AreTheseClose(y + this.Size.Height, screenRect.Bottom))
y = screenRect.Bottom - this.Size.Height;
this.Location = new Point(x, y);
moving = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment