C#鼠标事件处理程序

时间:2015-06-16 14:42:19

标签: c#

我正在实施纸牌游戏。以下是实施的基本部分:

我有一类卡片:

Public
/*Constructor*/
         Card( string n)
        {
            this.name = n;
            this.image.MouseDown += new MouseEventHandler(this.OnMouseDown);
            this.image.MouseUp += new MouseEventHandler(this.OnMouseUp);
            this.image.MouseMove += new MouseEventHandler(this.OnMouseMove);


        }

Private
PictureBox image;// Keeps a tracker of the image of the card

// Event handlres
            public event MouseEventHandler MouseDown;
            public event MouseEventHandler MouseUp;
            public event MouseEventHandler MouseMove;

private  void OnMouseDown(object sender, MouseEventArgs e) 
        {
            mouseOriginalX = e.X;
            mouseOriginalY = e.Y;
            dragable = true;
        }
        private void OnMouseMove(object sender, MouseEventArgs e)
        {

            if (dragable)
            {
              this.image.Top += e.Y - mouseOriginalY;
              this.image.Left += e.X - mouseOriginalX;
              this.image.BringToFront();
            }
        }

        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            this.image.Top = 200;
            this.image.Left = 150;
            dragable = false;
        }


    Private
    PictureBox image;// Keeps a tracker of the image of the card

    // Event handlres
                public event MouseEventHandler MouseDown;
                public event MouseEventHandler MouseUp;
                public event MouseEventHandler MouseMove;

我还定义了一张卡片列表,其中包含52张卡片实例

public partial class Form1 : Form
    {
        List<Card> allCards = new List<Card>();// To store each card
      // More stuff here

    }

当我运行程序时,我可以轻松地拖放每张卡(Card类的PictureBox)。问题是表单不知道哪个卡被拖放。假设我们在主窗体上有一个名为lblName的标签。每次拖动卡片时,我都想将标签的Text属性(lblName.Text)设置为卡片的name属性。 一般来说,每转一圈就会抛出4张牌。我需要跟踪这四张牌。也许表格中的某个地方有:

publipublic partial class Form1 : Form
    {
        List<Card> PlayedCards = new List<Card>();// Played cards in each turn. Four cards in each turn
      // More stuff here

    }

然后每次抛出一张卡片,我们都可以将卡片添加到上面的列表中。一旦我们抛出4张牌,应用游戏的逻辑并清除此列表并继续下一次运行。 谢谢

1 个答案:

答案 0 :(得分:0)

问题解决了:

  

PictureBox pb =(PictureBox)sender;

     

Form1 frm =(Form1)pb.Parent.GetContainerControl();

然后frm是我的主要形式,包含我需要的所有主要表格