Getting Mouse Position in C#

Getting mouse position in c#

You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."

C# Get Mouse Coordinates on the screen

i have solved it with this update code:
i have change the order of some execution and change task with threat.

using System;
using System.Threading;
using System.Windows.Forms;

namespace iMouse
{
public partial class Events : Form
{
public Thread TrackerThread;
public Mutex Checking = new Mutex(false);
public AutoResetEvent Are = new AutoResetEvent(false);
public Events()
{
InitializeComponent();
}
private void Events_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
public void Button3_Click(object sender, EventArgs e)
{
Visible = false;
MouseTracker();
}
private void MouseTracker()
{
if (Checking.WaitOne(10))
{
var ctx = new SynchronizationContext();
Are.Reset();
TrackerThread = new Thread(() =>{
while (true)
{
if (Are.WaitOne(1))
{
break;
}
if (MouseButtons == MouseButtons.Left)
{
ctx.Send(CLickFromOutside, null);
break;
}
}
}
);
TrackerThread.Start();
Checking.ReleaseMutex();
}
}

private void CLickFromOutside(object state)
{
Are.Set();
int X = MousePosition.X;
int Y = MousePosition.Y;
TextBox2.Text = X.ToString();
TextBox3.Text = Y.ToString();
Visible = true;
}
}
}

Get Mouse Cursor position in a C# console app

This program will get X, Y position of mouse on screen each 1 second

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Threading;

namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
while (true)
{
// New point that will be updated by the function with the current coordinates
Point defPnt = new Point();

// Call the function and pass the Point, defPnt
GetCursorPos(ref defPnt);

// Now after calling the function, defPnt contains the coordinates which we can read
Console.WriteLine("X = " + defPnt.X.ToString());
Console.WriteLine("Y = " + defPnt.Y.ToString());
Thread.Sleep(1000);
}
}

// We need to use unmanaged code
[DllImport("user32.dll")]

// GetCursorPos() makes everything possible
static extern bool GetCursorPos(ref Point lpPoint);
}
}

Source

Getting mouse coordinates on mouse click

You were almost there. The problem is that the EventArgs will give you the position relative to the button at the time of the click.

If you want the cursor position instead of the click, you can use the Cursor class to get its Position property:

private void button12_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
// user clicked ok
Point coordinates = Cursor.Position;
MessageBox.Show("Coordinates are: " + coordinates);
}
}

To get the coordinates after the user closed the MessageBox, you can use a timer. In order to do so, you will have to declare one at the class level, set its Tick event and move your cursor login into it.

The button12_Click method will now start the timer, which will show the cursor position once it expires (In this example, after one second).

private Timer timer; //Declare the timer at class level
public Form1()
{
InitializeComponent();
// We set it to expire after one second, and link it to the method below
timer = new Timer {Interval = 1000}; //Interval is the amount of time in millis before it fires
timer.Tick += OnTick;
}

private void OnTick(object sender, EventArgs eventArgs)
{
timer.Stop(); //Don't forget to stop the timer, or it'll continue to tick
Point coordinates = Cursor.Position;
MessageBox.Show("Coordinates are: " + coordinates);
}


private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Pick a position after clicking OK", "OK", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
timer.Start();
}
}

Get Mouse Position from Screen

here is my working code based on this answer:

Also, I have added one more function: move cursor to the random position.

XAML:

<Grid>    
<TextBlock x:Name="tbMouse_X" HorizontalAlignment="Left" Margin="66,31,0,0" VerticalAlignment="Top/>
<TextBlock x:Name="tbMouse_Y" HorizontalAlignment="Left" Margin="66,61,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="tbMouse_A" HorizontalAlignment="Left" Margin="206,31,0,0" VerticalAlignment="Top"/>
<TextBlock x:Name="tbMouse_B" HorizontalAlignment="Left" Margin="206,61,0,0" VerticalAlignment="Top"/>
</Grid>

C#:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += new EventHandler(timer_tick);
dt.Tick += new EventHandler(move_mouse);
dt.Interval = new TimeSpan(0, 0, 0, 0, 10000);
dt.Start();
}

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT pPoint);

[DllImport("user32.dll")]
public static extern bool SetCursorPos(int new_x, int new_y);

private void timer_tick(object sender, EventArgs e)
{
POINT pnt;
GetCursorPos(out pnt);
tbMouse_X.Text = (pnt.X).ToString();
tbMouse_Y.Text = (pnt.Y).ToString();
}

public void move_mouse(object sender, EventArgs e)
{
Random rnd = new Random();
int A = rnd.Next(0, 1000);
int B = rnd.Next(0, 1000);

SetCursorPos(A, B);

tbMouse_A.Text = A.ToString();
tbMouse_B.Text = B.ToString();
}

public struct POINT
{
public int X;
public int Y;

public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
}

How to get the position of a Coordinate with respect to Mouse position?

Based on this How to scale a coordinate system? Now we know the equation. Then I use it this way:

int Distance;
CoordPoint temp = new CoordPoint();
temp.X = MousePosition.X / 660 * Bitmap.Width;
temp.Y = Bitmap.Height - (MousePosition.Y / 440 * Bitmap.Height); // y is flipped

Sample Image

Getting Mouse Position in Status Bar using C# and Wpf

Mouse.GetPosition(displayArea);
Check this

C# Get the mouse position relative to another process Window

Once you have WindowA rectangle, find the offset position of the current cursor position relative to the top left of the WindowA rectangle:

Point offsetA = new Point(Cursor.Position.X - windowARect.Left, Cursor.Position.Y - windowARect.Top);

Now compute the "percentage" of this offset relative to the size (width/height) of WindowA:

double xPct = (double)offsetA.X / (double)(windowARect.Right - windowARect.Left + 1);
double yPct = (double)offsetA.Y / (double)(windowARect.Bottom - windowARect.Top + 1));

Now you can find the "same" spot in WindowB by finding its width/height, multiplying by the "percent", and adding that number to the top left of WindowB:

int xOffsetB = (int)((double)(windowBRect.Right - windowBRect.Left + 1) * xPct);
int yOffsetB = (int)((double)(windowBRect.Bottom - windowBRect.Top + 1) * yPct);
Point offsetB = new Point(windowBRect.Left + xOffsetB, windowBRect.Top + yOffsetB);

Now you can use the X, Y values in offsetB to know where to click in the second window.



Related Topics



Leave a reply



Submit