Link to home
Start Free TrialLog in
Avatar of HaiFai
HaiFaiFlag for Finland

asked on

vb.net click button in another window

Hi

I cant get this code to work .. i need to click yes button in another form called "Sample"
could anyone tell me what im doing wrong.

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
    End Function
    Private Const WM_LBUTTONDOWN As Long = &H201
    Private Const WM_LBUTTONUP As Long = &H202
    Private Const BN_CLICKED As Integer = 245
    Sub CommunicateByClickButton(ByVal ApplicationTitle As String)
        Dim hwnd As IntPtr = IntPtr.Zero
        Dim hwndChild As IntPtr = IntPtr.Zero
        hwnd = FindWindow(Nothing, ApplicationTitle)
        If hwnd.Equals(IntPtr.Zero) Then
            MsgBox("Couldn't find the " & ApplicationTitle)
            Exit Sub
        Else
            AppActivate(ApplicationTitle)
        End If
        'Get a handle for the button with caption
        hwndChild = FindWindowEx(hwnd, 0, "Button", "Yes")
        'send BN_CLICKED message 
        If Not hwndChild.Equals(IntPtr.Zero) Then
            SendMessage(CType(hwndChild, Integer), BN_CLICKED, 0, IntPtr.Zero)
        End If
    End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  CommunicateByClickButton("Sample")
end sub
End Class

Open in new window

Avatar of asfahani
asfahani

The code you're trying relies on the captions of the individual buttons to identify them. For example, it uses the following code to get a handle to the "Yes" button:

hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "Yes");
Which specifies "Button" for the name of the window class, and "Yes" for the name of the window (in the case of a button, this is the same as the caption text displayed on the button itself).

This code worked fine under Windows XP (and previous versions), where the buttons were identified with textual captions. The "Yes" button had a window name of "Yes", and thus "Yes" was displayed as the button's caption.

However, it looks like things have changed under Windows 7 (possibly under Vista as well)). Using Spy++ to investigate the Sample window confirms that the "Yes" button no longer has a window name of "Yes". In fact, it doesn't have a window name at all; the caption is NULL. Presumably, the new fancy look of the Forms required that buttons be custom drawn, thus the captions are no longer necessary to indicate which button corresponds to which function. The custom painting routines take care of drawing the necessary captions.

Since no button can be found with the window text you specified, a value of 0 (NULL) for (HwndChild) is returned for the window handle.
Avatar of HaiFai

ASKER

Is there any alternate way to click that yes button from .net ?
ASKER CERTIFIED SOLUTION
Avatar of asfahani
asfahani

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HaiFai

ASKER

Nice thanks, it seems to work...
just need to change  
'If sb.ToString.Contains("BUTTON") Then' to
'If sb.ToString.Contains("Button") Then'

there seems to be one bug, if window is activated it will return "No" and if its not it will return "yes"
but its on since app i need to click is in backround it does not matter.