簡體   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