簡體   English   中英

C#單擊源檢測

[英]C# Click source detection

我有一個具有多個控件的GroupBox ,尤其是4 PictureBoxes 在此處輸入圖片說明

這些是圖像拼圖的一部分。 請注意,圖像數量可能會改變。 我希望對每個對象都具有拖放效果。 為了標記源和目標,我執行了以下操作:

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和由面板的X,Y,寬度,高度指定的矩形之間進行比較。
  • 將處理程序添加到GroupBox(也不起作用)

筆記:

  • eX和eY指的是鼠標的位置

    無效的box_MouseDown(對象發送者,MouseEventArgs e)

  • 這樣做的目的是切換框(源<->目標)

您只需在控件容器的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.
...

暫無
暫無

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

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