How to call user32.dll API to do the mouse click

1. Add reference to System.Windows.Forms.

2. Add reference to System.Drawings.

3. Create a MouseHook class like below.

 

classMouseHook

{

//used for sending the mouse events like

[DllImport(“user32.dll”)]

privatestaticexternvoid mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);

//constants for the mouse events

//found at http://msdn.microsoft.com/en-us/library/ms646273(v=vs.85).aspx

privateconstUInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;  //The left button was pressed

privateconstUInt32 MOUSEEVENTF_LEFTUP = 0x0004;  //The left button was released.

privateconstUInt32 MOUSEEVENTF_RIGHTDOWN = 0x08;   //The right button was pressed

privateconstUInt32 MOUSEEVENTF_RIGHTUP = 0x10;   //The left button was released.

privateconstUInt32 MOUSEEVENTF_MIDDLEDOWN = 0x0020;  //The middle button was pressed

privateconstUInt32 MOUSEEVENTF_MIDDLEUP = 0x0040;  //The middle button was released.

 

[DllImport(“user32.dll”)]

publicstaticexternshort GetAsyncKeyState(UInt16 virtualKeyCode);

//Virtual key codes

//found at http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx

privateconstUInt16 VK_MBUTTON = 0x04;//middle mouse button

privateconstUInt16 VK_LBUTTON = 0x01;//left mouse button

privateconstUInt16 VK_RBUTTON = 0x02;//right mouse button

 

 

///<summary>

/// Returns negative when the button is DOWN and 0 when the button is UP

///</summary>

///<returns></returns>

privatestaticshort GetMiddleButtonState()

{

  return GetAsyncKeyState(VK_MBUTTON);

}

///<summary>

/// Returns negative when the button is DOWN and 0 when the button is UP

///</summary>

///<returns></returns>

privatestaticshort GetRightButtonState()

{

  return GetAsyncKeyState(VK_RBUTTON);

}

///<summary>

/// Returns negative when the button is DOWN and 0 when the button is UP

///</summary>

///<returns></returns>

privatestaticshort GetLeftButtonState()

{

  return GetAsyncKeyState(VK_LBUTTON);

}

///<summary>

/// Self explanatory

///</summary>

publicstaticbool IsMiddleButtonUp

{

  get

  {

    return (MouseHook.GetMiddleButtonState() == 0);

  }

}

///<summary>

/// Self explanatory

///</summary>

publicstaticbool IsLeftButtonUp

{

  get

  {

    return (MouseHook.GetLeftButtonState() == 0);

  }

}

///<summary>

/// Self explanatory

///</summary>

publicstaticbool IsRightButtonUp

{

  get

  {

    return (MouseHook.GetRightButtonState() == 0);

  }

}

 

///<summary>

/// Sends the Down and Up Events for the Left Mouse Button

///</summary>

publicstaticvoid SendClick()

{

  SendDown();

  SendUp();

}

///<summary>

/// Clicks as a normal user does,

/// if the mouse button is up the button will be pressed

/// if the mouse button is down it will be released

///</summary>

publicstaticvoid SendClickUser()

{

  if (IsLeftButtonUp)

    SendDown();

  else

    SendUp();

}

 

///<summary>

/// Sends the Down and Up Events for the Right Mouse Button

///</summary>

publicstaticvoid SendRightClick()

{

  SendDownRight();

  SendUpRight();

}

///<summary>

/// Clicks as a normal user does,

/// if the mouse button is up the button will be pressed

/// if the mouse button is down it will be released

///</summary>

publicstaticvoid SendRightClickUser()

{

  if (IsRightButtonUp)

    SendDownRight();

  else

    SendUpRight();

}

 

///<summary>

/// Send the event Up for the Left Mouse Button

///</summary>

publicstaticvoid SendUp()

{

  mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());

}

 

///<summary>

/// Send the event Down for the Left Mouse Button

///</summary>

publicstaticvoid SendDown()

{

  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());

}

 

///<summary>

/// Send the event Up for the Right Mouse Button

///</summary>

publicstaticvoid SendUpRight()

{

  mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, new System.IntPtr());

}

 

///<summary>

/// Send the event Down for the Right Mouse Button

///</summary>

publicstaticvoid SendDownRight()

{

  mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, new System.IntPtr());

}

 

 

///<summary>

/// Send the event Up for the Middle Mouse Button

///</summary>

publicstaticvoid SendMiddleUp()

{

  mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, new System.IntPtr());

}

 

///<summary>

/// Send the event Down for the Middle Mouse Button

///</summary>

publicstaticvoid SendMiddleDown()

{

  mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, new System.IntPtr());

}

 

///<summary>

/// Moves the mouse cursor to a point

///</summary>

///<param name=”p”>Point to where the mouse cursor will be moved</param>

publicstaticvoid MoveMouse(System.Drawing.Point p)

{

  System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)p.X, (int)p.Y);

}

 

Example on how to call the mouse click.

 

classProgram

{

  staticvoid Main(string[] args)

  {

    MouseHook.SendClick();

    Console.ReadLine();

  }

}

 

You can also download a single solution from http://skydrive.live.com. The sample file name is ConsoleAppMouseHook.rar. My MSN ID is chanmmn@hotmail.com.

 

Resources:

http://social.msdn.microsoft.com/Forums/en-US/kinectsdk/thread/877f75fa-d74a-4173-a44d-62c3cf1d3baf/#d9373128-5560-472e-8ade-688b5db7907e

 

 

 

 

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s