问题描述
我在panel里面动态创建了几个button,通过MouseMove实现了拖动,但是现在我拖动以后,在原来的位置会还有一个button,我需要怎么样才能把原来那个去掉。如果不是动态创建的,我看如果拖动,原来的位置是没有控件了的。privatevoidDrawJob(Graphicsg){//创建一个新的Button组件ButtonmyButton=newButton();//设定他的名称和Text属性,以及产生的相对位置myButton.Name="button"+1;myButton.Text="按钮"+1;myButton.Location=newPoint(100,200);//为产生的新的Button组件设定事件,本文中为产生的按钮设定了三个事件myButton.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.button1_MouseMove);myButton.MouseDown+=newSystem.Windows.Forms.MouseEventHandler(this.button1_MouseDown);//myButton.Click+=newSystem.EventHandler(this.btn_Click);//在窗体中显示此按钮this.back_panel.Controls.Add(myButton);}privatevoidbutton1_MouseMove(objectsender,MouseEventArgse){Buttoncontrol=(Button)sender;if(e.Button==MouseButtons.Left){control.Left+=Cursor.Position.X-_oldPosition.X;control.Top+=Cursor.Position.Y-_oldPosition.Y;_oldPosition=Cursor.Position;}}privatevoidbutton1_MouseDown(objectsender,MouseEventArgse){_oldPosition=Cursor.Position;}
解决方案
解决方案二:
重绘按钮原先所在位置区域
解决方案三:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsApplication1{publicpartialclassForm14:Form{//以下为拖拽缩放的代码privateenumEnumMousePointPosition{MouseSizeNone=0,//'无MouseSizeRight=1,//'拉伸右边框MouseSizeLeft=2,//'拉伸左边框MouseSizeBottom=3,//'拉伸下边框MouseSizeTop=4,//'拉伸上边框MouseSizeTopLeft=5,//'拉伸左上角MouseSizeTopRight=6,//'拉伸右上角MouseSizeBottomLeft=7,//'拉伸左下角MouseSizeBottomRight=8,//'拉伸右下角MouseDrag=9//'鼠标拖动}//2、定义几个变量constintBand=5;constintMinWidth=10;constintMinHeight=10;privateEnumMousePointPositionm_MousePointPosition;privatePointp,p1;//3、定义自己的MyMouseDown事件privatevoidMyMouseDown(objectsender,System.Windows.Forms.MouseEventArgse){p.X=e.X;p.Y=e.Y;p1.X=e.X;p1.Y=e.Y;}//4、定义自己的MyMouseLeave事件privatevoidMyMouseLeave(objectsender,System.EventArgse){m_MousePointPosition=EnumMousePointPosition.MouseSizeNone;this.Cursor=Cursors.Arrow;}//5、设计一个函数,确定光标在控件不同位置的样式privateEnumMousePointPositionMousePointPosition(Sizesize,System.Windows.Forms.MouseEventArgse){if((e.X>=-1*Band)|(e.X<=size.Width)|(e.Y>=-1*Band)|(e.Y<=size.Height)){if(e.X<Band){if(e.Y<Band){returnEnumMousePointPosition.MouseSizeTopLeft;}else{if(e.Y>-1*Band+size.Height){returnEnumMousePointPosition.MouseSizeBottomLeft;}else{returnEnumMousePointPosition.MouseSizeLeft;}}}else{if(e.X>-1*Band+size.Width){if(e.Y<Band){returnEnumMousePointPosition.MouseSizeTopRight;}else{if(e.Y>-1*Band+size.Height){returnEnumMousePointPosition.MouseSizeBottomRight;}else{returnEnumMousePointPosition.MouseSizeRight;}}}else{if(e.Y<Band){returnEnumMousePointPosition.MouseSizeTop;}else{if(e.Y>-1*Band+size.Height){returnEnumMousePointPosition.MouseSizeBottom;}else{returnEnumMousePointPosition.MouseDrag;}}}}}else{returnEnumMousePointPosition.MouseSizeNone;}}
解决方案四:
//6、定义自己的MyMouseMove事件,在这个事件里,会使用上面设计的函数privatevoidMyMouseMove(objectsender,System.Windows.Forms.MouseEventArgse){ControllCtrl=(senderasControl);if(e.Button==MouseButtons.Left){switch(m_MousePointPosition){caseEnumMousePointPosition.MouseDrag:lCtrl.Left=lCtrl.Left+e.X-p.X;lCtrl.Top=lCtrl.Top+e.Y-p.Y;break;caseEnumMousePointPosition.MouseSizeBottom:lCtrl.Height=lCtrl.Height+e.Y-p1.Y;p1.X=e.X;p1.Y=e.Y;//'记录光标拖动的当前点break;caseEnumMousePointPosition.MouseSizeBottomRight:lCtrl.Width=lCtrl.Width+e.X-p1.X;lCtrl.Height=lCtrl.Height+e.Y-p1.Y;p1.X=e.X;p1.Y=e.Y;//'记录光标拖动的当前点break;caseEnumMousePointPosition.MouseSizeRight:lCtrl.Width=lCtrl.Width+e.X-p1.X;//lCtrl.Height=lCtrl.Height+e.Y-p1.Y;p1.X=e.X;p1.Y=e.Y;//'记录光标拖动的当前点break;caseEnumMousePointPosition.MouseSizeTop:lCtrl.Top=lCtrl.Top+(e.Y-p.Y);lCtrl.Height=lCtrl.Height-(e.Y-p.Y);break;caseEnumMousePointPosition.MouseSizeLeft:lCtrl.Left=lCtrl.Left+e.X-p.X;lCtrl.Width=lCtrl.Width-(e.X-p.X);break;caseEnumMousePointPosition.MouseSizeBottomLeft:lCtrl.Left=lCtrl.Left+e.X-p.X;lCtrl.Width=lCtrl.Width-(e.X-p.X);lCtrl.Height=lCtrl.Height+e.Y-p1.Y;p1.X=e.X;p1.Y=e.Y;//'记录光标拖动的当前点break;caseEnumMousePointPosition.MouseSizeTopRight:lCtrl.Top=lCtrl.Top+(e.Y-p.Y);lCtrl.Width=lCtrl.Width+(e.X-p1.X);lCtrl.Height=lCtrl.Height-(e.Y-p.Y);p1.X=e.X;p1.Y=e.Y;//'记录光标拖动的当前点break;caseEnumMousePointPosition.MouseSizeTopLeft:lCtrl.Left=lCtrl.Left+e.X-p.X;lCtrl.Top=lCtrl.Top+(e.Y-p.Y);lCtrl.Width=lCtrl.Width-(e.X-p.X);lCtrl.Height=lCtrl.Height-(e.Y-p.Y);break;default:break;}if(lCtrl.Width<MinWidth)lCtrl.Width=MinWidth;if(lCtrl.Height<MinHeight)lCtrl.Height=MinHeight;}else{m_MousePointPosition=MousePointPosition(lCtrl.Size,e);//'判断光标的位置状态switch(m_MousePointPosition)//'改变光标{caseEnumMousePointPosition.MouseSizeNone:this.Cursor=Cursors.Arrow;//'箭头break;caseEnumMousePointPosition.MouseDrag:this.Cursor=Cursors.SizeAll;//'四方向break;caseEnumMousePointPosition.MouseSizeBottom:this.Cursor=Cursors.SizeNS;//'南北break;caseEnumMousePointPosition.MouseSizeTop:this.Cursor=Cursors.SizeNS;//'南北break;caseEnumMousePointPosition.MouseSizeLeft:this.Cursor=Cursors.SizeWE;//'东西break;caseEnumMousePointPosition.MouseSizeRight:this.Cursor=Cursors.SizeWE;//'东西break;caseEnumMousePointPosition.MouseSizeBottomLeft:this.Cursor=Cursors.SizeNESW;//'东北到南西break;caseEnumMousePointPosition.MouseSizeBottomRight:this.Cursor=Cursors.SizeNWSE;//'东南到西北break;caseEnumMousePointPosition.MouseSizeTopLeft:this.Cursor=Cursors.SizeNWSE;//'东南到西北break;caseEnumMousePointPosition.MouseSizeTopRight:this.Cursor=Cursors.SizeNESW;//'东北到南西break;default:break;}}//drawpoint();}//7、制作一个初始化过程,将界面panel1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件,在Form初始化或者Form_Load时先执行它。publicvoidinitProperty(){for(inti=0;i<this.panel1.Controls.Count;i++){this.panel1.Controls[i].MouseDown+=newSystem.Windows.Forms.MouseEventHandler(MyMouseDown);this.panel1.Controls[i].MouseLeave+=newSystem.EventHandler(MyMouseLeave);this.panel1.Controls[i].MouseMove+=newSystem.Windows.Forms.MouseEventHandler(MyMouseMove);}}publicForm14(){TextBoxbt=newTextBox();bt.Bounds=newRectangle(10,10,72,21);InitializeComponent();panel1.Controls.Add(bt);initProperty();}}}
解决方案五:
放个Panel名字panel1和一个Button名字button1
解决方案六:
给你个例子你可以参考下http://blog.csdn.net/zgke/archive/2009/01/06/3718989.aspx
解决方案七:
up
解决方案八:
学习