C#WinForm - 将PictureBox从一个GroupBox拖放到另一个GroupBox

时间:2018-03-05 01:54:25

标签: c# winforms drag-and-drop picturebox groupbox

如果已经提出要求,我会事先道歉。搜索stackoverflow和互联网并没有提供任何有用的例子。

在正在运行的程序中,我有一个图片框,我需要能够从一个GroupBox单击并拖动到另一个GroupBox。我知道如何在WinForm本身周围拖动图片框(即没有涉及任何GroupBox)。 我找不到任何如何做到这一点的例子。

我为组合框/鼠标交互相关的每个事件创建了代码(请参阅代码的结尾)。

注意:感谢您对需要代码的反馈。我是新的Stack Overflow。

INTERFACE: Initial Interface

问题: 当我在组框上单击并拖动pbxMoveIt时,组框事件永远不会根据状态标签中的文本触发。

Dragging image over group box doesn't trigger group box enter, hover or other events

当我在不拖动任何内容的情况下移动鼠标时,组框鼠标悬停事件将触发。

Group box mouse hover event fires when not dragging anything.

ADDED代码:

namespace MoveControlsOnFormBetweenGroupBoxes {
    public partial class frmMain : Form {

        private Point m_MouseDownLocation;
        private bool m_IsDragging;

        public frmMain ( ) {
            InitializeComponent ( );

            pbxMoveIt.BringToFront ( );
            gbx1.AllowDrop = true;
            gbx2.AllowDrop = true;
            lblStatus.Text = "GUI Status: Started";
        }

        #region Picture Box Related Methods
        // Picture Related Methods
        private void pbxMoveIt_MouseDown ( object sender, MouseEventArgs e ) {
            lblStatus.Text = "GUI Status: pbxMoveIt - MouseDown";
            if ( e.Button == MouseButtons.Left ) {
                m_MouseDownLocation = e.Location;
                m_IsDragging = true;
            }
        }
        private void pbxMoveIt_MouseMove ( object sender, MouseEventArgs e ) {
            int newX;
            int newY;
            int minX = 10;
            int minY = 10;
            int maxX = this.Width - (25 + pbxMoveIt.Width);
            int maxY = this.Height - (45 + pbxMoveIt.Height);
            if ( e.Button == MouseButtons.Left ) {
                lblStatus.Text = "GUI Status: pbxMoveIt - MouseMove";
                newX = e.X + pbxMoveIt.Left - m_MouseDownLocation.X;
                newY = e.Y + pbxMoveIt.Top - m_MouseDownLocation.Y;
                if ( m_IsDragging ) {
                    if ( ( newX >= minX ) && ( newX <= maxX ) ) {
                        pbxMoveIt.Left = newX;
                    }
                    if ( ( newY >= minY ) && ( newY <= maxY ) ) {
                        pbxMoveIt.Top = newY;
                    }
                }
            }
        }
        private void pbxMoveIt_MouseUp ( object sender, MouseEventArgs e ) {
            lblStatus.Text = "GUI Status: pbxMoveIt - MouseUp";
            if ( e.Button == System.Windows.Forms.MouseButtons.Left ) {
                m_IsDragging = false;
            }
        }
        #endregion

        #region Group Box Related
        // Group Box Related Methods
        private string Gbx_Title ( object sender ) {
            string boxTitle = "Unknown";
            if ( sender == gbx1 ) {
                boxTitle = "Group Box 1";
            }
            if ( sender == gbx2 ) {
                boxTitle = "Group Box 2";
            }
            return boxTitle;
        }
        private void gbx_DragDrop ( object sender, DragEventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragDrop", Gbx_Title ( sender ) );
        }
        private void gbx_DragEnter ( object sender, DragEventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragEnter", Gbx_Title ( sender ) );
        }
        private void gbx_DragLeave ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragLeave", Gbx_Title ( sender ) );
        }

        private void gbx_DragOver ( object sender, DragEventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - DragOver", Gbx_Title ( sender ) );
        }

        private void gbx_Enter ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - Enter", Gbx_Title ( sender ) );
        }

        private void gbx_Leave ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - Leave", Gbx_Title ( sender ) );
        }

        private void gbx_MouseHover ( object sender, EventArgs e ) {
            lblStatus.Text = String.Format ( "GUI Status: {0} - MouseHover", Gbx_Title ( sender ) );
        }
        #endregion
    }
}

1 个答案:

答案 0 :(得分:0)

将我的课程添加到您的项目中

using System;
using System.Windows.Forms;

namespace Hector.Framework.Utils
{
    public class Drag : Form
    {
        private bool isDraggable;
        private Control target;
        private int a, b;

        public Drag(){}

        public void Grab(Control control)
        {
            try
            {
                this.isDraggable = true;
                this.target = control;
                this.a = Control.MousePosition.X - this.target.Left;
                this.b = Control.MousePosition.Y - this.target.Top;
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void MoveObject(bool Horizontal = true, bool Vertical = true)
        {
            try
            {
                if (this.isDraggable)
                {
                    int x = Control.MousePosition.X,
                        y = Control.MousePosition.Y;

                    if (Horizontal)
                    {
                        this.target.Left = x - this.a;
                    }
                    if (Vertical)
                    {
                        this.target.Top = y - this.b;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void Release()
        {
            this.isDraggable = false;
        }
    }
}

然后,调用PictureBox事件

    public partial class Form1 : Form
    {
    private Hector.Framework.Utils.Drag drag = new Hector.Framework.Utils.Drag();

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
       drag.Grab(pictureBox);
    }

    private void pictureBox_MouseUp(object sender, MouseEventArgs e)
    {
       drag.Release();
    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
       drag.MoveObject();
    }
    }
相关问题