Click Button Programmably using VB.Net
I needed to click an update button autmatically. I did a quick search and found several answers, this is the one I found worked the best for what I needed.
Private Declare Function GetMenu Lib“user32” (ByVal handle As IntPtr) As IntPtr
Private Declare Function GetSubMenu Lib“user32” (ByVal handle As IntPtr, ByVal nPos AsLong) As IntPtr
Private Declare Function GetMenuItemID Lib“user32”Alias“GetMenuItemID” (ByVal handle As IntPtr, ByVal nPos AsLong) As Integer
Private Declare Function FindWindowEx Lib“user32”Alias“FindWindowExA” (ByVal parent As IntPtr, ByVal child As IntPtr, ByVal className AsString, ByVal caption AsString) As IntPtr
Private Declare Function SendMessage Lib“user32”Alias“SendMessageA” (ByVal handle As IntPtr, ByVal uMsg AsInteger, ByVal wParam AsInteger, ByVal lParam AsInteger) As Integer
Dim wCurrentWindow as IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, “#32770”, “WindowTitle”)
Dim WM_SYSCOMMAND AsInteger = 274
Dim strTitle AsString = proc.MainWindowTitle.ToString()
Dim wNotepadWindow As IntPtr = FindWindowEx(IntPtr.Zero, IntPtr.Zero, “Notepad”, strTitle)IfNot wNotepadWindow.Equals(IntPtr.Zero) Then‘Dim hFileMenu As IntPtr = GetMenu(wNotepadWindow)‘Dim hSubMenu As IntPtr = GetSubMenu(hFileMenu, 0)‘Dim WM_COMMAND As Integer = &H111‘ Dim MenuID As Integer = GetMenuItemID(hSubMenu, 8)‘ Call SendMessage(wNotepadWindow, WM_COMMAND, MenuID, 0&)Dim WM_SYSCOMMAND AsInteger = 274Dim SC_CLOSE AsInteger = 61536
Call SendMessage(wNotepadWindow, WM_SYSCOMMAND, SC_CLOSE, 0)
‘If Not hMenu.Equals(IntPtr.Zero) Then‘ Dim hFileMenu As IntPtr = GetSubMenu(hMenu, 0)‘ Dim hMenuId As IntPtr = GetMenuItemID(hFileMenu, 6)‘ Dim WM_COMMAND As Integer = &HF5‘ Call SendMessage(hFileMenu, WM_COMMAND, hMenuId, 0)‘End If
Dim SC_CLOSE AsInteger = 61536
Call SendMessage(wERAccessWindow, WM_SYSCOMMAND, SC_CLOSE, 0&)