1

I want to get control coordinates on mouse Clicked. Let's say after running my app user click on Visual Studio File Menu. So, I want to get File Menu control Coordinates. This is the code I have tried. I am not successful getting it. Note: This is windform application.

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)
}

Any Suggestion code will be much appreciated.

mohsin
  • 11
  • 5
  • In web applications when you want to get the cursor position inside a div or something, you calculate the cursor x and y positions, and subtract the offset x and y of the div from the respective positions. I think you could do something like that. Your offset would be the control's position. Not sure about the menus, but you can have an idea. – Phiter Jul 11 '17 at 11:36
  • What type of application? WPF, WinForms, ASP.NET (MVC/WebForms)? – D Hansen Jul 11 '17 at 11:37
  • *"control coordinates on mouse Clicked"* - erm, what? How do you know what that would be a `Control`? And if you get that what was clicked, what are you going to do with it? – Sinatr Jul 11 '17 at 11:37
  • @DHansen this is windform application. – mohsin Jul 11 '17 at 11:42
  • @Sinatr. Whenever Mouse clicked on something. I will try to get controls coordinates. After getting controls coordinates, I will capture a screenshot of that particular control and save it. This app basically belongs to training app. Which is used to train new users of previous steps. – mohsin Jul 11 '17 at 11:45
  • How does screenshot of clicked control helps in training? With your approach it very much depends what is the application what you are clicking. For most you can obtain HWND and if it is a winforms - [get a `Control` from it](https://stackoverflow.com/q/3996547/1997232). But e.g. WPF application required special approach, as their controls doesn't have handles. – Sinatr Jul 11 '17 at 12:12
  • https://stackoverflow.com/questions/8201286/get-cursor-position-with-respect-to-the-control-c-sharp – M Y Jul 11 '17 at 14:40

1 Answers1

0

Have you try this: Getting mouse position in c#

?

Thanks

  • there is a problem with this solution. This solution only gives cursor points which is the sub part of control and I need to get the whole control. – mohsin Jul 11 '17 at 12:17
  • Wow. Sorry about that. I think you have two options:Control Position or Mouse Hook. Link 1: https://stackoverflow.com/questions/8578654/which-control-has-been-clicked or Link 2: https://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C – Ricardo Gonçalves Jul 11 '17 at 12:36