C# WinForms : 拖放 Parent 通过拖动控制 Child PictureBox 控件
C# WinForms : Drag and Drop Parent Control by Dragging Child PictureBox Control
注意:问题标题和文本已更改。我意识到我问错了问题。
我有一个我无法解决的问题。我在用户控件上有一个图片框:
单击 parent 允许我拖动整个控件(我可以单击并将其拖动到 WinForm 的任何位置)。
我需要能够通过单击并拖动图片框(child 控件)来拖动 parent 控件。
切勿在 parent 控件内移动图片框。单击 child 控件并拖动需要移动 parent 和 children 控件,而不改变它们在 parent 控件内的位置。
我尝试将我在网上看到的内容放在一起,但我遗漏了一些东西。
下面的代码在 WinForm 中有单独的事件,用于处理用户控件和用户控件的 children.
public partial class frmMain : Form {
private Point m_MouseDownLocation;
private bool m_IsDragging;
public frmMain ( ) {
InitializeComponent ( );
suc1.MouseDown += SimpleUserControl_MouseDown;
suc1.MouseMove += SimpleUserControl_MouseMove;
suc1.MouseUp += SimpleUserControl_MouseUp;
suc1.PbxMoveIt.MouseDown += SimpleUserChildControl_MouseDown;
suc1.PbxMoveIt.MouseMove += SimpleUserChildControl_MouseMove;
suc1.PbxMoveIt.MouseUp += SimpleUserChildControl_MouseUp;
}
#region SimpleUserControl Related
private void SimpleUserControl_MouseDown ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
m_MouseDownLocation = e.Location;
m_IsDragging = true;
suc1.DisableButton ( );
}
}
private void SimpleUserControl_MouseMove ( object sender, MouseEventArgs e ) {
int newX;
int newY;
int minX = 10;
int minY = 10;
int maxX = this.Width - (25 + suc1.Width);
int maxY = this.Height - (45 + suc1.Height);
if ( e.Button == MouseButtons.Left ) {
newX = e.X + suc1.Left - m_MouseDownLocation.X;
newY = e.Y + suc1.Top - m_MouseDownLocation.Y;
if ( m_IsDragging ) {
if ( ( newX >= minX ) && ( newX <= maxX ) ) {
suc1.Left = newX;
}
if ( ( newY >= minY ) && ( newY <= maxY ) ) {
suc1.Top = newY;
}
}
}
}
private void SimpleUserControl_MouseUp ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
m_IsDragging = false;
suc1.EnableButton ( );
}
}
#endregion
#region Simple User Child Control Related
private void SimpleUserChildControl_MouseDown ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
if ( e.Button == MouseButtons.Left ) {
m_MouseDownLocation = e.Location;
m_IsDragging = true;
useThis.DisableButton ( );
}
}
private void SimpleUserChildControl_MouseMove ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
int newX;
int newY;
int minX = 10;
int minY = 10;
int maxX = useThis.Width - (25 + useThis.Width);
int maxY = useThis.Height - (45 + useThis.Height);
if ( e.Button == MouseButtons.Left ) {
newX = e.X + useThis.Left - m_MouseDownLocation.X;
newY = e.Y + useThis.Top - m_MouseDownLocation.Y;
if ( m_IsDragging ) {
if ( ( newX >= minX ) && ( newX <= maxX ) ) {
useThis.Left = newX;
}
if ( ( newY >= minY ) && ( newY <= maxY ) ) {
useThis.Top = newY;
}
}
}
if ( e.Button == MouseButtons.Right ) {
MessageBox.Show ( "Right Button Clicked!" );
}
}
private void SimpleUserChildControl_MouseUp ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
if ( e.Button == MouseButtons.Left ) {
m_IsDragging = false;
useThis.EnableButton ( );
}
}
#endregion
}
经过一些挖掘,我找到了答案。由于我在互联网上看到的答案没有解释这些概念,所以我将其包含在答案中。希望这会帮助下一个提问的人。
回答概念:
问题出在子控件的行为(即事件)"catch"事件影响父控件之前。
子控件的行为需要link父控件的行为。这意味着使用子控件中的相关事件来调用父控件中的相同事件。
但是,由于事件需要影响父控件,子控件的代码必须存在于父控件中(参见下面的代码示例)。
考虑这一点的最佳方式是代码必须驻留在 effect 的上下文中。换句话说,如果您想使用子控件代码来影响父控件,则该代码应驻留在父控件代码中。
编码细节:
相关事件有MouseDown、MouseMove 和MouseUp。
link 从子到父的事件:
- 在父代码中为子事件创建代码。
- 将子事件的代码分配给父控件中的子控件
这是在父代码中定义的子事件的示例代码(注意 this.MouseX link 子事件 MouseX 到父事件 MouseX:
public void ChildControl_MouseDown ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Right ) {
MessageBox.Show ( "Huzzah!" );
}
if ( e.Button == MouseButtons.Left ) {
//MessageBox.Show ( "Booyah!" );
this.OnMouseDown ( e );
}
}
public void ChildControl_MouseMove ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
this.OnMouseMove ( e );
}
}
public void ChildControl_MouseUp ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
this.OnMouseUp ( e );
}
}
注意:问题标题和文本已更改。我意识到我问错了问题。
我有一个我无法解决的问题。我在用户控件上有一个图片框:
单击 parent 允许我拖动整个控件(我可以单击并将其拖动到 WinForm 的任何位置)。
我需要能够通过单击并拖动图片框(child 控件)来拖动 parent 控件。
切勿在 parent 控件内移动图片框。单击 child 控件并拖动需要移动 parent 和 children 控件,而不改变它们在 parent 控件内的位置。
我尝试将我在网上看到的内容放在一起,但我遗漏了一些东西。 下面的代码在 WinForm 中有单独的事件,用于处理用户控件和用户控件的 children.
public partial class frmMain : Form {
private Point m_MouseDownLocation;
private bool m_IsDragging;
public frmMain ( ) {
InitializeComponent ( );
suc1.MouseDown += SimpleUserControl_MouseDown;
suc1.MouseMove += SimpleUserControl_MouseMove;
suc1.MouseUp += SimpleUserControl_MouseUp;
suc1.PbxMoveIt.MouseDown += SimpleUserChildControl_MouseDown;
suc1.PbxMoveIt.MouseMove += SimpleUserChildControl_MouseMove;
suc1.PbxMoveIt.MouseUp += SimpleUserChildControl_MouseUp;
}
#region SimpleUserControl Related
private void SimpleUserControl_MouseDown ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
m_MouseDownLocation = e.Location;
m_IsDragging = true;
suc1.DisableButton ( );
}
}
private void SimpleUserControl_MouseMove ( object sender, MouseEventArgs e ) {
int newX;
int newY;
int minX = 10;
int minY = 10;
int maxX = this.Width - (25 + suc1.Width);
int maxY = this.Height - (45 + suc1.Height);
if ( e.Button == MouseButtons.Left ) {
newX = e.X + suc1.Left - m_MouseDownLocation.X;
newY = e.Y + suc1.Top - m_MouseDownLocation.Y;
if ( m_IsDragging ) {
if ( ( newX >= minX ) && ( newX <= maxX ) ) {
suc1.Left = newX;
}
if ( ( newY >= minY ) && ( newY <= maxY ) ) {
suc1.Top = newY;
}
}
}
}
private void SimpleUserControl_MouseUp ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
m_IsDragging = false;
suc1.EnableButton ( );
}
}
#endregion
#region Simple User Child Control Related
private void SimpleUserChildControl_MouseDown ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
if ( e.Button == MouseButtons.Left ) {
m_MouseDownLocation = e.Location;
m_IsDragging = true;
useThis.DisableButton ( );
}
}
private void SimpleUserChildControl_MouseMove ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
int newX;
int newY;
int minX = 10;
int minY = 10;
int maxX = useThis.Width - (25 + useThis.Width);
int maxY = useThis.Height - (45 + useThis.Height);
if ( e.Button == MouseButtons.Left ) {
newX = e.X + useThis.Left - m_MouseDownLocation.X;
newY = e.Y + useThis.Top - m_MouseDownLocation.Y;
if ( m_IsDragging ) {
if ( ( newX >= minX ) && ( newX <= maxX ) ) {
useThis.Left = newX;
}
if ( ( newY >= minY ) && ( newY <= maxY ) ) {
useThis.Top = newY;
}
}
}
if ( e.Button == MouseButtons.Right ) {
MessageBox.Show ( "Right Button Clicked!" );
}
}
private void SimpleUserChildControl_MouseUp ( object sender, MouseEventArgs e ) {
SimpleUserControl useThis = (SimpleUserControl)((Control)sender).Parent;
if ( e.Button == MouseButtons.Left ) {
m_IsDragging = false;
useThis.EnableButton ( );
}
}
#endregion
}
经过一些挖掘,我找到了答案。由于我在互联网上看到的答案没有解释这些概念,所以我将其包含在答案中。希望这会帮助下一个提问的人。
回答概念:
问题出在子控件的行为(即事件)"catch"事件影响父控件之前。
子控件的行为需要link父控件的行为。这意味着使用子控件中的相关事件来调用父控件中的相同事件。
但是,由于事件需要影响父控件,子控件的代码必须存在于父控件中(参见下面的代码示例)。
考虑这一点的最佳方式是代码必须驻留在 effect 的上下文中。换句话说,如果您想使用子控件代码来影响父控件,则该代码应驻留在父控件代码中。
编码细节:
相关事件有MouseDown、MouseMove 和MouseUp。 link 从子到父的事件:
- 在父代码中为子事件创建代码。
- 将子事件的代码分配给父控件中的子控件
这是在父代码中定义的子事件的示例代码(注意 this.MouseX link 子事件 MouseX 到父事件 MouseX:
public void ChildControl_MouseDown ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Right ) {
MessageBox.Show ( "Huzzah!" );
}
if ( e.Button == MouseButtons.Left ) {
//MessageBox.Show ( "Booyah!" );
this.OnMouseDown ( e );
}
}
public void ChildControl_MouseMove ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
this.OnMouseMove ( e );
}
}
public void ChildControl_MouseUp ( object sender, MouseEventArgs e ) {
if ( e.Button == MouseButtons.Left ) {
this.OnMouseUp ( e );
}
}