繁体   English   中英

将鼠标事件从一个PictureBox控件传递到另一个C#

[英]Pass mouse event from one PictureBox control to another C#

如标题所示,我正在尝试将鼠标事件从一个控件传递到另一个控件。 我对C#还是有点陌生​​,甚至不确定是否可行。 我正在创建一个基本的纸牌游戏,当我在卡座上向下单击鼠标时,我想弹出卡并拖动它而不抬起鼠标按钮。 以下是我的主窗体上的牌组代码

public partial class Form1 : Form
{
    int CardWidth = 63;
    int CardHeight = 88;

    public List<Card> cards = new List<Card>();
    public List<Card> deck = new List<Card>();

    public Form1()
    {
        InitializeComponent();
        loadCards();
    }

    private void loadCards()
    {
        for(int i = 0; i < 10; i++)
        {
            cards.Add(new Card(i*CardWidth, CardHeight * 0, CardWidth, CardHeight,this));
        }

        deck.AddRange(cards);
        updateDeck();
    }

    public void updateDeck()
    {
        Console.WriteLine($"Deck has {deck.Count} cards in it!");
        int min = new[] { 4, deck.Count}.Min();

        if(deck.Count == 1)
        {
            pbDeck.Image = Properties.Resources.CardBackDefault;
        }
        if (deck.Count > 1)
        {
            switch (min)
            {
                case 2:
                    pbDeck.Image = Properties.Resources.Deck2;
                    break;
                case 3:
                    pbDeck.Image = Properties.Resources.Deck3;
                    break;
                case 4:
                    pbDeck.Image = Properties.Resources.Deck4;
                    break;
            }
        }
        if (deck.Count <= 0)
        {
            pbDeck.Image = Properties.Resources.DeckEmpty;
        }
    }

    private void pbDeck_MouseDown(object sender, MouseEventArgs e)
    {
        //Pickup Top card if exists
        if (deck.Count > 0)
        {
            //Get last value in Deck List (a.k.a. Last card placed in Deck
            Card card = deck.Last();

            //Set the card's location to the deck's location
            card.Location = pbDeck.Location;

            //Load the Card
            card.loadCard();

            //Remove the card from the deck
            deck.Remove(card);

            //Update the deck image
            updateDeck();

            card.card_MouseDown(sender, e);

            return;
        }
    }
}

这是卡类

//TODO: May need to be revisited how this value is accessed and modified
public Point Location { get; set; }

//Our Card's visual canvas
public PictureBox card;

//Reference to our main Form
private Form1 Form;

//How we know if the card has been picked up
public bool isMoving = false;

public Card(int X, int Y, int w, int h, Form1 form)
    {
        Location = new Point(X, Y);

        Form = form;

        card = new PictureBox();
        card.Image = Properties.Resources.CardDefaultTemplate;
        card.Location = Location;
        card.Name = "card";
        card.Size = new Size(w, h);
        card.SizeMode = PictureBoxSizeMode.StretchImage;
        card.TabIndex = 0;
        card.TabStop = false;
        card.MouseDown += new MouseEventHandler(card_MouseDown);
        card.MouseMove += new MouseEventHandler(card_MouseMove);
        card.MouseUp += new MouseEventHandler(card_MouseUp);
        card.Paint += new PaintEventHandler(card_Paint);
    }
    public void loadCard()
    {
        //Set the location of the card just in case it's changed
        card.Location = Location;

        //Add this object to the Form
        Form.Controls.Add(card);

        //Bring it in front of all other items on the form
        card.BringToFront();
    }

    public void removeCard()
    {
        //Remove card from form
        Form.Controls.Remove(card);
    }

    public void card_MouseDown(object sender, MouseEventArgs e)
    {
        //Let everyone know we're moving
        isMoving = true;

        //Bring card to frint of screen for visibility
        card.BringToFront();
    }

    public void card_MouseMove(object sender, MouseEventArgs e)
    {
        //If we're moving (a.k.a. Mouse is is down on the card)
        if (isMoving)
        {
            //Move the card center to mouse location
            card.Location = Form.PointToClient(new Point(Cursor.Position.X - (card.Width / 2), Cursor.Position.Y - (card.Height / 2)));
        }
    }

    private void card_MouseUp(object sender, MouseEventArgs e)
    {
        //Save the initial locations of the card
        int setX = card.Location.X;
        int setY = card.Location.Y;

        //Let everyone know we're not moving anymore
        isMoving = false;

        //If the card is past the right boundry
        if(setX+card.Width > Form.Width)
        {
            //Move it back within boundry
            setX = Form.Width - card.Width;
        }
        //If the card is past the left boundry
        else if (setX < 0)
        {
            //Move it back within boundry
            setX = 0;
        }
        //If the card is past the lower boundry
        if (setY+card.Height > Form.Height)
        {
            //Move it back within boundry
            setY = Form.Height-card.Height;
        }
        //If the card is past the upper boundry
        else if (setY < 0)
        {
            //Move it back within boundry
            setY = 0;
        }

        //Set final location
        card.Location = new Point(setX, setY);

        //If the final location is over the deck image on the form
        if (overDeck())
        {
            //Add this card to the deck
            Form.deck.Add(this);
            //Update the deck image
            Form.updateDeck();
            //Remove the card from the form
            removeCard();
        }

    }

    private bool overDeck()
    {
        //If the card's center X value is within the left and right boundries of the deck image on the form
        if (card.Location.X + (card.Width / 2) > Form.pbDeck.Location.X && card.Location.X + (card.Width / 2) < Form.pbDeck.Location.X + Form.pbDeck.Width)
        {
            //If the cards center Y value is within the left and right boundries of the deck image on the form
            if (card.Location.Y + (card.Height / 2) > Form.pbDeck.Location.Y && card.Location.Y + (card.Height / 2) < Form.pbDeck.Location.Y + Form.pbDeck.Height)
            {
                return true;
            }
        }

        //If the above is not true
        return false;
    }

卡从卡座中拖出时,它们会按我的预期拖动,但从卡匣中弹出时却不会。 当前,如果我松开鼠标按钮,它们将跟随光标,并在再次单击时下降,这不是期望的结果

编辑:添加了更多代码以使内容更加清晰。 将标题更改为更具体的标题编辑2:添加了卡片类缺少的变量

我不会试图弄清楚如何将其合并到代码中,但是您想要将鼠标输入转移到另一个控件的属性Control.Capture Property

这是布尔值,可以将要响应移动鼠标的卡片 PictureBox设置为true。 您可以在另一个控件的MouseDown事件处理程序中进行设置。

为了演示,创建一个新的WinForm项目并将Label和PictureBox控件添加到窗体。 连接以下事件处理程序并运行程序。 向下单击标签并拖动鼠标。 PictureBox将跟随光标。

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        pictureBox1.Location = this.PointToClient(pictureBox1.PointToScreen(e.Location));
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        pictureBox1.Capture = true;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM