winform 的textbox 鼠标拖动

这个窗体有3个TextBox控件,控件ID分别是textBox1,textBox2,textBox3

两个button,分别为button1,button2

其实这个就是通过mousedown.mouseup,mousemove   三个事件来控制,实现textbox的长度可变,窗体的宽度可变

ContractedBlock.gif ExpandedBlockStart.gif Code
//定义一个布尔变量,作为事件的开关。
        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+ 123);
                
this.button2.Location = new System.Drawing.Point(e.X - 251);
                
this.button1.Location = new System.Drawing.Point(e.X - 501);
            }

            
//如果获取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 + 119);
                
this.textBox2.Location = new System.Drawing.Point(textBox1.Size.Width + 251);
                
this.textBox3.Location = new System.Drawing.Point(textBox1.Size.Width + textBox2.Size.Width+ 401);
            }
        }

        
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 + 119);
                
this.textBox3.Location = new System.Drawing.Point(textBox1.Size.Width + textBox2.Size.Width + 401);
            }
        }

        
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 + 119);
            }
        }

        
private void textBox3_MouseUp(object sender, MouseEventArgs e)
        {
            txt3MoveFlg 
= false;
        }

转载于:https://www.cnblogs.com/libiao/archive/2009/10/28/1591483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值