Click here to Skip to main content
12,693,757 members (35,558 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
hi friends
i am working with and VS2010 , to develop an application that
* get frames from webcam
* apply some functions and get location of colored object infront of the cam.
* uses this object's location to move cursor, and after i shall use some technique to perform click operations.

i have done much work with it and application can move cursor in desktop explorer and browser etc, even when my application is inactive. for example when my application is minimized it still track the object and moves the pointer.

but problem is that when i run any game like VCOP2 etc then my application dont controll pointer.
plz help me to intract with the game aswell.

i have tried following two methods to move mouse pointer.
Cursor.Position= (any point)

        static extern bool SetCursorPos(int x, int y);
SetCursorPos(xpos, ypos);
Posted 7-Feb-13 4:36am
Member 8973214 27-Feb-13 12:20pm
the application should work like a pointing device.. for instance the application sends a point(x,y) to OS to move the mouse pointer at this location. so that the pointer will move by the application even in the games.
Sergey Alexandrovich Kryukov 27-Feb-13 14:41pm
Then I turned out to be a pretty interesting question. Thank you for clarification.
Member 8973214 28-Feb-13 11:05am
thanks for your time.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Oh, no! You don't need to use P/Invoke as you do. You can use System.Windows.Forms.Cursor for this purpose:[^].

Note that it's bad to use P/Invoke unless it's absolutely unavoidable, because it totally kills your platform compatibility.


I recognize the problem if this API is not working. I explained the possible problem: there is a number of big incompatibilities between older GDI API and DirectX.

Then I have a different idea: instead of manipulating the mouse cursor, simulate mouse input by the function SendInput:[^].

Here, you unfortunately need to use P/Invoke, but you don't need to write it by yourself; it's already done for you:[^].

I tested it: it works very reliably. Actually, this is a low-level input, close to what is used by the hardware driver. You simulate input as it came from hardware, something you probably wanted to have in first place.

Please try it out and give me your feedback. You see, I never use any games, so I would like to here from you if you can solve this problem.

Espen Harlinn 7-Feb-13 18:32pm
Good advice :-D
Thank you, Espen.
Member 8973214 27-Feb-13 12:01pm
thanks SA, sorry for inactivity , it was due to unavailability of internet service.
I am going to try your suggestion.
Member 8973214 27-Feb-13 12:22pm
Sir SAK , I visited your suggested link but I did not get there that how to move pointer in the games. I need more help. any implemented example if you provide plz, which shows how to move mouse pointer?
Sergey Alexandrovich Kryukov 27-Feb-13 13:10pm
Did you ask about setting cursor position? It tells you how to do it, doesn't it?
Member 8973214 27-Feb-13 13:29pm
yeah i asked about cursor movement. i checked your suggested link , but i didnt get a few things there.
what I saw on this link is "Position Property" which can be used to move the cursor.
like Cursor.Position ...
isnt it?
i tried this cursor.position property , and yes it moves the cursor perfectly in all the applications(like notepad, word and all other) , but it do not move the cursor inside a game. what should i do?
actually i want to make my software which will work like a pointing device (like mouse etc).
Sergey Alexandrovich Kryukov 27-Feb-13 13:54pm
Yes, you can do it. What's the problem? What "it do does not move the cursor inside a game" is supposed to mean?
Member 8973214 27-Feb-13 14:02pm
*Sir the software is moving the cursor even when it is minimized (inactive). here no any problem.
* but at at the same time when i play any game it stops moving the cursor.
Member 8973214 27-Feb-13 14:06pm
I think i cant explain the problem. :-)
Member 8973214 27-Feb-13 13:43pm
its an example video that shows how my software works.

same as this video , my software gets location of the object as a Point(x,y), it get this point of color object infront of the camera, even when this software is minimized.

Now what to Move the cursor (mouse pointer) inside a game, (mouse pointer inside any game like VCOP2 etc)
Plz help
Sergey Alexandrovich Kryukov 27-Feb-13 13:52pm
No, thank you, I don't want to see the video. I see from your code that you move the mouse cursor. I've shown how to do it correctly. Do with it all you want.
"Move cursor" programmatically is the same as set cursor position, isn't it? So what else do you need? You can read and set its location, that's all what matters.
Member 8973214 27-Feb-13 13:58pm
Yes dear 100% right ,
and your suggested code works perfect , it moves the cursor on many of the applications.
but problem is that my software do not move cursor when a game is played. this is the problem.
i dont know you are getting me or not.
Sergey Alexandrovich Kryukov 27-Feb-13 14:10pm
Oh, that's bad... What kind is your game? Is it, by any chance, DirectX? Or WPF? Or System.Windows.Forms?
Member 8973214 27-Feb-13 14:19pm
No sir i didnt developed the game, i am running other games. any shooting game like VCOP2, counter strike.
i want to move the pointer with my software , when i play any game. ( means my software interact with the other game and move pointer when i play any game.)
Sergey Alexandrovich Kryukov 27-Feb-13 14:33pm
Oh, I see. They can use DirectX which is unfriendly to GDI+ you are actually using. It's entirely possible. I have another idea...
Member 8973214 27-Feb-13 14:35pm
Oh yeah, Thank your sir, I am waiting for you.
Sergey Alexandrovich Kryukov 27-Feb-13 14:40pm
Please see the updated answer, after [EDIT]. Are you saying that hardware mouse actually work properly, but cursor was no moved programmatically.
Is so, then the new approach I suggest will certainly work for you.
Member 8973214 27-Feb-13 14:50pm
Yes 100%. I'll try it.:) thank you so much.
Member 8973214 28-Feb-13 10:50am
Hi SAK, I tried sendinput, and most of its parts are working fine(clicks etc).

but here is a problem with movement. should I ask here or I post a new Question for that?
Sergey Alexandrovich Kryukov 28-Feb-13 11:31am
First, congratulations! I knew it would work. Thank you for your feedback.

As to the new problem, you can do either way. If the problem is very different, I'll ask you to post a new question. Or, if you prefer, post it right now, but leave me a comment (by commenting on my present comment, "Reply") with the link, so I would be notified and could find that page...

Member 8973214 28-Feb-13 17:09pm
Thanks SAK, thanks for your attention to my problem.
the cursor movement problem is a bit resolved, it moves the cursor now even when i play game (Vcop2), I used SendInput as you suggested.
The example code i followed is here

just a problem is that it is a bit slow movement in the game. may be due to high image processing that is used in my application. I am still working on it, and shall give you feedback about it.

What do you suggest me that multithreads can help me to resolve slow image processing? Because image processing takes most of the time and slows down the software..
Sergey Alexandrovich Kryukov 28-Feb-13 17:16pm
I had hundreds answers on threading and some articles. There are a lot to it. I can just give you all the bookmarked links, not too many, but it would be better if you describe more particular problem.

On key advice: don't create and delete threads: create one extra thread (or more, fixed number of then, one per activity) in the very beginning and use it all the run time. Throttle it with close (non-signaled) EventWaitHandle when not in use. Use it with thread wrapper:

I have a lot of advice on threading, but it's too much to discuss at once. If you have concerns, you can always ask. Only give me a comment in reply to any of my comments...

Member 8973214 28-Feb-13 17:21pm
Okay bro thanks, I'll work on it then shall discus with you. Thanks
Sergey Alexandrovich Kryukov 28-Feb-13 17:59pm
You are always welcome...
Member 8973214 26-Apr-13 14:59pm
Hi SAK Here is another question related to Plz help.
Sergey Alexandrovich Kryukov 26-Apr-13 15:26pm
Yes, I have some idea. Please see my answer...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100