Main

C# how to get ON SCREEN MOUSE POSITION

Today we're gonna show you how to make a mouse motion tracker in C#. In this tutorial we show you how to get mouse position anywhere on screen, even outside of the form(Window) .Easy for beginners.Best way to get mouse xy coordinates(position) on screen We use Threads, functions(Methods), while loops.while mouse is moving. Free edition (not a trial) of visual studio is: Visual Studio Community Download visual studio: https://visualstudio.microsoft.com/downloads/ Gooogle plus: C# and .Net Developers https://plus.google.com/communities/105633785309529478535

Techimo

5 years ago

Hi guys today I'm gonna show you how to get the mouse position on screen using c# Open visual studeio Create a new projects Ok ou Create a Windows Forms App Let's design the forn using designer Add a panel, and add 4 labels on it these labels are to show the x and y coordinates on screen Declare a new function to create the mouse postion finder,

Comments

@symmetry3515

Good,keep it up.

@techtipstv8278

nice tutorial👍👍👍

@ZarkubFC

download link for the tracker please

@archi_designer

Thank you How to track left and right clicks?

@hihiegirluwu5369

if i wanted to execute a line of code if for example the mouse pointer is at x50 y50 how would i do that pls help me

@solvedplus858

MousePosition is not available in my code , what do i miss?

@matprox910

nice one

@nicklas8853

Hey everyone. I suggest tweaking the Tracker method a bit as it would use lots of CPU when you run it. Add the codes I didn't strike through. Codes: using System.Threading.Tasks; private async void Tracker() { while (true) { int x = MousePosition.X; int y = MousePosition.Y; SetText(labelX, x.ToString()); SetText(labelY, y.ToString()); await Task.Delay(10); // 10 milliseconds } } And if your codes raise an error like this: Cross-thread operation not valid: ... Add those two methods AFTER and NOT INSIDE Tracker() Codes: delegate void SetTextCallback(Label label, string text); private void SetText(Label l ,string text) { if (l.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); Invoke(d, new object[] { l, text }); } else { l.Text = text; } }

@chrisvenn4827

I have the code copied exactly and its not working, i have no errors or warnings, will there be something different in terms of syntax if i was using a different version of visual studio?

@user-iv2em1bu5p

Pls share code

@carlopositivi6656

a 1 linha q escrevo ja da erro o q estou fazendo errado? fica vermelho o Tracker

@LuzuVlogsGamer

Guys if you get infinity code reports in the Output Do a HotReload(Alt+F5) next to the start button. After Click Stop after that run the code again and it should run but if you get that problem it means that the thread is still running in the background and its conflicting also it uses a lot of CPU so use in the Method() with the Position commands this: await Task.Delay(10); // 10 milliseconds This will fix the Background thread problem and the CPU program :O If you use this: trackerThread.IsBackground = true; In the Form_Load1 where the Thread you created is its Only going to Fix the Background Thread Running when you close the program but wont fix your CPU usage problem so just use the Command on top that I pasted. I read some of the comments problems and solutions and wanted to make it clear what works.

@WalterNunez507

add source code ...

@techimo7801

Thanks for watching

@sirjeyo

I'm getting cross-threading exceptions

@stra3947

No code to copy.