【问题标题】:C# Click source detectionC#点击源检测
【发布时间】:2014-03-15 12:46:47
【问题描述】:

我有一个 GroupBox,它有多个控件,特别是 4 个 PictureBox

这些是图像拼图的一部分。请注意,图像的数量可能会发生变化。我想允许对它们中的每一个进行拖放效果。为了标记来源和目的地,我做了以下操作:

foreach (var box in boxes)
{
    box.DragEnter += new DragEventHandler(box_DragEnter);
    box.MouseDown += new MouseEventHandler(box_MouseDown);
    box.DragDrop += new DragEventHandler(box_DragDrop);
    box.AllowDrop = true;
}

当一个框被点击时,它将被标记为一个全局变量(这将是源),当一个框引发 DragDrop 事件时,它将成为目标。

正如我之前提到的,由于可以更改框的数量,我不想为每个事件添加单独的方法作为处理程序。

我的问题是我不知道如何“检测”哪个盒子引发了什么事件。

示例:

*如果我按下左上角的图像,我想知道我按下它是为了标记源(假设全局 picSource = 1),如果我放在右下角的图像上,我想知道它是目的地(picDestination = 4)。所有这些都必须发生在处理程序中。 *

测试:

  • 使用 Click 事件添加父面板(与 GroupBox 大小相同)并在事件处理程序提供的 X&Y 发出的 Point 与面板的 X、Y、宽度、高度指定的矩形之间进行比较.
  • 向 GroupBox 添加处理程序(也不起作用)

注意事项:

  • e.X 和 e.Y 指的是鼠标的位置

    void box_MouseDown(object sender, MouseEventArgs e)

  • 这样做的目的是切换框(源目标)

【问题讨论】:

    标签: c# winforms drag-and-drop


    【解决方案1】:

    您可以通过鼠标在控件容器的MouseDownMouseUp事件中的坐标来检测拼图元素,如下所示:

    public partial class PuzzleForm : Form
    {
        private readonly Image[,] Images;
        private readonly int Nx;
        private readonly int Ny;
        private int sourceIndexX;
        private int sourceIndexY;
        private int destinationIndexX;
        private int destinationIndexY;
    
        private PuzzleForm()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            InitializeComponent();
        }
    
        public PuzzleForm(Image[,] images)
            : this()
        {
            Images = images;
            Nx = Images.GetLength(0);
            Ny = Images.GetLength(1);
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
    
            using (Graphics g = e.Graphics)
            {
                for (int j = 0; j < Ny; j++)
                    for (int i = 0; i < Nx; i++)
                    {
                        Rectangle rect = new Rectangle(ClientSize.Width * i / Nx, ClientSize.Height * j / Ny, ClientSize.Width / Nx - 1, ClientSize.Height / Ny - 1);
                        g.DrawImage(Images[i, j], rect);
                    }
            }
        }
    
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
    
            if (e.Button != MouseButtons.Left)
                return;
    
            sourceIndexX = e.X * Nx / ClientSize.Width;
            sourceIndexY = e.Y * Ny / ClientSize.Height;
    
            Cursor = Cursors.Hand;
        }
    
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
    
            if (e.Button != MouseButtons.Left)
                return;
    
            destinationIndexX = e.X * Nx / ClientSize.Width;
            destinationIndexY = e.Y * Ny / ClientSize.Height;
    
            Cursor = Cursors.Default;
    
            if (sourceIndexX != destinationIndexX || sourceIndexY != destinationIndexY)
            {
                swapImages();
                MessageBox.Show(String.Format("From [{0}, {1}] to [{2}, {3}]", sourceIndexX, sourceIndexY, destinationIndexX, destinationIndexY));
            }
        }
    
        private void swapImages()
        {
            Image tmp = Images[sourceIndexX, sourceIndexY];
            Images[sourceIndexX, sourceIndexY] = Images[destinationIndexX, destinationIndexY];
            Images[destinationIndexX, destinationIndexY] = tmp;
            Invalidate();
        }
    }
    

    用法:

    Image[,] images = new Image[2, 2];
    // Fill array with images:
    images[0, 0] = Bitmap.FromFile(@"..."); 
    images[0, 1] = Bitmap.FromFile(@"...");
    images[1, 0] = Bitmap.FromFile(@"...");
    images[1, 1] = Bitmap.FromFile(@"...");
    
    PuzzleForm puzzleForm = new PuzzleForm(images);
    // Show form or whatever you want.
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-17
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多