如何获得点击的位置? [英] How to get the position of a Click?

查看:122
本文介绍了如何获得点击的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个游戏,玩家将点击其中一个单位(即图片框),并且一个圆圈将变为可见,玩家的单位在中心。 (圆也是一个图片框)当玩家点击圆圈的图片框时,我需要弄清楚点击的位置是否在圆的半径内。

I'm currently making a game where the player will click on one of his units (which are pictureboxes) and a circle will become visible with the player's unit in the center. (Circle is also a picturebox) When the player clicks on the picturebox of the circle I need to figure out if the position of the click is inside the radius of the circle. My question is how do I get the position of the click?

推荐答案

在点击处理程序中执行:

In click handler do:

   MousePosition.X
   MousePosition.Y

添加示例:

        // 
        // pictureBox1 Init
        // 
        this.pictureBox1.Location = new System.Drawing.Point(1, 1);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(100, 100);
        this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);

..... .....................

..........................................

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(string.Format("X: {0} Y: {1}", MousePosition.X, MousePosition.Y));
    }

显示:X:537 Y:946

Shows: "X: 537 Y: 946"

另一件事:

带坐标的MouseEventArgs只接收MouseUp和MouseDown。

The MouseEventArgs with coordinates receive only MouseUp and MouseDown.

MouseClick无法隐藏您的喜好,因为点击由MouseUp和MouseDown组成,并且两者都可以有不同的坐标。

MouseClick can't recive you cordinates because click consists from MouseUp and MouseDown and it both can have different coordinates.

其他解决方案(最好考虑)

    private int X;
    private int Y;

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(string.Format("X: {0} Y: {1}", X, Y));
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        X = e.X;
        Y = e.Y;
    }

这篇关于如何获得点击的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆