martedì 4 novembre 2008

Windows Mouse Control

With this class you can easy control the mouse from c#. For the click event it's use the P/Invoke of mouse_event API. To move the cursor positon use directly the managed code.
(Only for x86.)

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MouseMov
{
public class MouseHook
{
[DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;

internal static void SendDoubleClick()
{
SendClick();
SendClick();
}

internal static void SendClick()
{
SendDown();
SendUp();
}

internal static void SendUp()
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}
private void MoveMouse(Point p)
{
Cursor.Position = new Point(Cursor.Position.X + p.X, Cursor.Position.Y + p.Y);
}


internal static void SendDown()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
}
}
}

Have fun.

4 commenti:

Blaine Wood ha detto...

Thank you so much for you class library. I've been looking for a while for exactly what you gave. It was robust enough to clue me in as to where I could add more features yet succinct enough to be clear and concise. Thanks again for a good piece of code.

Blaine

Dr.Luiji ha detto...

You are welcome Blaine.

I've also posted on CodeProject Mobile TouchPad a program that lets you control your PC through your touch pad phones (source inculded) and You can easily connect to your desktop without configuration.

The Desktop side code use this class.

Cheers
Dr.Luiji

Anonimo ha detto...

Hi,
I've got a compilation error stating that the parameter "Point P" of MoveMouse function could not be found. I can't figure it out.Can u help?
-Thanks

Dr.Luiji ha detto...

Hi TRIZDON,
Sorry for the late reply.

Anyway, the class Point is a .Net class, you need to add Namespace System.Drawing (using System.Drawing;) at the top of the class or add the Assembly System.Drawing (in System.Drawing.dll) into your project.
Take a look here.
Have a nice Xmas!
Cheers Dr.Luiji