I need to capture the mouse click position outside of form. I am using windows 10, and Visual Studio Professional 2017
In order to capture the mouse click position outside of form I am using the form Deactivate event :
private void Form1_Deactivate(object sender, EventArgs e)
{
txtBox1.Text = "Xpos : " + Cursor.Position.X + " Ypos : " + Cursor.Position.Y;
this.Activate();
}
After a click outside of Form1 window the text box is updated by the new mouse position but Form1 is no more active. That means that a second click outside of Form1 window will not have any effect. The Form1 window label font color changes from black to grey.
The line this.Activate(); shows following behavior (unfortunately it doesn`t work as I need):
//this.Activate(); ->After click outside of Form1 window I see in the windows task bar that Form 1 is not more active
this.Activate(); ->After click outside of Form1 window I see in the task bar that form 1 is still active (Form1 remains marked) sometimes it is flashing orange.
How to obtain the following behavior :
After a click outside of Form1 window, the text box is updated by the new click position, Form1 window remains active and is ready to capture the next click outside of Form1 window. Furthermore Form1 shall always remain in the front.
Please see below my current code:
namespace GetMouseCursor2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Deactivate(object sender, EventArgs e)
{
txtBox1.Text = "Xpos : " + Cursor.Position.X + " Ypos : " + Cursor.Position.Y;
this.Activate();
}
}
}
Many thanks for help in advance. Jan