Solved

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

Posted on 2013-01-17
6
1,107 Views
Last Modified: 2013-01-18
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
Comment
Question by:rkckjk
  • 3
  • 3
6 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38787297
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
 
LVL 2

Author Comment

by:rkckjk
ID: 38787413
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
 
LVL 9

Accepted Solution

by:
sognoct earned 500 total points
ID: 38792238
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Closing Comment

by:rkckjk
ID: 38792263
Thanks!!!! Quick Question could I use the same routine: PerformMouseClick for events on Right click mouse???
0
 
LVL 9

Expert Comment

by:sognoct
ID: 38792287
yes ... I implemented right , left and double click
0
 
LVL 2

Author Comment

by:rkckjk
ID: 38792299
Thanks again for all your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now