mousedown(object sender, mouseeventargs e)
mouseup(object sender, mouseeventargs e)
mousemove(object sender, mouseeventargs e)
这三个函数的作用分别是鼠标按下时激活,鼠标松开激活和鼠标移动
需要考虑的情况如下
1. 只有在鼠标按下的时候才可以使用鼠标移动事件
2. 鼠标移动的时候控件重新调整位置
为了有效控制第一个条件,所以可以将鼠标移动事件的绑定推迟到鼠标按下事件中,而不是在初始化时完成,另外要记录下初始状态下鼠标位于控件中的坐标,这里注意mouseeventargs参数中的x,y表示的是鼠标在当前控件中的layout坐标,而不是鼠标在主窗口中的坐标。
rivate void button1_mousedown(object sender, mouseeventargs e)
{
this.tmpx = e.x;
this.tmpy = e.y;
this.button1.mousemove += new system.windows.forms.mouseeventhandler(this.button1_mousemove);
}
同时在鼠标送开事件中再将该方法绑定脱离
private void button1_mouseup(object sender, mouseeventargs e)
{
this.button1.mousemove -= new system.windows.forms.mouseeventhandler(this.button1_mousemove);
}
最后是mousemove事件
private void button1_mousemove(object sender, mouseeventargs e)
{
this.button1.location = new system.drawing.point(this.button1.location.x + e.x - this.tmpx, this.button1.location.y + e.y - this.tmpy);
}
这里重新计算了控件的新位置。
例子中使用了button作为实验对象并且额外定义了
private int tmpx = 0;
private int tmpy = 0;
private int tmpx = 0;
private int tmpy = 0;
private void panel1_mousedown(object sender, mouseeventargs e)
{
if (e.button == mousebuttons.left)
{
this.tmpx = e.x;
this.tmpy = e.y;
this.panel1.cursor = cursors.sizeall;
this.panel1.mousemove += new mouseeventhandler(panel1_mousemove);
}
}
private void panel1_mouseup(object sender, mouseeventargs e)
{
this.panel1.cursor = cursors.default;
this.panel1.mousemove -= new mouseeventhandler(panel1_mousemove);
}
void panel1_mousemove(object sender, mouseeventargs e)
{
this.panel1.location = new point(this.panel1.location.x + e.x - this.tmpx, this.panel1.location.y + e.y - this.tmpy);
}
看一个winform,showdialog,子窗体向父窗体传值
例子:
下面是子窗体代码,要求输入phone,然后会返回给父窗体。
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;namespace windowsapplication1
{
public partial class phone : form
{
public phone()
{
initializecomponent();
btnok.dialogresult = dialogresult.ok;
btnok.dialogresult = dialogresult.cancel;
}
public string phonenumber
{
get { return textbox1.text; }
set { textbox1.text = value; }
}
private void phone_load(object sender, eventargs e)
{}
}
}
不包含任何处理按钮单击事件的代码,因为设置了每个按钮的dialogresult属性,所以单击ok或者cancel按钮后,窗体就消失了。下面的代码显示了父窗体中调用phone对话框的方法。
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;namespace windowsapplication1
{
public partial class form7 : form
{
public form7()
{
initializecomponent();
}private void button1_click(object sender, eventargs e)
{
phone frm = new phone();
frm.showdialog();
if (frm.dialogresult == dialogresult.ok)
{
label1.text = "phone number is " + frm.phonenumber;
}
else if (frm.dialogresult == dialogresult.cancel)
{
label1.text = "form was canceled";
}
frm.close();
}
}
}
看起来非常简单,创建新的phone对象frm,在调用frm.showdialog方法是,代码停止,等待phone窗体返回,接着检查phone窗体的dialogresult属性,由于窗体还没有释放,是不可见的,所以仍可以访问公共属性phonenumber,一旦获取了需要的数据,就可以嗲用窗体的close方法。
一切正常,但是如果返回的格式不正确怎么办,就要把showdialog方法放在循环中,就可以再次调用,让用户重新输入,就可以得到正确的值。
上面的代码改成下面的即可。
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;namespace windowsapplication1
{
public partial class form7 : form
{
public form7()
{
initializecomponent();
}private void button1_click(object sender, eventargs e)
{
phone frm = new phone();while (true)
{
frm.showdialog();
if (frm.dialogresult == dialogresult.ok)
{
label1.text = "phone number is " + frm.phonenumber;
if (frm.phonenumber.length == 8 || frm.phonenumber.length == 12)
{
break;
}
else
{
messagebox.show("");
}
}
else if (frm.dialogresult == dialogresult.cancel)
{
label1.text = "form was canceled";
break;
}
}
frm.close();
}
}
}