【发布时间】:2018-08-22 09:54:57
【问题描述】:
注意:问题标题和文本已更改。我意识到我根据自己的需要提出了错误的问题。
我有一个无法解决的问题。我在用户控件上有一个图片框:
单击父控件允许我拖动整个控件(我可以单击并将其拖动到 WinForm 上的任意位置)。
我需要能够通过点击并拖动图片框(子控件)来拖动父控件。
不应在父控件内移动图片框。单击子控件并拖动需要移动父控件和子控件,而不改变它们在父控件中的位置。
我尝试将我在网上看到的内容汇总起来,但我遗漏了一些东西。 下面的代码在 WinForm 中有单独的事件,用于处理用户控件和用户控件的子控件。
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
}
【问题讨论】:
-
你的“MouseButtons.Right”在哪里?
-
MouseButtons.Right 功能不是我需要帮助的部分。为了完整起见,我可以添加它。
-
注意:这种方法不起作用,因为它试图将 Parent 和 Child 控件行为链接到 Parent 之外(在错误的偶数范围内)。
标签: c# winforms user-controls mouseevent parent-child