Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi CodeScientists!

My project requires to take control of all mouse functions(cursor movements,left click,right click,speed,etc.) after a particular button is clicked,say "takeControlButton".When this button is clicked my application window should get minimized and the cursor movements should be according the "cursor.position.X" and "cursor.position.Y" passed by my application(the points are calculated from a real time tracking of an object,something like camera mouse).

My problem is my little programming knowledge of C# and "user32.dll" APIs. I don't know how to transfer these (x,y) values to "System.Windows.MouseHandler" methods.

Please help...any related article/code snippet/similar projects/forum discussion/....just anything.

Thanks for help in advance!
Posted

1 solution

You need to P/Invoke only one Windows API: SendInput. It provides comprehensive simulation of all mouse and keyboard input exactly as the user and device drivers would do.

I assume you know how to do P/Invoke.

[EDIT]

Just a side note:

Consider improving your event handling technique. This would be more maintainable (but if you like my technique you should not create events using Designer; I still think its better:

C#
MyButton.Click += (sender, eventArgs) => {
   DoWhateverYouWant(); //note: you don't ***have to*** use sender, eventArgs in signature, you just ***can***
};


Also note that Apply_Click violates (good) Microsoft naming conventions.

Here is my explanation why such technique is better the code is more maintainable:
[Solved] How to add Event for C# Control[^].

See also:
how to call keydown event on particular button click[^].

—SA
 
Share this answer
 
v3
Comments
AmarjeetAlien 1-May-11 16:47pm    
Thanks SA! for guiding in exact direction...reading P/Invoke tutorial(http://msdn.microsoft.com/en-us/library/aa446536.aspx).If you know other better link,please do post.

Thanks!
Sergey Alexandrovich Kryukov 1-May-11 17:05pm    
I guess this is all you need. I used to test SendInput and I don't remember any problems, but I remember I had to translate a good deal of Window API declarations to get all structures needed for SendInput. So, if you face any problems feel free to ask another questions

For not, will you formally accept this Solution (green button)?
I'm 100% sure it will do the trick as I tested it before.
--SA
Sergey Alexandrovich Kryukov 1-May-11 17:23pm    
Thank you,
--SA
AmarjeetAlien 1-May-11 17:28pm    
Actually I'm developing an eyemouse for disabled peoples(http://img192.imageshack.us/i/cap1e.png/[^] http://img807.imageshack.us/i/imouse3.png/[^])

So I'm trying to port all mouse properties and functions to my application.I googled a lot and finally found I need to inport "user32.dll"....thanks Code Project that I got the solution.But so far I've been able to implement only "SwapMouseButton" using:

Collapse

[DllImport("user32.dll")]
public static extern Int32 SwapMouseButton(Int32 bSwap);

And
Collapse

private void Apply_Click(object sender, EventArgs e)
{
SwapMouseButton(Convert.ToInt32(BtnChkBoxBC.Checked));
ApplyDisable();
}


There are >700 API calls for user32.dll and I don't know which and all and how to call for mouse only. If you know some site which maintains such database with code snippet(ya,I'm poor at programming),please let me know.

Thanks SA!
Sergey Alexandrovich Kryukov 1-May-11 17:51pm    
I don't know sites like that. Maybe CodeProject and Stackoverflow.com, mostly, but there no regular database or something. On Windows, I almost never use anything beyond MSDN and pretty much always make the code successfully. If my practice demonstrate a success story, why not using my approach?
I would gladly try to help if you ask a concrete question.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900