Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code
Imitate a mouse-click through code Imitate a mouse-click through code
Imitate a mouse-click through code
Go Back  Xtreme Visual Basic Talk > > > Imitate a mouse-click through code


Reply
 
Thread Tools Display Modes
  #1  
Old 10-19-2005, 09:32 AM
BulletSponge's Avatar
BulletSponge BulletSponge is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Orlando
Posts: 335
Default Imitate a mouse-click through code


I need a way to left click a coordinate using the X & Y values, but I don't know the API call to do so.

Anyone have any idea how to do this?
Reply With Quote
  #2  
Old 10-19-2005, 10:41 AM
Sonreir's Avatar
Sonreir Sonreir is offline
Contributor
 
Join Date: Jul 2004
Location: Hampshire, England
Posts: 540
Default

The API function you're looking for is SendInput. This is what I've got so far, but for the life of me, I can't get it to work. I keep getting error code 87 (invalid arguements) when I try to use it.

Code:
Declare Function SendInput Lib "user32" (ByVal nCommands As Integer, _ ByVal iCommand As Object, ByVal cSize As Integer) As Integer Declare Function SetCursorPos Lib "user32" (ByVal X As Integer, ByVal Y As Integer) As Integer Public Structure MOUSECOMMAND ' INPUT structure Public iType As Integer ' 0 for mouse Public iDx As Integer ' rel movt in pixels (unless ABSOLUTE) Public iDy As Integer Public iWheelData As Integer ' we don't use this Public iFlags As Integer ' we use this (see MOUSEEVENT flags below) Public iTime As Integer ' don't use this Public iXtra As Integer ' or this End Structure Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2 ' left button down Private Const MOUSEEVENTF_LEFTUP As Integer = &H4 ' left button up Private Sub MoveAndClick(ByVal X As Integer, ByVal Y As Integer) Dim mCOMMAND As MOUSECOMMAND SetCursorPos(X, Y) mCOMMAND.iFlags = MOUSEEVENTF_LEFTDOWN Dim temp As Integer = SendInput(1, mCOMMAND , System.Runtime.InteropServices.Marshal.SizeOf(mCOMMAND )) If temp = 0 Then temp = Err.LastDllError() End If System.Threading.Thread.Sleep(50) mCOMMAND.iFlags = MOUSEEVENTF_LEFTUP temp = SendInput(1, mCOMMAND , System.Runtime.InteropServices.Marshal.SizeOf(mCOMMAND )) End Sub

Maybe you'll have better luck than me.
Also, here's the API reference:
http://msdn.microsoft.com/library/de.../sendinput.asp
Reply With Quote
  #3  
Old 10-19-2005, 12:26 PM
Joker9125 Joker9125 is offline
Freshman
 
Join Date: Sep 2005
Posts: 31
Default

Code:
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long) Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Private Const mouse_eventC = &H2 ' Event contains mouse event record Private Const MOUSE_MOVED = &H1 Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up Private Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down Private Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move Private Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down Private Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up Private Const KEYEVENTF_KEYDOWN As Long = &H0 Private Const KEYEVENTF_KEYUP As Long = &H2 Private Const VK_SHIFT = &H10 Private Const VK_A = &H41 Private Const VK_B = &H42 Private Const VK_C = &H43 Private Const VK_D = &H44 Private Const VK_E = &H45 Private Const VK_F = &H46 Private Const VK_G = &H47 Private Const VK_H = &H48 Private Const VK_I = &H49 Private Const VK_J = &H4A Private Const VK_K = &H4B Private Const VK_L = &H4C Private Const VK_M = &H4D Private Const VK_N = &H4E Private Const VK_O = &H4F Private Const VK_P = &H50 Private Const VK_Q = &H51 Private Const VK_R = &H52 Private Const VK_S = &H53 Private Const VK_T = &H54 Private Const VK_U = &H55 Private Const VK_V = &H56 Private Const VK_W = &H57 Private Const VK_X = &H58 Private Const VK_Y = &H59 Private Const VK_Z = &H5A Private Const VK_0 = &H30 Private Const VK_1 = &H31 Private Const VK_2 = &H32 Private Const VK_3 = &H33 Private Const VK_4 = &H34 Private Const VK_5 = &H35 Private Const VK_6 = &H36 Private Const VK_7 = &H37 Private Const VK_8 = &H38 Private Const VK_9 = &H39 'To move the cursor Cursor.Position = NEW (0, 0) 'To simulate a click: mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 'To simulate keystroke keybd_event VK_(Whatever letter/number you want), 0, 0, 0 'if you want uppercase keybd_event VK_SHIFT, 0, KEYEVENTF_KEYDOWN, 0 keybd_event VK_(Whatever letter/number you want), 0, 0, 0 keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0

Thats all I got.
Reply With Quote
  #4  
Old 10-19-2005, 03:19 PM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

BulletSponge,

Why do you want to do this?
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #5  
Old 10-20-2005, 02:33 AM
Sonreir's Avatar
Sonreir Sonreir is offline
Contributor
 
Join Date: Jul 2004
Location: Hampshire, England
Posts: 540
Default

Joker,
If you're using 2000 or later, you should use the SendInput API, not mouse_event or keybd_event (just to let you know).

(Of course, if you're like me and can't get SendInput to work, you may just settle for what does).
Reply With Quote
  #6  
Old 10-20-2005, 03:15 AM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

Woah... Woah! Stop the presses!! Simply use GetCursorPos! lol...
Code:
'declaration Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As Point) As Integer 'usage Dim RPoint as Point GetCursorPos(RPoint)
Now you have a point object that has an X coordinate and a Y Coordinate

I used it in a timer.. gave me 5 seconds to put my mouse where I wanted.. then the tick event fired the API call and returned the cursor position

You can be snazzy and have your form one color, then maximize and set the color to transparent in code (so you can see the desktop) when you want to locate the position, and when you click, you would still be clicking on the transparent form, thus initializing a form click event that you can use to call the API function...
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...

Last edited by gigemboy; 10-20-2005 at 03:23 AM.
Reply With Quote
  #7  
Old 10-20-2005, 10:00 AM
BulletSponge's Avatar
BulletSponge BulletSponge is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Orlando
Posts: 335
Default

I am trying to expand a Listview that allows subitem edits (as in dropdowns and textboxes).

The code I found only supports mouse clicks.
I went and modified the code to navigate through the use of ARROW keys, but there's a problem.

It works fine except for ONE control, dropdown control (dropdown type - List + plus option for user values).
The Arrow key event fires twice when used to leave the dropdown, and I can only type in a single character.

Pulling the Arrow key code (Protected Overrides Function ProcessCmdKey) makes the controls function correctly....

Hence, adding code to "Click" a position will solve my problem.

I've been pulling my hair out for the past two days trying to get the dropdown controls working correctly through the use of Arrow keys because I need the listview to function like an Excel Spreadsheet.

(I've attached the app just in case someone knows what's going on).
The Text one (typo of test) is the VB interface.
The code for the Listview (in C# is the src zip).
Code for listview is courtesy of http://www.codeproject.com/cs/miscct...ellEditors.asp
Attached Files
File Type: zip ListViewEx_src.zip (31.3 KB, 9 views)
File Type: zip ListViewExText.zip (43.9 KB, 4 views)
Reply With Quote
  #8  
Old 10-20-2005, 10:56 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Sounds like a bug, maybe you should email the author?

(I know, not as much fun as banging it around yourself, but just trying to be practical...)
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #9  
Old 10-20-2005, 02:23 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

If you want to go ahead and try to do the mouse click option, you can imitate a click using the SendMessage API. There are constants to send for the middle mouse button:
Code:
Const WM_MBUTTONDOWN As Integer = &H207 Const WM_MBUTTONUP As Integer = &H208 'Sendmessage declaration (its a little tough trying to get this to work correctly.. I read 'somewhere where 3 different declarations were needed depending on the types of messages they needed to send.. Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _ ByVal hwnd As Integer, _ ByVal wMsg As Integer, _ ByVal wParam As Integer, _ ByRef lParam As Any) As Integer
It takes an hWnd as a parameter, which would be the handle of the control you need to send the click to, then use the SendMessage API twice, one to send the WM_MBUTTONDONW, and WM_MBUTTONUP message. The handle is going to be the tough part. You can use FindWindowEX to return the parent window in the app, but then need to enumerate the child windows using EnumChildWins for all controls until you find the class and name of the control you need....
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...

Last edited by herilane; 10-20-2005 at 02:48 PM. Reason: broke long line
Reply With Quote
  #10  
Old 10-20-2005, 02:58 PM
BulletSponge's Avatar
BulletSponge BulletSponge is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Orlando
Posts: 335
Default

I tried that before, but VB.Net doesn't support the "Any" type.
Reply With Quote
  #11  
Old 10-20-2005, 03:21 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

Change it.. its not "any".. sorry that was a vb6 declaration that I changed real quick.. I've used it before... change lParam to string...
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...
Reply With Quote
  #12  
Old 10-20-2005, 03:25 PM
BulletSponge's Avatar
BulletSponge BulletSponge is offline
Junior Contributor
 
Join Date: Aug 2005
Location: Orlando
Posts: 335
Default

That's where the different declarations come into play. Some require integers, some strings, and yet others require something completely different.

For now, Im focusing on the real problem, the ridiculous mess of a combobox...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code Imitate a mouse-click through code
Imitate a mouse-click through code
Imitate a mouse-click through code
 
Imitate a mouse-click through code
Imitate a mouse-click through code
 
-->