Avatar of HaiFai
Flag for Finland asked on

vb.net click button in another window


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
        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
end sub
End Class

Open in new window

Visual Basic.NET.NET ProgrammingProgramming

Avatar of undefined
Last Comment

8/22/2022 - Mon

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.

Is there any alternate way to click that yes button from .net ?

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy