windows 获取被点击的图片框的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5235967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:22:15  来源:igfitidea点击:

get the position of picturebox that has been clicked

c#windowsclickpositionpoint

提问by Farna

I want to get the position of the picturebox that has been cliked by the mouse,but i don't know how?? I mean the position of picturebox not the form that the picturebox on it. thanks.

我想获取鼠标点击的图片框的位置,但我不知道如何??我的意思是图片框的位置而不是图片框的形式。谢谢。

回答by KeithS

MUGAN's close. The Point you'll get from MouseEventArgs is the "screen" point of the mouse, where 0,0 is the top left of the entire monitor or desktop (however you want to think of it). To convert that to a "client" point within the PictureBox control, where 0,0 is the top left of that PictureBox, you'll need to use the Control.PointToClient() method:

MUGAN 快到了。您将从 MouseEventArgs 获得的 Point 是鼠标的“屏幕”点,其中 0,0 是整个显示器或桌面的左上角(无论您想怎么想)。要将其转换为 PictureBox 控件中的“客户端”点,其中 0,0 是该 PictureBox 的左上角,您需要使用 Control.PointToClient() 方法:

private void pb_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
{
    Point mouseDownLocation = (Control)sender.PointToClient(new Point(e.X, e.Y));
    //here goes your if condition ...
}

回答by MUG4N

private void pb_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
{
    Point mouseDownLocation = new Point(e.X, e.Y);
    //here goes your if condition ...
}