问题描述
初学者,自己做个小软件。窗体设置为无边框,加了label控件,拖动此label控件可以拖动窗体移动。每个窗体都是采用这个办法,我想编写一个公共方法,都来调用。Pointmouseoff;//定义鼠标移动位置变量boolleftflag;//定义标签是否为左键privatevoidlabel1_MouseDown(objectsender,MouseEventArgse){if(e.Button==MouseButtons.Left){mouseoff=newPoint(-e.X,-e.Y);//得到变量的值leftflag=true;//点击左键按下时标注为true}}privatevoidlabel1_MouseMove(objectsender,MouseEventArgse){if(leftflag){Pointmouseset=Control.MousePosition;mouseset.Offset(mouseoff.X,mouseoff.Y);//设置移动后的位置Location=mouseset;}}privatevoidlabel1_MouseUp(objectsender,MouseEventArgse){if(leftflag){leftflag=false;//释放鼠标后标注为false}}每新建窗体都需要加这些代码,有没有写成办法简单调用就行了。
解决方案
解决方案二:
新建一个用户控件,加个Label,把这些代码放进去以后从工具箱拖出来就行了
解决方案三:
写一个baseform:form,这些代码在baseform里面,然后其它form继承自这个baseform
解决方案四:
引用2楼starfd的回复:
写一个baseform:form,这些代码在baseform里面,然后其它form继承自这个baseform
空闲时能不能写出来,我水平太次,感激。
解决方案五:
参考,无边框的窗体,移动和改变大小usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Diagnostics;namespaceHSEI.Windows.Forms{publicpartialclassMetroForm:Form{privateenumMouseBorderLocation{None,TopLeft,Top,TopRight,Left,Right,BottomLeft,Bottom,BottomRight}privatebool_isUpdateFormCur=true;privateint_borderWidth=5;privatePoint_ptMouseDown=Point.Empty;privateRectangle_mouseDownBounds=Rectangle.Empty;privateRectangleF_rectBorder=RectangleF.Empty;privateMouseBorderLocation_mouseLoc=MouseBorderLocation.None;privateCursor_cur=Cursors.Arrow;publicMetroForm(){InitializeComponent();this.MaximumSize=Screen.GetWorkingArea(this).Size;this.Padding=newPadding(4);CalcBorderArea();WinApi.SetClassLong(this.Handle,WinApi.GCL_STYLE,WinApi.GetClassLong(this.Handle,WinApi.GCL_STYLE)|WinApi.CS_DropSHADOW);}//publicoverrideRectangleDisplayRectangle//{//get//{//varrect=base.DisplayRectangle;//returnnewRectangle(_borderWidth,_borderWidth,rect.Width-2*_borderWidth,//rect.Height-2*_borderWidth);//}//}publicoverrideCursorCursor{get{returnbase.Cursor;}set{base.Cursor=value;if(_isUpdateFormCur){_cur=value;}}}protectedoverridevoidOnPaint(PaintEventArgse){base.OnPaint(e);if(this.WindowState!=FormWindowState.Maximized){Rectanglerect=Rectangle.Round(_rectBorder);e.Graphics.DrawRectangle(newPen(Theme.CurrentTheme.GetPressedBorderColor()),rect);//rect=newRectangle(rect.Left+1,rect.Top+1,rect.Width-2,rect.Height-2);//e.Graphics.DrawRectangle(Pens.White,rect);}}protectedoverridevoidOnSizeChanged(EventArgse){base.OnSizeChanged(e);CalcBorderArea();if(IsMaxSize()){ReloadCursor();}this.Invalidate();}protectedoverridevoidOnMouseMove(MouseEventArgse){base.OnMouseMove(e);if(!IsMaxSize()&&MouseButtons==System.Windows.Forms.MouseButtons.None){CalcMousePostion(e.X,e.Y);switch(_mouseLoc){caseMouseBorderLocation.TopLeft:caseMouseBorderLocation.BottomRight:SetCursor(Cursors.SizeNWSE);break;caseMouseBorderLocation.Top:caseMouseBorderLocation.Bottom:SetCursor(Cursors.SizeNS);break;caseMouseBorderLocation.TopRight:caseMouseBorderLocation.BottomLeft:SetCursor(Cursors.SizeNESW);break;caseMouseBorderLocation.Left:caseMouseBorderLocation.Right:SetCursor(Cursors.SizeWE);break;caseMouseBorderLocation.None:SetCursor(Cursors.Arrow);break;}}elseif(!IsMaxSize()&&MouseButtons==System.Windows.Forms.MouseButtons.Left){//this.SuspendLayout();intshiftx=Cursor.Position.X-_ptMouseDown.X;intshifty=Cursor.Position.Y-_ptMouseDown.Y;intx=_mouseDownBounds.X;inty=_mouseDownBounds.Y;intw=_mouseDownBounds.Width;inth=_mouseDownBounds.Height;intleft=Left;inttop=Top;intwidth=Width;intheight=Height;switch(_mouseLoc){caseMouseBorderLocation.TopLeft:left=x+shiftx;top=y+shifty;width=w-shiftx;height=h-shifty;break;caseMouseBorderLocation.Top:left=x;top=y+shifty;width=w;height=h-shifty;break;caseMouseBorderLocation.TopRight:left=x;top=y+shifty;width=w+shiftx;height=h-shifty;break;caseMouseBorderLocation.Left:left=x+shiftx;top=y;width=w-shiftx;height=h;break;caseMouseBorderLocation.Right:left=x;top=y;width=w+shiftx;height=h;break;caseMouseBorderLocation.BottomLeft:left=x+shiftx;top=y;width=w-shiftx;height=h+shifty;break;caseMouseBorderLocation.Bottom:left=x;top=y;width=w;height=h+shifty;break;caseMouseBorderLocation.BottomRight:left=x;top=y;width=w+shiftx;height=h+shifty;break;}if(width<MinimumSize.Width){width=MinimumSize.Width;}if(height<MinimumSize.Height){height=MinimumSize.Height;}this.Bounds=newRectangle(left,top,width,height);//this.ResumeLayout();}}protectedoverridevoidOnMouseDown(MouseEventArgse){base.OnMouseDown(e);if(!IsMaxSize()&&e.Button==System.Windows.Forms.MouseButtons.Left){_ptMouseDown=Cursor.Position;_mouseDownBounds=this.Bounds;CalcBorderArea();}}privatevoidCalcBorderArea(){//floatx=_borderWidth/2f;_rectBorder=newRectangleF(0,0,Width-1,Height-1);}protectedoverridevoidOnMouseLeave(EventArgse){base.OnMouseLeave(e);ReloadCursor();}privatevoidCalcMousePostion(intx,inty){intb=_borderWidth;if(x<=b){if(y<=b){_mouseLoc=MouseBorderLocation.TopLeft;}elseif(y>b&&y<=Height-2*b){_mouseLoc=MouseBorderLocation.Left;}else{_mouseLoc=MouseBorderLocation.BottomLeft;}}elseif(x>b&&x<Width-2*b){if(y<=b){_mouseLoc=MouseBorderLocation.Top;}elseif(y>b&&y<=Height-2*b){_mouseLoc=MouseBorderLocation.None;}else{_mouseLoc=MouseBorderLocation.Bottom;}}else{if(y<=b){_mouseLoc=MouseBorderLocation.TopRight;}elseif(y>b&&y<=Height-2*b){_mouseLoc=MouseBorderLocation.Right;}else{_mouseLoc=MouseBorderLocation.BottomRight;}}}privateboolIsMaxSize(){returnWindowState==FormWindowState.Maximized;}privatevoidSetCursor(Cursorcur){_isUpdateFormCur=false;Cursor=cur;_isUpdateFormCur=true;}privatevoidReloadCursor(){SetCursor(_cur);}}}
解决方案六:
你要学习下继承。
解决方案七:
1楼和2楼的思路都可行,但是如果你不会的话就尴尬了........另:35还不老好吧?我也35了,但是我还觉得年轻着呢
解决方案八:
35岁初学编程?有毅力
解决方案九:
做成用户控件,比继承什么的要好用。