C# 是否可以通过单击复合控件的一个子控件来移动它?
我试图实现的是通过拖放方法移动一组复合控件,如下图所示: 目前我只能通过复合控件的边缘来实现这一点。甚至可以单击复合控件中的子/子控件来移动整个复合控件吗 以下是我的代码,用于将控件添加到主窗体的C# 是否可以通过单击复合控件的一个子控件来移动它?,c#,winforms,drag-and-drop,C#,Winforms,Drag And Drop,我试图实现的是通过拖放方法移动一组复合控件,如下图所示: 目前我只能通过复合控件的边缘来实现这一点。甚至可以单击复合控件中的子/子控件来移动整个复合控件吗 以下是我的代码,用于将控件添加到主窗体的panel3: private void newPictureBox_Click(object sender, EventArgs e) { UserControl1 _UserControl = new UserControl1(); PictureBox _PictureBox =
panel3
:
private void newPictureBox_Click(object sender, EventArgs e)
{
UserControl1 _UserControl = new UserControl1();
PictureBox _PictureBox = (PictureBox)sender;
string _NewControlClusterName = "_New" + _PictureBox.Name;
_UserControl.Name = _NewControlClusterName;
_UserControl.ThreadCount = 16;
_UserControl.ImageBackground = _PictureBox.BackColor;
_UserControl.Dock = DockStyle.Top;
_UserControl.AllowDrop = true;
_UserControl.Cursor = Cursors.SizeAll;
_UserControl.MouseMove += _UserControl_MouseMove;
_UserControl.DragDrop += _UserControl_DragDrop;
_UserControl.DragEnter += _UserControl_DragEnter;
string ColorName = toolTip1.GetToolTip(_PictureBox);
string ColorCode = toolTip2.GetToolTip(_PictureBox);
toolTip1.SetToolTip(_UserControl.pictureBox1, ColorName);
toolTip2.SetToolTip(_UserControl.pictureBox1, ColorCode);
toolTip2.Active = false;
_UserControl.PictureClick += new EventHandler(ClusterControl_Click);
_UserControl.TrackBarScroll += new EventHandler(GetTartanCode);
panel3.Controls.Add(_UserControl);
panel3.Controls.SetChildIndex(_UserControl, 0);
}
private void _UserControl_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void _UserControl_DragDrop(object sender, DragEventArgs e)
{
UserControl1 target = sender as UserControl1;
if (target != null)
{
int targetIndex = FindUserControlIndex(target);
if (targetIndex != -1)
{
string _UserControlFormat = typeof(UserControl1).FullName;
if (e.Data.GetDataPresent(_UserControlFormat))
{
UserControl1 source = e.Data.GetData(_UserControlFormat) as UserControl1;
int sourceIndex = this.FindUserControlIndex(source);
if (targetIndex != -1)
this.panel3.Controls.SetChildIndex(source, targetIndex);
}
}
}
}
private int FindUserControlIndex(UserControl1 _UserControl)
{
for (int i = 0; i < this.panel3.Controls.Count; i++)
{
UserControl1 target = this.panel3.Controls[i] as UserControl1;
if (_UserControl == target)
return i;
}
return -1;
}
private void _UserControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
UserControl1 _UserControl1 = sender as UserControl1;
_UserControl1.BorderStyle = BorderStyle.Fixed3D;
_UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
}
}
private void ClusterControl_Click(object sender, EventArgs e)
{
PictureBox _PictureBox = (PictureBox)sender;
GroupBox _GroupBox = (GroupBox)_PictureBox.Parent;
UserControl1 _UserControl1 = (UserControl1)_GroupBox.Parent;
panel3.Controls.Remove(_UserControl1);
}
private void newPictureBox\u单击(对象发送方,事件参数e)
{
UserControl1_UserControl=newusercontrol1();
PictureBox\u PictureBox=(PictureBox)发送方;
字符串\u NewControlClusterName=“\u New”+\u PictureBox.Name;
_UserControl.Name=\u NewControlClusterName;
_UserControl.ThreadCount=16;
_UserControl.ImageBackground=\u PictureBox.BackColor;
_UserControl.Dock=DockStyle.Top;
_UserControl.AllowDrop=true;
_UserControl.Cursor=Cursors.SizeAll;
_UserControl.MouseMove+=\u UserControl\u MouseMove;
_UserControl.DragDrop+=\u UserControl\u DragDrop;
_UserControl.DragEnter+=\u UserControl\u DragEnter;
字符串ColorName=toolTip1.GetToolTip(_PictureBox);
字符串ColorCode=toolTip2.GetToolTip(_PictureBox);
toolTip1.SetToolTip(_UserControl.pictureBox1,ColorName);
toolTip2.SetToolTip(_UserControl.pictureBox1,颜色代码);
工具提示2.Active=false;
_UserControl.PictureClick+=新建事件处理程序(ClusterControl\u单击);
_UserControl.TrackBarScroll+=新的事件处理程序(GetTartanCode);
panel3.Controls.Add(_UserControl);
panel3.Controls.SetChildIndex(_UserControl,0);
}
私有void\u UserControl\u DragEnter(对象发送方,DragEventArgs e)
{
e、 效果=DragDropEffects.Move;
}
私有void\u UserControl\u DragDrop(对象发送方,DragEventArgs e)
{
UserControl1 target=发送方为UserControl1;
如果(目标!=null)
{
int targetIndex=FindUserControlIndex(目标);
如果(targetIndex!=-1)
{
字符串_UserControlFormat=typeof(UserControl1).FullName;
if(例如Data.GetDataPresent(_UserControlFormat))
{
UserControl1 source=e.Data.GetData(_UserControlFormat)作为UserControl1;
int sourceIndex=this.FindUserControlIndex(源);
如果(targetIndex!=-1)
this.panel3.Controls.SetChildIndex(源,targetIndex);
}
}
}
}
私有int FindUserControlIndex(UserControl1\u UserControl)
{
for(int i=0;i
没有简单的方法可以做到这一点
您可以尝试将事件处理程序递归地附加到每个子控件。大概是这样的:
public class UserControl1 : Control
{
public UserControl1()
{
// ...
ApplyChildEvents(this);
}
private void ApplyChildEvents(Control control)
{
foreach (Control subcontrol in control.Controls)
{
subcontrol.MouseMove += _UserControl_MouseMove;
subcontrol.DragDrop += _UserControl_DragDrop;
subcontrol.DragEnter += _UserControl_DragEnter;
ApplyChildEvents(subcontrol);
}
}
}
因此,您的
UserControl1
上的所有控件都将调用此方法。使用了上面的答案以及对_UserControl\u MouseMove事件处理程序的以下代码调整,它现在可以完美地工作
private void _UserControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (sender.GetType() == typeof(GroupBox))
{
GroupBox _GroupBox = sender as GroupBox;
UserControl1 _UserControl1 = _GroupBox.Parent as UserControl1;
_UserControl1.BorderStyle = BorderStyle.Fixed3D;
_UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
}
else
{
UserControl1 _UserControl1 = sender as UserControl1;
_UserControl1.BorderStyle = BorderStyle.Fixed3D;
_UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
}
}
}
并且还必须将上述代码更改为以下代码(因为要跟踪的区域是动态生成的):
newPictureBox\u单击
foreach (Control subcontrol in _UserControl.Controls)
{
if (subcontrol.GetType() == typeof(GroupBox)){
subcontrol.MouseMove += _UserControl_MouseMove;
subcontrol.DragDrop += _UserControl_DragDrop;
subcontrol.DragEnter += _UserControl_DragEnter;
}
}
完整代码如下:
private void newPictureBox_Click(object sender, EventArgs e)
{
UserControl1 _UserControl = new UserControl1();
PictureBox _PictureBox = (PictureBox)sender;
string _NewControlClusterName = "_New" + _PictureBox.Name;
_UserControl.Name = _NewControlClusterName;
_UserControl.ThreadCount = 16;
_UserControl.ImageBackground = _PictureBox.BackColor;
_UserControl.Dock = DockStyle.Top;
_UserControl.AllowDrop = true;
_UserControl.Cursor = Cursors.SizeAll;
_UserControl.MouseMove += _UserControl_MouseMove;
_UserControl.DragDrop += _UserControl_DragDrop;
_UserControl.DragEnter += _UserControl_DragEnter;
string ColorName = toolTip1.GetToolTip(_PictureBox);
string ColorCode = toolTip2.GetToolTip(_PictureBox);
toolTip1.SetToolTip(_UserControl.pictureBox1, ColorName);
toolTip2.SetToolTip(_UserControl.pictureBox1, ColorCode);
toolTip2.Active = false;
_UserControl.PictureClick += new EventHandler(ClusterControl_Click);
_UserControl.TrackBarScroll += new EventHandler(GetTartanCode);
foreach (Control subcontrol in _UserControl.Controls)
{
if (subcontrol.GetType() == typeof(GroupBox)){
subcontrol.MouseMove += _UserControl_MouseMove;
subcontrol.DragDrop += _UserControl_DragDrop;
subcontrol.DragEnter += _UserControl_DragEnter;
}
}
panel3.Controls.Add(_UserControl);
panel3.Controls.SetChildIndex(_UserControl, 0);
}
private void _UserControl_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void _UserControl_DragDrop(object sender, DragEventArgs e)
{
UserControl1 target = sender as UserControl1;
if (target != null)
{
int targetIndex = FindUserControlIndex(target);
if (targetIndex != -1)
{
string _UserControlFormat = typeof(UserControl1).FullName;
if (e.Data.GetDataPresent(_UserControlFormat))
{
UserControl1 source = e.Data.GetData(_UserControlFormat) as UserControl1;
int sourceIndex = this.FindUserControlIndex(source);
if (targetIndex != -1)
this.panel3.Controls.SetChildIndex(source, targetIndex);
}
}
}
}
private int FindUserControlIndex(UserControl1 _UserControl)
{
for (int i = 0; i < this.panel3.Controls.Count; i++)
{
UserControl1 target = this.panel3.Controls[i] as UserControl1;
if (_UserControl == target)
return i;
}
return -1;
}
private void _UserControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (sender.GetType() == typeof(GroupBox))
{
GroupBox _GroupBox = sender as GroupBox;
UserControl1 _UserControl1 = _GroupBox.Parent as UserControl1;
_UserControl1.BorderStyle = BorderStyle.Fixed3D;
_UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
}
else
{
UserControl1 _UserControl1 = sender as UserControl1;
_UserControl1.BorderStyle = BorderStyle.Fixed3D;
_UserControl1.DoDragDrop(_UserControl1, DragDropEffects.All);
}
}
}
private void newPictureBox\u单击(对象发送方,事件参数e)
{
UserControl1_UserControl=newusercontrol1();
PictureBox\u PictureBox=(PictureBox)发送方;
字符串\u NewControlClusterName=“\u New”+\u PictureBox.Name;
_UserControl.Name=\u NewControlClusterName;
_UserControl.ThreadCount=16;
_UserControl.ImageBackground=\u PictureBox.BackColor;
_UserControl.Dock=DockStyle.Top;
_UserControl.AllowDrop=true;
_UserControl.Cursor=Cursors.SizeAll;
_UserControl.MouseMove+=\u UserControl\u MouseMove;
_UserControl.DragDrop+=\u UserControl\u DragDrop;
_UserControl.DragEnter+=\u UserControl\u DragEnter;
字符串ColorName=toolTip1.GetToolTip(_PictureBox);
字符串ColorCode=toolTip2.GetToolTip(_PictureBox);
toolTip1.SetToolTip(_UserControl.pictureBox1,ColorName);
toolTip2.SetToolTip(_UserControl.pictureBox1,颜色代码);
工具提示2.Active=false;
_UserControl.PictureClick+=新建事件处理程序(ClusterControl\u单击);
_UserControl.TrackBarScroll+=新的事件处理程序(GetTartanCode);
foreach(用户控件中的控件子控件)
{
if(subcontrol.GetType()==typeof(GroupBox)){
subcontrol.MouseMove+=\u UserControl\u MouseMove;
subcontrol.DragDrop+=\u UserControl\u DragDrop;
subcontrol.DragEnter+=\u用户控制\u DragEnter;
}
}
panel3.Controls.Add(_UserControl);
panel3.Controls.SetChildIndex(_UserControl,0);
}
私有void\u UserControl\u DragEnter(对象发送方,DragEventArgs e)
{
e、 效果=DragDropEffects.All;
}
私有void\u UserControl\u DragDrop(对象发送方,DragEventArgs e)
{
UserControl1 target=发送方为UserControl1;
如果(目标!=null)
{
int targetIndex=FindUserControlIndex(目标);
如果(targetIndex!=-1)
{
字符串_UserControlFormat=typeof(UserControl1).FullName;
if(例如Data.GetDataPresent(_UserControlFormat))
{
UserControl1 source=e.Data.GetData(\u UserControlFor