winform 移动窗口中的控件,panel
2012-12-23 13:59
176 查看
from:http://www.codeproject.com/Articles/31840/Move-controls-on-a-form-at-runtime
thank the author a lot 。
本文是根据codeproject中的一个程序修改的。
可以实现,窗口中的控件鼠标拖动。really? yes
code:
一个form 。一个类:
form:
效果:
代码:
接下来是用于移动控件的类:
thank the author a lot 。
本文是根据codeproject中的一个程序修改的。
可以实现,窗口中的控件鼠标拖动。really? yes
code:
一个form 。一个类:
form:
效果:
代码:
public partial class Frm_MoveControl : Form { private Cls_MoveControl MoveControl; public Frm_MoveControl() { InitializeComponent(); MoveControl = new Cls_MoveControl(this);//this代表窗口,或者panel等容器 MoveControl.ApplyMove(button2); MoveControl.ApplyMove(button3, Cls_MoveControl.MoveDirection.Vertical); MoveControl.ApplyMove(button1,panel1,Cls_MoveControl.MoveDirection.Any); } }
接下来是用于移动控件的类:
using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace cacheDemo1 { class Cls_MoveControl { public enum MoveDirection { Any, Horizontal, Vertical } private int int_FrmWidth=200; private int int_FrmHeight = 200; public int Int_FrmWidth { get { return int_FrmWidth; } set { int_FrmWidth = value; } } public int Int_FrmHeight { get { return int_FrmHeight; } set { int_FrmHeight = value; } } public Cls_MoveControl(int width,int height) { this.int_FrmWidth = width; this.int_FrmHeight = height; } //这里如果传进来的只是窗口可以用form。当然也可以用Control类,因为Form也是派上于Control类 public Cls_MoveControl(Control frm) { this.int_FrmWidth = frm.ClientRectangle.Width; this.int_FrmHeight = frm.ClientRectangle.Height; frm.Resize += delegate(object sender, EventArgs e) { this.int_FrmWidth = frm.ClientRectangle.Width; this.int_FrmHeight = frm.ClientRectangle.Height; }; } /* * frm.ClientRectangle.Width;这里用ClientRectangle的原因 * 因为ClientRectangle可以得到窗口可用区域的大小, * 可以去掉窗口的标题栏的高度 * 这里写Control的好处是,可以传入Form。也可以穿容器控件,如panel */ public void ApplyMove(Control control) { ApplyMove(control, MoveDirection.Any); } public void ApplyMove(Control control, MoveDirection moveDirection) { ApplyMove(control, control, moveDirection); } public void ApplyMove(Control control, Control container, MoveDirection moveDirection) { bool isDrag = false; Point dragStartPoint = Point.Empty; control.MouseDown += delegate(object sender, MouseEventArgs e) { isDrag = true; dragStartPoint = new Point(e.X, e.Y); control.Cursor = Cursors.SizeAll; control.Capture = true; }; control.MouseUp += delegate(object sender, MouseEventArgs e) { isDrag = false; control.Cursor = Cursors.Default; control.Capture = false; }; control.MouseMove += delegate(object sender, MouseEventArgs e) { if (isDrag) //当鼠标有按下才会为true。一旦鼠标放开就为false { //横向移动的只能横向移动。纵向移动的只能纵向移动。 //任意方向的则都可以移动 if (moveDirection!=MoveDirection.Vertical) //horizontal和any可以进入 { int left=container.Left + e.X - dragStartPoint.X; // container.Left = Math.Max(0, left); //不要超过左边界 container.Left = GetMiddleValue(left,container.Width,this.int_FrmWidth); } if (moveDirection != MoveDirection.Horizontal) { int top = container.Top + e.Y - dragStartPoint.Y; // container.Top = Math.Max(0, top); container.Top = GetMiddleValue(top, container.Height, this.int_FrmHeight); } } }; } /// <summary> /// 控制拖动的控件不要超出窗口 /// </summary> /// <param name="val"></param> /// <param name="containerWOH"></param> /// <param name="FrmWidth"></param> /// <returns></returns> private int GetMiddleValue(int val,int containerWOH,int FrmWOH) { int min = 0; int max = FrmWOH - containerWOH; if (val<min) { val = min; } if (val>max) { val = max; } Console.WriteLine(val); return val; } } }
相关文章推荐
- 关于winform中panel中控件不随滚动条移动的解决办法。
- [VB.net][WinForm]Panel控件移动\鼠标拖动
- C# winform中使用Panel调节窗口变化是各控件的位置
- WinForm------如何修改PanelControl控件背景色
- Winform窗口去边框实现可以移动
- updatepanel控件中弹出子窗口,子窗口操作成功后自动关闭并刷新父页面
- 当去掉WinForm的边框时候,鼠标移动窗口的方法
- ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
- winform窗体中移动控件
- winform实现的仿Msn移动提示信息窗口
- Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
- C# WinForm 使用FlowLayoutPanel控件做为导航菜单按钮的容器
- 移动Panel的滚动条到窗口中间
- ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
- winform移动窗体或窗体中的控件
- 在winform中使用webbrowser控件时,不弹出新的页面,只在当前窗口打开
- Winform窗体可自由移动用户控件
- Winform窗口去边框实现可以移动
- C# winform使用webbrowser控件,不弹出新的页面,只在当前窗口打开
- Winform遍历窗口的所有控件(两种方式)