• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1148
  • Last Modified:

How to use "SendKeys" with FindWindow with a DropDown control?

I have an application that I'd like to automate the process in using FindWindow to SendKeys to a DropDown control to pick an item from the list in the dropdown.

The X & Y coordinates for the mouse are: 145, 200

Here is what I have so far...

Public Class Form1

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetActiveWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myTest As Long
        myTest = FindWindow(Nothing, "CSS - New Search - Windows Internet Explorer")
        SetActiveWindow(myTest)
      '  SendKeys.Send  ???????
    End Sub


I attached two screen prints to show you the dropdown..
Initial-screen.JPG
DropDown---Personal-Name.jpg
0
Richard Kreidl
Asked:
Richard Kreidl
  • 3
  • 3
1 Solution
 
sognoctCommented:
can use mouse click

Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As IntPtr)

    Private Sub PerformMouseClick(ByVal LClick_RClick_DClick As String, ByVal x As Int32, ByVal y As Int32)
    Const MOUSEEVENTF_LEFTDOWN As Integer = 2
    Const MOUSEEVENTF_LEFTUP As Integer = 4
    Const MOUSEEVENTF_RIGHTDOWN As Integer = 6
    Const MOUSEEVENTF_RIGHTUP As Integer = 8
    If LClick_RClick_DClick = "RClick" Then
      mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, IntPtr.Zero)
    ElseIf LClick_RClick_DClick = "LClick" Then
      mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, IntPtr.Zero)
    ElseIf LClick_RClick_DClick = "DClick" Then
      mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, IntPtr.Zero)
    End If
  End Sub

Open in new window


and use it with
  PerformMouseClick("LClick", x, y)
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
Not Working...

Here's what I got with your code and mine:

Public Class Form1
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetActiveWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myTest As Long
          myTest = FindWindow(Nothing, "CSS - New Search - Windows Internet Explorer")
        SetActiveWindow(myTest)
        PerformMouseClick("LClick", 145, 200)
    End Sub

    Private Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As IntPtr)

    Private Sub PerformMouseClick(ByVal LClick_RClick_DClick As String, ByVal x As Int32, ByVal y As Int32)
        Const MOUSEEVENTF_LEFTDOWN As Integer = 2
        Const MOUSEEVENTF_LEFTUP As Integer = 4
        Const MOUSEEVENTF_RIGHTDOWN As Integer = 6
        Const MOUSEEVENTF_RIGHTUP As Integer = 8
        If LClick_RClick_DClick = "RClick" Then
            mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, IntPtr.Zero)
            mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, IntPtr.Zero)
        ElseIf LClick_RClick_DClick = "LClick" Then
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, IntPtr.Zero)
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, IntPtr.Zero)
        ElseIf LClick_RClick_DClick = "DClick" Then
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, IntPtr.Zero)
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, IntPtr.Zero)
            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, IntPtr.Zero)
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, IntPtr.Zero)
        End If
    End Sub
 End Class
0
 
sognoctCommented:
I had to study mouse_event, initially I though it was easier, but now it works (really funny function)

Private Sub PerformMouseClick(ByVal Operation As String, ByVal x As Int32, ByVal y As Int32)
    Const MOUSEEVENTF_MOVE As Integer = 1
    Const MOUSEEVENTF_LEFTDOWN As Integer = 2
    Const MOUSEEVENTF_LEFTUP As Integer = 4
    Const MOUSEEVENTF_RIGHTDOWN As Integer = 6
    Const MOUSEEVENTF_RIGHTUP As Integer = 8
    Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000
    Dim OnePixelX As Double
    Dim OnePixelY As Double
    Dim xX, yY As Int32
    OnePixelX = 65535 / (Screen.PrimaryScreen.Bounds.Width)
    OnePixelY = 65535 / (Screen.PrimaryScreen.Bounds.Height)
    xX = CInt(Math.Ceiling(OnePixelX * CDbl(x)))
    yY = CInt(Math.Ceiling(OnePixelY * CDbl(y)))

    If Operation = "position" Then
      mouse_event(MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, xX, yY, 0, IntPtr.Zero)
    ElseIf Operation = "Move" Then
      mouse_event(MOUSEEVENTF_MOVE, x, y, 0, IntPtr.Zero)
    ElseIf Operation = "RClick" Then
      mouse_event(MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, xX, yY, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, IntPtr.Zero)
    ElseIf Operation = "LClick" Then
      mouse_event(MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, xX, yY, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero)
    ElseIf Operation = "DClick" Then
      mouse_event(MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, xX, yY, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero)
      mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero)
    End If
  End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Richard KreidlSoftware DeveloperAuthor Commented:
Thanks!!!! Quick Question could I use the same routine: PerformMouseClick for events on Right click mouse???
0
 
sognoctCommented:
yes ... I implemented right , left and double click
0
 
Richard KreidlSoftware DeveloperAuthor Commented:
Thanks again for all your help.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now