首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求c#的一个运行中的控件拖拽和控制大小的实例 80分,该如何处理

2012-01-19 
求c#的一个运行中的控件拖拽和控制大小的实例80分在页面写一个panel,用户可以通过按钮来添加lable,然后可

求c#的一个运行中的控件拖拽和控制大小的实例 80分
在页面写一个panel,用户可以通过按钮来添加lable,然后可以通过拖拽的形式来控制大小和位置
最后panel的位置不一定是固定的,用一个类来实现上述的方法。

[解决办法]
参考一下下面的代码吧,按住Control可以调整大小,不按Control键的时候是移动控件:

C# code
private Point lastLocation;private Point lastMsPoint;private Size lastSize;private void label1_MouseDown(object sender, MouseEventArgs e){    this.lastLocation = this.label1.Location;    this.lastSize = this.label1.Size;    this.lastMsPoint = Control.MousePosition;}private void label1_MouseMove(object sender, MouseEventArgs e){    if (e.Button == MouseButtons.Left)    {        Point mp = Control.MousePosition;        if ((ModifierKeys & Keys.Control) == Keys.Control)        {            this.label1.Size = new Size(this.lastSize.Width + mp.X - this.lastMsPoint.X, this.lastSize.Height + mp.Y - this.lastMsPoint.Y);        }        else        {            this.label1.Location = new Point(this.lastLocation.X + mp.X - this.lastMsPoint.X, this.lastLocation.Y + mp.Y - this.lastMsPoint.Y);        }    }} 

热点排行