winform 的textbox 鼠标拖动
这个窗体有3个TextBox控件,控件ID分别是textBox1,textBox2,textBox3
两个button,分别为button1,button2
其实这个就是通过mousedown.mouseup,mousemove 三个事件来控制,实现textbox的长度可变,窗体的宽度可变
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
//定义一个布尔变量,作为事件的开关。
bool b = false;
//定义一个布尔变量,作为窗体大小改变的开关。
bool bFormChange = false;
//定义一个‘点’的变量,接收鼠标的点位置。
Point mousePonit;
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if (this.Cursor == System.Windows.Forms.Cursors.SizeWE && e.Button == MouseButtons.Left)
{
bFormChange = true;
}
//考虑是否鼠标左键按下,如果按下则开始做以下的事情。
else if (e.Button == MouseButtons.Left)
{
//给mousePonit定义为当前的鼠标位置坐标。
mousePonit = new Point(-e.X, -e.Y);
//设置变量b为布尔真值。
b = true;
}
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (e.X >= this.Width - 3)
{
this.Cursor = System.Windows.Forms.Cursors.SizeWE;
}
else
{
this.Cursor = System.Windows.Forms.Cursors.Hand;
}
int tempWidth = textBox1.Size.Width + textBox2.Size.Width + textBox3.Size.Width + 100;
if (bFormChange && e.X <= 1000 && e.X >= tempWidth)
{
this.Size = new System.Drawing.Size(e.X+ 1, 23);
this.button2.Location = new System.Drawing.Point(e.X - 25, 1);
this.button1.Location = new System.Drawing.Point(e.X - 50, 1);
}
//如果获取b为真的时候,开始执行下面的语句。
if (b)
{
//定义一个‘点’变量,为组件的鼠标光标位置
Point p = Control.MousePosition;
//平移mousePonit为p变量。
p.Offset(mousePonit);
//控件的位置,为p位置。
this.Location = p;
}
}
private void Form2_MouseUp(object sender, MouseEventArgs e)
{
bFormChange = false;
b = false;
}
// 关于txt1拖动
bool txt1MoveFlg = false;
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
if (this.textBox1.Cursor == System.Windows.Forms.Cursors.SizeWE && e.Button == MouseButtons.Left)
{
txt1MoveFlg = true;
}
}
private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X >= textBox1.Size.Width - 3)
{
this.textBox1.Cursor = System.Windows.Forms.Cursors.SizeWE;
}
else
{
this.textBox1.Cursor = System.Windows.Forms.Cursors.IBeam;
}
int tempWidth = e.X + textBox2.Size.Width + textBox3.Size.Width + 100;
if (txt1MoveFlg && e.X >= 40 && tempWidth <= this.Width)
{
this.textBox1.Size = new System.Drawing.Size(e.X + 1, 19);
this.textBox2.Location = new System.Drawing.Point(textBox1.Size.Width + 25, 1);
this.textBox3.Location = new System.Drawing.Point(textBox1.Size.Width + textBox2.Size.Width+ 40, 1);
}
}
private void textBox1_MouseUp(object sender, MouseEventArgs e)
{
txt1MoveFlg = false;
}
// 关于txt2拖动
bool txt2MoveFlg = false;
private void textBox2_MouseDown(object sender, MouseEventArgs e)
{
if (this.textBox2.Cursor == System.Windows.Forms.Cursors.SizeWE && e.Button == MouseButtons.Left)
{
txt2MoveFlg = true;
}
}
private void textBox2_MouseMove(object sender, MouseEventArgs e)
{
if (e.X >= textBox2.Size.Width - 3)
{
this.textBox2.Cursor = System.Windows.Forms.Cursors.SizeWE;
}
else
{
this.textBox2.Cursor = System.Windows.Forms.Cursors.IBeam;
}
int tempWidth = e.X + textBox1.Size.Width + textBox3.Size.Width + 100;
if (txt2MoveFlg && e.X >= 40 && tempWidth <= this.Width)
{
this.textBox2.Size = new System.Drawing.Size(e.X + 1, 19);
this.textBox3.Location = new System.Drawing.Point(textBox1.Size.Width + textBox2.Size.Width + 40, 1);
}
}
private void textBox2_MouseUp(object sender, MouseEventArgs e)
{
txt2MoveFlg = false;
}
// 关于txt3拖动
bool txt3MoveFlg = false;
private void textBox3_MouseDown(object sender, MouseEventArgs e)
{
if (this.textBox3.Cursor == System.Windows.Forms.Cursors.SizeWE && e.Button == MouseButtons.Left)
{
txt3MoveFlg = true;
}
}
private void textBox3_MouseMove(object sender, MouseEventArgs e)
{
if (e.X >= textBox3.Size.Width - 3)
{
this.textBox3.Cursor = System.Windows.Forms.Cursors.SizeWE;
}
else
{
this.textBox3.Cursor = System.Windows.Forms.Cursors.IBeam;
}
int tempWidth = e.X + textBox1.Size.Width + textBox2.Size.Width + 100;
if (txt3MoveFlg && e.X >= 40 && tempWidth <= this.Width)
{
this.textBox3.Size = new System.Drawing.Size(e.X + 1, 19);
}
}
private void textBox3_MouseUp(object sender, MouseEventArgs e)
{
txt3MoveFlg = false;
}