Question : Programming Help - vb.net/C# app to click in another application window using Win API or sendmessage

I have to install a package on a bunch of machines and the package takes forever to install (45+ min).  To make matters worse it pops up with a continue box about every 5 min during the install forcing you to sit around and hit continue.

I want to write a simple program that clicks continue for me so I can automate the install process.  In VB 6 I use to use sendmessage but I can't seem to figure it out in vb.net

Example program structure of what I would like to write:
For each window in openWindowCollection
    If window.title = "Install Window" then
        Window.Sendmessage("Click Continue")
      end if
Next
Sleep()

Can someone help me out with this please?

Answer : Programming Help - vb.net/C# app to click in another application window using Win API or sendmessage

This uses Bob's (TheLearnedOne) code from here:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23357525.html

Instead of a Button you can put the code into a Timer that checks every xxx seconds.

So it might look something like:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1

    Private Const WM_LBUTTONDOWN As Integer = &H201
    Private Const BM_SETSTATE As Integer = &HF3
    Private Const BM_CLICK As Integer = &HF5
    Private Const WM_LBUTTONUP As Integer = &H202
  
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal handle As Int32, ByVal uMsg As Integer, _
         ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim enumerator As New WindowsEnumerator
        For Each top As ApiWindow in enumerator.GetTopLevelWindows()
            If top.MainWindowTitle.ToLower = "install window title here" Then
                For Each child As ApiWindow child in enumerator.GetChildWindows(top.hWnd) 
                    If child.MainWindowTitle.ToLower = "button text here" Then
                        ClickButton(child.hWnd)
                        Exit For
                    End If
                Next child
                Exit For
            End If
        Next top
    End Sub

    Private Sub ClickButton(ByVal handle As Int32)
        Call SendMessage(handle, WM_LBUTTONDOWN, 0, 0)
        Call SendMessage(handle, BM_SETSTATE, 1, 0)
        Call SendMessage(handle, BM_CLICK, 0, 0)
        Call SendMessage(handle, WM_LBUTTONUP, 0, 0)
    End Sub

End Class

Public Class ApiWindow
    Public MainWindowTitle As String = ""
    Public ClassName As String = ""
    Public hWnd As Int32
End Class

'''  
''' Enumerate top-level and child windows 
'''  
'''  
''' Dim enumerator As New WindowsEnumerator()
''' For Each top As ApiWindow in enumerator.GetTopLevelWindows()
'''    Console.WriteLine(top.MainWindowTitle)
'''    For Each child As ApiWindow child in enumerator.GetChildWindows(top.hWnd) 
'''        Console.WriteLine(" " + child.MainWindowTitle)
'''    Next child
''' Next top
'''  
Public Class WindowsEnumerator

    Private Delegate Function EnumCallBackDelegate(ByVal hwnd As Integer, ByVal lParam As Integer) As Integer

    ' Top-level windows.
    Private Declare Function EnumWindows Lib "user32" _
     (ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer

    ' Child windows.
    Private Declare Function EnumChildWindows Lib "user32" _
     (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumCallBackDelegate, ByVal lParam As Integer) As Integer

    ' Get the window class.
    Private Declare Function GetClassName _
     Lib "user32" Alias "GetClassNameA" _
     (ByVal hwnd As Integer, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer

    ' Test if the window is visible--only get visible ones.
    Private Declare Function IsWindowVisible Lib "user32" _
     (ByVal hwnd As Integer) As Integer

    ' Test if the window's parent--only get the one's without parents.
    Private Declare Function GetParent Lib "user32" _
     (ByVal hwnd As Integer) As Integer

    ' Get window text length signature.
    Private Declare Function SendMessage _
     Lib "user32" Alias "SendMessageA" _
     (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

    ' Get window text signature.
    Private Declare Function SendMessage _
     Lib "user32" Alias "SendMessageA" _
     (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As StringBuilder) As Int32

    Private _listChildren As New List(Of ApiWindow)
    Private _listTopLevel As New List(Of ApiWindow)

    Private _topLevelClass As String = ""
    Private _childClass As String = ""

    ''' 
    ''' Get all top-level window information
    ''' 
    ''' List of window information objects
    Public Overloads Function GetTopLevelWindows() As List(Of ApiWindow)

        EnumWindows(AddressOf EnumWindowProc, &H0)

        Return _listTopLevel

    End Function

    Public Overloads Function GetTopLevelWindows(ByVal className As String) As List(Of ApiWindow)

        _topLevelClass = className

        Return Me.GetTopLevelWindows()

    End Function

    ''' 
    ''' Get all child windows for the specific windows handle (hwnd).
    ''' 
    ''' List of child windows for parent window
    Public Overloads Function GetChildWindows(ByVal hwnd As Int32) As List(Of ApiWindow)

        ' Clear the window list.
        _listChildren = New List(Of ApiWindow)

        ' Start the enumeration process.
        EnumChildWindows(hwnd, AddressOf EnumChildWindowProc, &H0)

        ' Return the children list when the process is completed.
        Return _listChildren

    End Function

    Public Overloads Function GetChildWindows(ByVal hwnd As Int32, ByVal childClass As String) As List(Of ApiWindow)

        ' Set the search
        _childClass = childClass

        Return Me.GetChildWindows(hwnd)

    End Function

    ''' 
    ''' Callback function that does the work of enumerating top-level windows.
    ''' 
    ''' Discovered Window handle
    ''' 1=keep going, 0=stop
    Private Function EnumWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32

        ' Eliminate windows that are not top-level.
        If GetParent(hwnd) = 0 AndAlso CBool(IsWindowVisible(hwnd)) Then

            ' Get the window title / class name.
            Dim window As ApiWindow = GetWindowIdentification(hwnd)

            ' Match the class name if searching for a specific window class.
            If _topLevelClass.Length = 0 OrElse window.ClassName.ToLower() = _topLevelClass.ToLower() Then
                _listTopLevel.Add(window)
            End If

        End If

        ' To continue enumeration, return True (1), and to stop enumeration 
        ' return False (0).
        ' When 1 is returned, enumeration continues until there are no 
        ' more windows left.

        Return 1

    End Function

    ''' 
    ''' Callback function that does the work of enumerating child windows.
    ''' 
    ''' Discovered Window handle
    ''' 1=keep going, 0=stop
    Private Function EnumChildWindowProc(ByVal hwnd As Int32, ByVal lParam As Int32) As Int32

        Dim window As ApiWindow = GetWindowIdentification(hwnd)

        ' Attempt to match the child class, if one was specified, otherwise
        ' enumerate all the child windows.
        If _childClass.Length = 0 OrElse window.ClassName.ToLower() = _childClass.ToLower() Then
            _listChildren.Add(window)
        End If

        Return 1

    End Function

    ''' 
    ''' Build the ApiWindow object to hold information about the Window object.
    ''' 
    Private Function GetWindowIdentification(ByVal hwnd As Integer) As ApiWindow

        Const WM_GETTEXT As Int32 = &HD
        Const WM_GETTEXTLENGTH As Int32 = &HE

        Dim window As New ApiWindow()

        Dim title As New StringBuilder()

        ' Get the size of the string required to hold the window title.
        Dim size As Int32 = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0)

        ' If the return is 0, there is no title.
        If size > 0 Then
            title = New StringBuilder(size + 1)

            SendMessage(hwnd, WM_GETTEXT, title.Capacity, title)
        End If

        ' Get the class name for the window.
        Dim classBuilder As New StringBuilder(64)
        GetClassName(hwnd, classBuilder, 64)

        ' Set the properties for the ApiWindow object.
        window.ClassName = classBuilder.ToString()
        window.MainWindowTitle = title.ToString()
        window.hWnd = hwnd

        Return window

    End Function

End Class
Random Solutions  
 
programming4us programming4us