这是我用C#编写的小程序的接口。 我希望无论何时在图像上单击鼠标,我都应该能够获得光标的x和y值。
MessageBox.show没有显示鼠标指针的值
namespace ChromaKeyerControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(this.MyMouseDown);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void MyMouseDown(object sender, MouseEventArgs e)
{
bool entered = pictureBox1.ClientRectangle.Contains(pictureBox1.PointToScreen(Cursor.Position));
// if( pictureBox1.ClientRectangle.Contains(pictureBox1.PointToClient(Cursor.Position)))
if (entered)
{
MessageBox.Show(Cursor.Position.ToString());
}
}
}
}
答案 0 :(得分:1)
您需要将鼠标点击与PictureBox
绑定,而不是Form
自己绑定
this.pictureBox1.MouseClick += pictureBox1_MouseClick;
绑定MouseClick
,在事件方法中你可以得到X和Y,然后调用你想要的方法......
public Form1()
{
InitializeComponent();
pictureBox1.MouseClick += pictureBox1_MouseClick;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show(e.X + " - " + e.Y);
CallAnotherMethod(e.X, e.Y);
}