2017-07-11 109 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 –