【问题标题】:C# WinForm - Drag and Drop PictureBox from One GroupBox to Another [duplicate]C# WinForm - 将图片框从一个 GroupBox 拖放到另一个 [重复]
【发布时间】:2018-08-12 15:14:17
【问题描述】:

如果已经问过这个问题,我提前道歉。搜索 stackoverflow 和互联网并没有提供任何有用的示例。

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

我已经为每个似乎与分组框/鼠标交互相关的事件创建了代码(代码见帖子末尾)。

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

界面: 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.

添加代码:

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
    }
}

【问题讨论】:

  • 您是否真的拖放,即使用 DragDrop/Enrter 等事件?如果是这样,为第二个分组框编写事件应该不难。显示您尝试过的代码。请注意,放置操作将涉及将 pbox 添加到 gbox 的 Controls 集合中,并在那里设置合适的位置!
  • @TaW:我确实定义了组框事件。现在他们只是更改主窗体上的更新标签文本。当我知道如何触发事件时,我就可以将其他我想要的功能放入其中。
  • 您没有在 DragEnter 事件中设置效果。请参阅我链接的示例。我也没有看到你打电话给 DoDragDrop。
  • @LarsTech:我的代码基于该示例,但它并没有按照我需要的方式工作。我只是把代码放进去(对不起,我应该早点做的)。

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


【解决方案1】:

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

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();
    }
    }

【讨论】:

  • 不幸的是,这个 Drag 类重现了 groupbox 事件不触发的问题。我需要的是让鼠标在拖动模式下看起来像图片框中的图像。不过,这是一个不同的问题。
猜你喜欢
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
相关资源
最近更新 更多