VS2005 C#中关于usercontrol拖动的问题

各位高手,请教一个问题。VS2005C#中,假定有一个usercontrol1上面有一个botton1,并且在form1中new一个usercontrol1。现在要实现鼠... 各位高手,请教一个问题。VS2005 C#中,假定有一个usercontrol1 上面有一个botton1,并且在form1中new一个usercontrol1。现在要实现鼠标左键点住botton1使整个usercontrol1一起移动,要怎么实现?如何实现使botton1大小点击调整并且usercontrol1也随着调整?谢谢!!
谢谢高手!!移动的问题我自己已经用另一种方法解决了,不过你的也不错。能不能帮我看看怎么进行随动的大小调整啊?就是鼠标点上botton显示调整框,可以拉大拉小,并且usercontrol的大小也随着变。
展开
 我来答
skynomadism
2012-06-25 · TA获得超过319个赞
知道小有建树答主
回答量:410
采纳率:100%
帮助的人:166万
展开全部
//以下是一个自定义的控件,用它可以把任何控件实现点击出现大小调整框。
//它的用法是(以Button为例):
//Button btn = new Button();
//PickBox mPickBox = new PickBox();
//mPickBox.WireControl(btn);
//接下来,你的button只要点击就会变成大小可调
---------------------------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Drawing;
/// <summary>
/// 本类定义了对运行状态下的界面控件进行大小和置即时编辑的函数
/// runtime editing of graphic controls
/// </summary>
public class PickBox
{
//////////////////////////////////////////////////////////////////
// PRIVATE CONSTANTS AND VARIABLES
// 内部变量区
//////////////////////////////////////////////////////////////////

private const int BOX_SIZE = 8;
private Color BOX_COLOR = Color.White;
private ContainerControl m_container;
private Control m_control;
private Label[] lbl = new Label[8];
private int startl;
private int startt;
private int startw;
private int starth;
private int startx;
private int starty;
private bool dragging;
private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,
Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,
Cursors.SizeNESW, Cursors.SizeWE};
private Cursor oldCursor;

private const int MIN_SIZE = 20;

//
// Constructor creates 8 sizing handles & wires mouse events
// to each that implement sizing functions
//
public PickBox()
{
for (int i = 0; i < 8; i++)
{
lbl[i] = new Label();
lbl[i].TabIndex = i;
lbl[i].FlatStyle = 0;
lbl[i].BorderStyle = BorderStyle.FixedSingle;
lbl[i].BackColor = BOX_COLOR;
lbl[i].Cursor = arrArrow[i];
lbl[i].Text = "";
lbl[i].BringToFront();
lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown);
lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove);
lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp);
}
}

//////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// 公共函数区
//////////////////////////////////////////////////////////////////

//
// Wires a Click event handler to the passed Control
// that attaches a pick box to the control when it is clicked
//
public void WireControl(Control ctl)
{
ctl.Click += new EventHandler(this.SelectControl);
}

/////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// 内部函数区
/////////////////////////////////////////////////////////////////

//
// Attaches a pick box to the sender Control
//
private void SelectControl(object sender, EventArgs e)
{

if (m_control is Control)
{
m_control.Cursor = oldCursor;

//Remove event any pre-existing event handlers appended by this class
m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);

m_control = null;
}

m_control = (Control)sender;
//Add event handlers for moving the selected control around
m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp);

//Add sizing handles to Control's container (Form or PictureBox)
for (int i = 0; i < 8; i++)
{
m_control.Parent.Controls.Add(lbl[i]);
lbl[i].BringToFront();
}

//Position sizing handles around Control
MoveHandles();

//Display sizing handles
ShowHandles();

oldCursor = m_control.Cursor;
m_control.Cursor = Cursors.SizeAll;

}

public void Remove()
{
HideHandles();
m_control.Cursor = oldCursor;
}

private void ShowHandles()
{
if (m_control != null)
{
for (int i = 0; i < 8; i++)
{
lbl[i].Visible = true;
}
}
}

private void HideHandles()
{
for (int i = 0; i < 8; i++)
{
lbl[i].Visible = false;
}
}

private void MoveHandles()
{
int sX = m_control.Left - BOX_SIZE;
int sY = m_control.Top - BOX_SIZE;
int sW = m_control.Width + BOX_SIZE;
int sH = m_control.Height + BOX_SIZE;
int hB = BOX_SIZE / 2;
int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,
sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};
int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,
sY + sH-hB, sY + sH-hB, sY + sH / 2};
for (int i = 0; i < 8; i++)
lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
}

/////////////////////////////////////////////////////////////////
// MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL
// 定义了引用到本类中的界面控制的鼠标事件
/////////////////////////////////////////////////////////////////

//
// Store control position and size when mouse button pushed over
// any sizing handle
//
private void lbl_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
startl = m_control.Left;
startt = m_control.Top;
startw = m_control.Width;
starth = m_control.Height;
HideHandles();
}

//
// Size the picked control in accordance with sizing handle being dragged
// 0 1 2
// 7 3
// 6 5 4
//
private void lbl_MouseMove(object sender, MouseEventArgs e)
{
int l = m_control.Left;
int w = m_control.Width;
int t = m_control.Top;
int h = m_control.Height;
if (dragging)
{
switch (((Label)sender).TabIndex)
{
case 0: // Dragging top-left sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
w = startl + startw - m_control.Left;
h = startt + starth - m_control.Top;
break;
case 1: // Dragging top-center sizing box
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
h = startt + starth - m_control.Top;
break;
case 2: // Dragging top-right sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
h = startt + starth - m_control.Top;
break;
case 3: // Dragging right-middle sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
break;
case 4: // Dragging right-bottom sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case 5: // Dragging center-bottom sizing box
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case 6: // Dragging left-bottom sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
w = startl + startw - m_control.Left;
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case 7: // Dragging left-middle sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
w = startl + startw - m_control.Left;
break;
}
l = (l < 0) ? 0 : l;
t = (t < 0) ? 0 : t;
m_control.SetBounds(l, t, w, h);
}
}

//
// Display sizing handles around picked control once sizing has completed
//
private void lbl_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
MoveHandles();
ShowHandles();
}

/////////////////////////////////////////////////////////////////
// MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM
// 定义了环绕在界面控件四周的八个小方块的鼠标事件(进行Size调整时环绕在四周的小方块)
/////////////////////////////////////////////////////////////////

//
// Get mouse pointer starting position on mouse down and hide sizing handles
//
private void ctl_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
startx = e.X;
starty = e.Y;
HideHandles();
}

//
// Reposition the dragged control
//
private void ctl_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
int l = m_control.Left + e.X - startx;
int t = m_control.Top + e.Y - starty;
int w = m_control.Width;
int h = m_control.Height;
l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ?
m_control.Parent.ClientRectangle.Width - w : l);
t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ?
m_control.Parent.ClientRectangle.Height - h : t);
m_control.Left = l;
m_control.Top = t;
}
}

//
// Display sizing handles around picked control once dragging has completed
//
private void ctl_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
MoveHandles();
ShowHandles();
}

}
lionson0819
2012-06-19 · TA获得超过1233个赞
知道小有建树答主
回答量:1041
采纳率:0%
帮助的人:649万
展开全部
定义三个变量
int moving = 0;
int movingX = 0;
int movingY = 0;
在usercontrol的usercontrol_MouseDown事件
private void usercontrol_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
moving = 1;
movingX = e.X;
movingY = e.Y;
}
}
鼠标移动事件
private void usercontrol_MouseMove(object sender, MouseEventArgs e)
{
if (moving == 1)
{
this.palDdhp.Location = new Point(palDdhp.Location.X + (e.X - movingX), palDdhp.Location.Y + (e.Y - movingY));
}
}
鼠标抬起事件
private void usercontrol_MouseUp(object sender, MouseEventArgs e)
{
moving = 0;
movingX = 0;
movingY = 0;
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
miniappolsgnp41eojwd
2012-06-19 · TA获得超过181个赞
知道小有建树答主
回答量:449
采纳率:0%
帮助的人:208万
展开全部
通过改变样式模板是可以实现的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式