问题
It is very easy to get the position of cursor out side the form's boundary by just dragaging the mouse it sends many values to the form when ever the position changes, form the following line of code.
MessageBox.Show(Cursor.Position.ToString());
But I need to get the mouse position when user clicked out side the forms boundary. Not by just hovering the mouse. I used the following line of Code to do this:
private void Form1_Deactivate(object sender, EventArgs e)
{
MessageBox.Show(Cursor.Position.ToString());
}
I placed MessageBox.Show(Cursor.Position.ToString()); into forms Deactivate event. When user click outside the form this event definitely occures. But it also sends wrong values when user does not click outside but changes the program by using ALT + TAB key combination. Actually I have to capture screen shot of the area starting from the position of first click. Therefore I need the position of the cursor when it is clicked outside the form. like:
回答1:
You should use Global Mouse Hook
logic to do this.
Here is a good article that will help you: Processing Global Mouse and Keyboard Hooks in C#
回答2:
This might help some one.
Here I am using Windows.Forms.Timer
and two text boxes for displaying [X and Y] cursor positions. On timer tick calling the API GetCursorPos
and getting the cursor position.
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);
public Form1()
{
InitializeComponent();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Point pt = new Point();
GetCursorPos(ref pt);
textBox1.Text = pt.X.ToString();
textBox2.Text = pt.Y.ToString();
}
}
Regards, Ranjeet.
回答3:
The solution to this problem is simple. All what you need is a System.Windows.Forms.Timer, and use DllImport from System.Runtime.InteropService to extern the method GetKeyState from user32.dll. This function has one parameter of any type of integer, and it returns short (Int16). This function can tell you if some keys were pressed or not at every moment and everywhere, not depending on the form. While the Timer is Enabled, you can check if the mouse position is out of the form bounds. There are many ways to get the mouse position. One way is Cursor.Position, or Control.MousePosition, or you can use the bool GetCursorPos(ref Point lpPoint), an extern method, that you can DllImport it from "user32.dll". form.Bounds or form.ClientRectangle, or form.Location and form.Size or form.Left and form.Top and form.Width and form.Height, all these bring you the form bounds. In the timer_Tick function event, you write the following code for example: {
Point mp = Cursor.Position;
Rectangle fb = form.ClientRectangle; //or form.Bounds
if (mp.X < fb.X || mp.Y < fb.Y || mp.X > fb.X + fb.Width || mp.Y > fb.Y + fb.Height)
{
//use GetKeyState from user32.dll to detect if at least 1 key is pressed
//(look at internet how to do it exactly)
//if yes MessageBox.Show("Clicked outside");
}
} If at least 1 key was pressed, and then Show your message with the MessageBox. You can read on the Internet how to do all these stuff I was talking about before, and if you suceed, it will work!
回答4:
to calculate the area instead of typing this code
private void Form1_Deactivate(object sender, EventArgs e)
{
MessageBox.Show(Cursor.Position.ToString());
}
first make a variable named
Point pos1;
and another variable named
Point pos2;
and a Boolean variable named
Boolean b = true;
and two double named
double diffx;
and
double diffy;
and a double for the area named
double area;
all that variables will be in the Form1 class then make this in the Form1_Deactivate
private void Form1_Deactivate(object sender, EventArgs e)
{
if(b)
{
pos1 = Cursor.Position;
b = false;
}
else
{
pos2 = Cursor.Position
if (pos1.x >= pos2.x){
diffx = pos1.x - pos2.x;
}
else
{
diffx = pos2.x - pos1.x;
}
if (pos1.y >= pos2.y){
diffy = pos1.y - pos2.y;
}
else
{
diffy = pos2.y - pos1.y;
}
area = diffx * diffy;
//now display it in the message box by this:
MessageBox.Show(area.ToString());
}
}
but there is one problem that you have to click on the program again after clicking outside it in the start point and then click outside it in the end point to make the program work
回答5:
Deactivate event notifies your form that it's not active any more. It could happen because of many reasons.
Usually a Window gets mouse events only when mouse is over this window. SetCapture function lets you grab all mouse events (I don't know .NET counterpart). Because only one window can capture mouse events, you should not capture mouse events when there's no need for it. The question does not have enough details on what you really want to do and why you need to know when user clicked outside your form.
来源:https://stackoverflow.com/questions/5528543/getting-position-of-mouse-cursor-when-clicked-out-side-the-forms-boundary