Valhalla Legends Forums Archive | .NET Platform | [vb.net]Sending a click to the active window

AuthorMessageTime
SweatyOgre
How could I send a click to the active window? Someone told me that the SendInput command did it, but I dont quite understand how it works.
December 24, 2004, 10:22 PM
Myndfyr
[code]
Imports System.Runtime.InteropServices

Public Class Mouse

Public Declare Function SendInput Lib "user32" _
        Alias "SendInputW" (Integer nInputs, _
<MarshalAs(UnmanagedType.LPArray)>_
        INPUT pInputs(), _
        Integer cbSize)
) As Integer

End Class

<StructLayout(LayoutKind.Explicit)> _
Public Struct INPUT
  <FieldOffset(0)>_
  Public mi As MOUSEINPUT
  <FieldOffset(0)>_
  Public ki As KEYBOARDINPUT
  <FieldOffset(0)>_
  Public hi As HARDWAREINPUT
End Struct

' Since you only want the mouse input, I'm only converting that one:
<StructLayout(LayoutKind.Explicit)>_
Public Struct MOUSEINPUT
  <FieldOffset(0)>_
  Public dx As Integer
  <FieldOffset(4)>_
  Public dy As Integer
  <FieldOffset(8), MarshalAs(UnmanagedType.U4)>_
  Public mouseData As Integer
  <FieldOffset(12), MarshalAs(UnmanagedType.U4)>_
  Public dwFlags As MouseEventFlags
  <FieldOffset(16), MarshalAs(UnmanagedType.U4)>_
  Public time As Integer
  <FieldOffset(20)>_
  Public dwExtraInfo As IntPtr
End Struct

' I said there is a MouseEventFlags enumeration.  This is based on #defines in winuser.h.  It is:
<Flags()>_
Public Enum MouseEventFlags
  MOUSEEVENTF_MOVE = &H0001    ' mouse move */
  MOUSEEVENTF_LEFTDOWN = &H0002    ' left button down */
  MOUSEEVENTF_LEFTUP = &H0004      ' left button up */
  MOUSEEVENTF_RIGHTDOWN = &H0008      ' right button down */
  MOUSEEVENTF_RIGHTUP = &H0010        ' right button up */
  MOUSEEVENTF_MIDDLEDOWN = &H0020      ' middle button down */
  MOUSEEVENTF_MIDDLEUP = &H0040        ' middle button up */
  MOUSEEVENTF_XDOWN = &H0080        ' x button down */
  MOUSEEVENTF_XUP = &H0100          ' x button down */
  MOUSEEVENTF_WHEEL = &H0800          ' wheel button rolled */
  MOUSEEVENTF_VIRTUALDESK = &H4000          ' map to entire virtual desktop */
  MOUSEEVENTF_ABSOLUTE = &H8000        ' absolute move */
End Enum
[/code]

That is the declaration set for the Windows API in Visual Basic .NET.  If you have questions as to how to use it, refer to the MSDN Library.
December 25, 2004, 3:43 AM

Search