I'm using this function to move the cursor.
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
When I use a hotkey to trigger it, the cursor will move to the correct coords and next time I move the mouse it continues from that position. Working as intended.
However I need to trigger SetCursorPos during a MouseMove event. If user moves mouse into a certain area I want it to hop to a different place and carry on from there. But right now it hops to the destination and then immediately hops back (90% of the time). How can I avoid that behavior?
Edit: I decided to work around it by clipping the cursor in 1 by 1 px square for 1 mousemove event. Cursor.Clip(MousePosition, new Rectangle(1, 1));
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
My guess is that there is another control on top of your form in the area where you want to trigger the event. If so, the control is capturing the MouseMove
event.
For example, here I've added a green 200x200 panel at position 0, 0 in the upper left hand corner. If the mouse moves over the panel, the form's MouseMove
event will stop capturing the mouse cursor position. In my form's mouse_move
event, I set the form's text to display the mouse coordinates. Notice the coordinates in the Window Text are still 200, 200 when the mouse was actually at 0, 0 (can't see my cursor due to having to click on SnippingTool.exe to get the screenshot).
To remedy this, use the same code you placed in your form's MouseMove
event in the panel's MouseMove
event (or whichever control you are using). This results in the correct coordinates in the form's text.
And here is the code (This could obviously be refactored into a single method):
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = string.Format("X: {0}, Y: {1}", e.X, e.Y);
if (e.X >= 0 && e.X <= 200)
{
if (e.Y >= 0 && e.Y <= 200)
{
SetCursorPos(500, 500);
}
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = string.Format("X: {0}, Y: {1}", e.X, e.Y);
if (e.X >= 0 && e.X <= 200)
{
if (e.Y >= 0 && e.Y <= 200)
{
SetCursorPos(500, 500);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With