2017-07-11 111 views
1

我想获得鼠标点击控制坐标。比方说,运行我的应用程序用户点击Visual Studio文件菜单后。所以,我想获得文件菜单控制坐标。这是我试过的代码。我没有成功获得它。 注意:这是windform应用程序。如何在鼠标上获取控制坐标单击? C#

if (ClientRectangle.Contains(PointToClient(Control.MousePosition))) 
{ 
    // MessageBox.Show("Width:" + ClientRectangle.Width.ToString() + "--- Height: " + ClientRectangle.Height.ToString() + "---" + "X:" + ClientRectangle.X.ToString() + "--- Y: " + ClientRectangle.Y.ToString()); 
    Point location = button1.PointToScreen(Point.Empty); 
    MessageBox.Show(location.X.ToString()+"---------"+location.Y.ToString()+"---------"+ location+ClientRectangle.Width.ToString()+"---------"+ClientRectangle.Height.ToString()); 
    // Rectangle rect = new Rectangle(0, 0, 100, 100); 
    int with = ClientRectangle.Width; 
    int height = ClientRectangle.Height; 
    Bitmap bmp = new Bitmap((int)with, (int)height, PixelFormat.Format32bppArgb); 
    Graphics g = Graphics.FromImage(bmp); 
    // g.CopyFromScreen(ClientRectangle.Left, ClientRectangle.Top, 50,50, bmp.Size, CopyPixelOperation.SourceCopy); 
    g.CopyFromScreen(location.X, location.Y, 0, 0, new Size(ClientRectangle.Width-30, ClientRectangle.Height-36)); 
    bmp.Save(@"capture.jpg", ImageFormat.Jpeg) 
} 

任何暗示代码将不胜感激。

+0

在Web应用程序,计算光标x和y位置,并减来自各个位置的div的偏移量x和y。我认为你可以做那样的事情。你的抵消将是控制的立场。不确定菜单,但你可以有一个想法。 – Phiter

+0

什么类型的应用程序? WPF,WinForms,ASP.NET(MVC/WebForms)? –

+0

*“控制鼠标点击坐标”* - 呃,什么?你怎么知道这将是一个“控制”?如果你知道点击了什么,你将如何处理它? – Sinatr

回答

0

你有没有尝试这个办法:如果你想获得一个div或东西在里面的光标位置 Getting mouse position in c#

? 

感谢

+0

此解决方案存在问题。这个解决方案只给出了光标点,这是控制的子部分,我需要获得整个控制。 – mohsin

+0

哇。对于那个很抱歉。我想你有两个选择:控制位置或鼠标钩子。链接1:https://stackoverflow.com/questions/8578654/which-control-has-been-clicked或链接2:https://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-键盘钩子,在-C –