欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 实现窗体拖到屏幕边缘自动隐藏

程序员文章站 2022-07-22 09:45:37
以下给出源代码: (注:hide为窗体名称) 复制代码 代码如下:private void hide_load(object sender, eventargs e) {...
以下给出源代码: (注:hide为窗体名称)
复制代码 代码如下:

private void hide_load(object sender, eventargs e)
{
system.windows.forms.timer stoprecttimer = new system.windows.forms.timer();
stoprecttimer.tick += new eventhandler(timer1_tick);
stoprecttimer.interval = 100;
stoprecttimer.enabled = true;
}
private void timer1_tick(object sender, eventargs e)
{
if (this.bounds.contains(cursor.position))
{
switch (this.stopaanhor)
{
case anchorstyles.top:
this.location = new point(this.location.x, 0);
break;
case anchorstyles.left:
this.location = new point(0, this.location.y);
break;
case anchorstyles.right:
this.location = new point(screen.primaryscreen.bounds.width - this.width, this.location.y);
break;
}
}
else
{
switch (this.stopaanhor)
{
case anchorstyles.top:
this.location = new point(this.location.x, (this.height - 2) * (-1));
break;
case anchorstyles.left:
this.location = new point((-1) * (this.width - 2), this.location.y);
break;
case anchorstyles.right:
this.location = new point(screen.primaryscreen.bounds.width - 2, this.location.y);
break;
}
}
}
internal anchorstyles stopaanhor = anchorstyles.none;
private void mstopanhor()
{
if (this.top <= 0)
{
stopaanhor = anchorstyles.top;
}
else if (this.left <= 0)
{
stopaanhor = anchorstyles.left;
}
else if (this.left >= screen.primaryscreen.bounds.width - this.width)
{
stopaanhor = anchorstyles.right;
}
else
{
stopaanhor = anchorstyles.none;
}
}
private void hide_locationchanged(object sender, eventargs e)
{
this.mstopanhor();
}