淡定从容,宁静致远
(329)
(4)
(50)
(68)
(45)
(162)
分类: windows
2015-04-15 09:58:16
拖动无边框窗体form至桌面任何位置
首先建一个windows应用程序
将form1的 formborderstyle属性设置为noe
主要是在form1窗体触发三个事件:form4_mousedown,form4_mousemove,form4_mouseup
代码如下:
public partial class form1 : form
{
point mouseoff; //鼠标移动位置变量
bool leftflag; //标签是否为左键
public form1()
{
initializecomponent();
}
//用代码设置窗体的起始位置
private void form_load(object sender, system.eventargs e)
{
this.left=(int)((screen.primaryscreen.bounds.width-this.width)/2);
this.top=(int)((screen.primaryscreen.bounds.height-this.height)/2);
}
private void form1_mousedown(object sender, mouseeventargs e)
{
if (e.button == mousebuttons.left)
{
mouseoff = new point(-e.x, -e.y); //得到变量的值
leftflag = true; //点击左键按下时标注为true;
}
}
private void form1_mousemove(object sender, mouseeventargs e)
{
if (leftflag)
{
point mouseset = control.mouseposition;
mouseset.offset(mouseoff.x, mouseoff.y); //设置移动后的位置
location = mouseset;
}
}
private void form1_mouseup(object sender, mouseeventargs e)
{
if (leftflag)
{
leftflag = false;//释放鼠标后标注为false;
}
}
}
上一篇:
下一篇: