1

I am writing a simulation application that shows mouse movements on screen. I can simply locate controls in the form. But now I need to know the location of "X" button (left top corner) on a form. How do I get that position?

Arash Aghlara
  • 330
  • 3
  • 11
  • 3
    For Vista and above, this [is present in the `WM_GET­TITLE­BAR­INFO­EX` message](http://blogs.msdn.com/b/oldnewthing/archive/2014/05/05/10522553.aspx). For XP and below, it [is more complicated](http://blogs.msdn.com/b/oldnewthing/archive/2014/06/30/10538242.aspx). – Mitch Sep 23 '15 at 19:22
  • Just to make myself clear, by "X" button I mean the button on the form that closes. – Arash Aghlara Sep 23 '15 at 23:20
  • And also, I want it in C# please. – Arash Aghlara Sep 24 '15 at 00:16
  • That's nice. Have you tried writing it yourself instead of using StackOverflow as a code generator? It should be simple work to translate the samples I pointed you towards. (And [searching for a minute or two](http://stackoverflow.com/a/6032327/138200) gets you 90% of the way there) – Mitch Sep 24 '15 at 04:05

2 Answers2

-2

Try this, if I'm understanding correctly.

 int xPos = XButton.Location.X;
 int yPos = XButton.Location.Y;
thewisegod
  • 1,524
  • 1
  • 10
  • 11
  • 2
    Pretty sure the OP means the close button, not a button named "XButton" on the form, although its on the right side not the left side. The right side has the icon and context menu. – Ron Beyer Sep 23 '15 at 19:23
  • 1
    @RonBeyer, I agree, but the close button [is on the left in an RTL layout](http://cdn9.howtogeek.com/wp-content/uploads/2010/04/sshot651.png). I think the OP is from Australia, though, so I would guess he meant right. – Mitch Sep 23 '15 at 19:27
  • @Mitch good point, I don't use RTL layouts very much so I overlooked that. – Ron Beyer Sep 23 '15 at 19:28
-2

You Can Try it

private void show_msg(object source, EventArgs e)
{

    Button b = (Button)source;
    int X = b.Location.X;
    int Y = b.Location.Y;
    MessageBox.Show(X.ToString() + " , " + Y.ToString());

}