By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

Click Button In Another Application

P: 2
Hello All ..
I'm Trying to Click a Button In Another Application With Process Name "CenterBot"
I have got ListView1 , Button1 , Button2
Here's my code

Expand|Select|Wrap|Line Numbers
  1. Imports System.Runtime.InteropServices
  2. Public Class Form1
  3.  
  4.  Private Const BM_CLICK = &HF5
  5.     Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As IntPtr, ByVal wMsg As UInteger, ByVal wParam As UInteger, ByVal lParam As UInteger) As UInteger
  6.     Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Integer, ByVal lpWindowName As String) As Int32
  7.  
  8.     Private Declare Auto Function FindWindowEx Lib "user32.dll" ( _
  9.     ByVal hWndParent As IntPtr, _
  10.     ByVal hWndChildAfter As IntPtr, _
  11.     <MarshalAs(UnmanagedType.LPTStr), [In]()> ByVal lpszClass As String, _
  12.     <MarshalAs(UnmanagedType.LPTStr), [In]()> ByVal lpszWindow As String _
  13.     ) As IntPtr
  14. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  15.         ListView1.Items.Clear()
  16.         For Each x As Process In Process.GetProcessesByName("CenterBot")
  17.  
  18.                 Dim A = ListView1.Items.Add(x.ProcessName)
  19.                 A.SubItems.Add(x.MainWindowTitle)
  20.                 A.SubItems.Add(x.Id)
  21.             End If
  22.         Next
  23.     End Sub
  24.  
  25.  
  26. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  27.         On Error Resume Next
  28.         If ListView1.SelectedItems.Count = 0 Then
  29.             MessageBox.Show("Choose Your Process For the list", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
  30.         Else
  31.             Dim HWND As Integer = FindWindow(0, ListView1.SelectedItems.Item(0).SubItems(1).Text)
  32.             If HWND = 0 Then
  33.                 MessageBox.Show("Choose Your Process For the list", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
  34.                 Exit Sub
  35.             Else
  36.                 Dim ChildWndHandle As IntPtr
  37.  
  38.  
  39. ChildWndHandle = FindWindowEx(HWND, IntPtr.Zero, "WindowsForms10.BUTTON.app.0.2bf8098_r13_ad1", "Button Text")
  40.                 MsgBox(ChildWndHandle)
  41.                 SendMessage(ChildWndHandle, BM_CLICK, 1, 0)
  42.   End If
  43.         End If
  44.     End Sub
  45. End Class
  46.  
This Code is working well if the button on the form
but the button in a groupbox
also the groupbox in a TabControl !!

Hope i get helped here ;)
Aug 7 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.