-
May 10th, 2007, 10:59 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] SendMessage API to move mouse?
Is it possible to use the SendMessage API to move the mouse to a set x,y coordinates?
Are there any others ways other than...
mouse_event
SetCursorPos
Using VB6 and 2005
Cheers!
-
May 10th, 2007, 11:06 AM
#2
Re: SendMessage API to move mouse?
SetCursorPos works fine for both but why wouldnt you want to use it?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
May 10th, 2007, 11:11 AM
#3
Re: SendMessage API to move mouse?
You can also use the sendinput api If you read on MSDN they have suggested that instead of using mouse_event to use sendinput
This is my sendinput class for keys and mouse movements.
Code:
Imports System.Runtime.InteropServices
Imports Microsoft.DirectX.DirectInput
Namespace WindowsAPI
Public Class User32
#Region " CONSTANTS "
' These had no type and so were objects!!
Public Const INPUT_MOUSE = 0
Public Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Public Const MOUSEEVENTF_LEFTUP As Integer = &H4
Public Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
Public Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
Public Const MOUSEEVENTF_MOVE As Integer = &H1
Public Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
Public Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
Public Const MOUSEEVENTF_RIGHTUP As Integer = &H10
Public Const HC_ACTION As Integer = 0
Public Const LLKHF_EXTENDED As Integer = &H1
Public Const LLKHF_INJECTED As Integer = &H10
Public Const LLKHF_ALTDOWN As Integer = &H20
Public Const LLKHF_UP As Integer = &H80
#End Region
#Region " STRUCTURES "
<StructLayout(LayoutKind.Explicit)> _
Public Structure INPUT
<FieldOffset(0)> Dim dwType As Integer
<FieldOffset(4)> Dim mouseInput As MOUSEINPUT
<FieldOffset(4)> Dim keyboardInput As KEYBDINPUT
<FieldOffset(4)> Dim hardwareInput As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure KEYBDINPUT
<FieldOffset(0)> Public wVk As Short
<FieldOffset(2)> Public wScan As Short
<FieldOffset(4)> Public dwFlags As KEYEVENTF
<FieldOffset(8)> Public time As Integer
<FieldOffset(12)> Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure HARDWAREINPUT
<FieldOffset(0)> Public uMsg As Integer
<FieldOffset(4)> Public wParamL As Short
<FieldOffset(6)> Public wParamH As Short
End Structure
<StructLayout(LayoutKind.Explicit)> _
Public Structure MOUSEINPUT
<FieldOffset(0)> Public dx As Integer
<FieldOffset(4)> Public dy As Integer
<FieldOffset(8)> Public mouseData As Integer
<FieldOffset(12)> Public dwFlags As Integer
<FieldOffset(16)> Public time As Integer
<FieldOffset(20)> Public dwExtraInfo As IntPtr
End Structure
#End Region
#Region " SUBCLASS "
Public Class HookEventArgs
Inherits EventArgs
Public HookCode As Integer
Public wParam As IntPtr
Public lParam As IntPtr
End Class
#End Region
#Region " ENUMERATIONS "
Public Enum HookType : int
WH_JOURNALRECORD = 0
WH_JOURNALPLAYBACK = 1
WH_KEYBOARD = 2
WH_GETMESSAGE = 3
WH_CALLWNDPROC = 4
WH_CBT = 5
WH_SYSMSGFILTER = 6
WH_MOUSE = 7
WH_HARDWARE = 8
WH_DEBUG = 9
WH_SHELL = 10
WH_FOREGROUNDIDLE = 11
WH_CALLWNDPROCRET = 12
WH_KEYBOARD_LL = 13
WH_MOUSE_LL = 14
End Enum
Public Enum Input_Type
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
End Enum
<Flags()> _
Public Enum MOUSEEVENTF As Integer
MOVE = &H1
LEFTDOWN = &H2
LEFTUP = &H4
RIGHTDOWN = &H8
RIGHTUP = &H10
MIDDLEDOWN = &H20
MIDDLEUP = &H40
XDOWN = &H80
XUP = &H100
VIRTUALDESK = &H400
WHEEL = &H800
ABSOLUTE = &H8000
End Enum
<Flags()> _
Public Enum KEYEVENTF As Integer
EXTENDEDKEY = 1
KEYUP = 2
[UNICODE] = 4
SCANCODE = 8
RELEASE = 10
End Enum
Public Enum MOUSEBUTTONS
LEFT
MIDDLE
RIGHT
End Enum
#End Region
#Region " API DECLERATIONS "
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function SendInput(ByVal cInputs As Integer, ByRef pInputs As INPUT, ByVal cbSize As Integer) As Integer
End Function
#End Region
End Class
Public Class Kernal32
End Class
End Namespace
'This is from my input class
Shared Function Press_Mouse_Key(ByVal procID As Integer, ByVal MouseKey As WindowsAPI.User32.MOUSEBUTTONS) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
'FocusGameWindow(procID)
Select Case MouseKey
Case WindowsAPI.User32.MOUSEBUTTONS.LEFT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.MIDDLE
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.RIGHT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
End Select
Return Marshal.GetLastWin32Error > 0
End Function
Shared Function Mouse_Down(ByVal ProcID As Integer, ByVal MouseKey As WindowsAPI.User32.MOUSEBUTTONS) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
'FocusGameWindow(ProcID)
Select Case MouseKey
Case WindowsAPI.User32.MOUSEBUTTONS.LEFT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.MIDDLE
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.RIGHT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTDOWN
WindowsAPI.User32.SendInput(1, input, cbSize)
End Select
Return Marshal.GetLastWin32Error > 0
End Function
Shared Function Mouse_Up(ByVal ProcID As Integer, ByVal MouseKey As WindowsAPI.User32.MOUSEBUTTONS) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
'FocusGameWindow(ProcID)
Select Case MouseKey
Case WindowsAPI.User32.MOUSEBUTTONS.LEFT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_LEFTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.MIDDLE
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_MIDDLEUP
WindowsAPI.User32.SendInput(1, input, cbSize)
Case WindowsAPI.User32.MOUSEBUTTONS.RIGHT
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_RIGHTUP
WindowsAPI.User32.SendInput(1, input, cbSize)
End Select
Return Marshal.GetLastWin32Error > 0
End Function
Shared Function MoveMouse(ByVal ProcID As Integer, ByVal X As Integer, ByVal Y As Integer) As Boolean
Dim input As WindowsAPI.User32.INPUT
input.dwType = WindowsAPI.User32.INPUT_MOUSE
input.mouseInput.dx = X * (65535 / Windows.Forms.Screen.PrimaryScreen.Bounds.Width)
input.mouseInput.dy = Y * (65535 / Windows.Forms.Screen.PrimaryScreen.Bounds.Height)
input.mouseInput.dwFlags = WindowsAPI.User32.MOUSEEVENTF_ABSOLUTE Or WindowsAPI.User32.MOUSEEVENTF_MOVE
WindowsAPI.User32.SendInput(1, input, cbSize)
Return Marshal.GetLastWin32Error > 0
End Function
Last edited by bmahler; May 10th, 2007 at 11:30 AM.
-
May 10th, 2007, 11:13 AM
#4
Thread Starter
Frenzied Member
Re: SendMessage API to move mouse?
Can't I do it with SendMessage or PostMessage? I want to send the mouse move to a specific window
-
May 10th, 2007, 11:16 AM
#5
Re: SendMessage API to move mouse?
I am pretty sure that mouse movents have nothing to do with the window they are on. You can move a mouse over everything, so as long as you are over a specific window when the click event is fired, it will fire on that window. What exactly are you trying to do?
-
May 10th, 2007, 11:21 AM
#6
Thread Starter
Frenzied Member
Re: SendMessage API to move mouse?
thanks for the code, I can't copy and paste it because it's copying the line numbers :|
Could you stick it in a txt please?
Cheers mate
-
May 10th, 2007, 11:31 AM
#7
Re: SendMessage API to move mouse?
There, I changed it to code tags
-
May 10th, 2007, 11:33 AM
#8
Re: SendMessage API to move mouse?
oh, one more thing, I pas sin the processId that I wan to bring in to focus and use AppActivate(ProcessID) to bring it to the front. That is in my focusgamewindow function
-
May 10th, 2007, 11:38 AM
#9
Thread Starter
Frenzied Member
Re: SendMessage API to move mouse?
Ok, I paste the entire code into a blank .vb file, but it underlines the Imports Microsoft.DirectX.DirectInput line and lots of others, is there something missing?
-
May 10th, 2007, 12:20 PM
#10
Re: SendMessage API to move mouse?
Still, why use almost 200+ lines of code when you can do it other ways so easily?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
May 10th, 2007, 03:16 PM
#11
Thread Starter
Frenzied Member
Re: SendMessage API to move mouse?
It's for a game, so SetCursorPos and mouse_event don't work normally.
I figured it out. It wouldn't let me use the ABSOLUTE flag of mouse event, so I have to move it relative to the current pos. So if I get the current pos using GetCursorPos, I can just tell it to move relativley but move negative with the value of the current pos, this always moves the cursor to 0,0. and from there I can move it to the exact x,y just by calling it relatively.
-
May 10th, 2007, 07:03 PM
#12
Re: SendMessage API to move mouse?
Originally Posted by RobDog888
Still, why use almost 200+ lines of code when you can do it other ways so easily?
I use this class for sending keys to a game that uses directinput and for moving the mouse around on that game.
-
May 10th, 2007, 11:10 PM
#13
Re: [RESOLVED] SendMessage API to move mouse?
Oh I see. I never dealt with DX. Always business type apps etc. for me.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|