簡體   English   中英

C#WinForms:通過拖動子PictureBox控件拖放父控件

[英]C# WinForms : Drag and Drop Parent Control by Dragging Child PictureBox Control

注意:問題標題和文本已更改。 我意識到我根據自己的需求提出了錯誤的問題。

我有一個我無法解決的問題。 我在用戶控件上有一個圖片框:

用戶控件上的PictureBox(pbxMoveIt)

單擊父級可以拖動整個控件(可以單擊並將其拖動到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
}

經過一些挖掘,我找到了答案。 由於我在互聯網上看到的答案並未解釋這些概念,因此將其包含在答案中。 希望這將有助於下一個提出問題的人。

答案概念:

問題在於子控件的行為(即事件)會在影響父控件之前“捕獲”該事件。

子控件的行為需要將父控件的行為與其鏈接起來。 這意味着使用子控件中的相關事件在父控件中調用同一事件。

但是,由於事件需要影響父控件,因此子控件的代碼必須存在於父控件中(請參見下面的代碼示例)。

想到這一點的最好方法是代碼必須駐留在效果上下文中。 換句話說,如果您想使用“子控件”代碼來實現“父控件”,則所述代碼應駐留在“父控件”代碼中。

編碼細節:

相關事件是MouseDown,MouseMove和MouseUp。 要將事件從孩子鏈接到父母:

  1. 在父代代碼中為子代事件創建代碼。
  2. 將子事件的代碼分配給父級中的子控件

這是在父代碼中定義的子事件的示例代碼(請注意此.MouseX將子事件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 );
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM