c# 实现窗体拖到屏幕边缘自动隐藏
程序员文章站
2023-11-22 11:05:16
以下给出源代码: (注: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();
}
复制代码 代码如下:
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();
}
上一篇: 详解JavaScript的内存空间、赋值和深浅拷贝
下一篇: 抖音上用记事本编写爱心小程序教程