get the control in window

chidambaram

Well-known member
Joined
Dec 27, 2007
Messages
62
Location
Chennai,India
Programming Experience
Beginner
hi,

I have to access the controls inside a window.

I found that window using FindWindow.

In my window there is two button.

How can i get the control of that button, So that i have to click that button?

My code is
' declaration
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As System.IntPtr

' code

Dim nWnd As IntPtr
Dim ceroIntPtr As New IntPtr(0)
Dim Wnd_name As String

Wnd_name = "Microsoft Internet Explorer"
nWnd = FindWindow(Nothing, Wnd_name)
MsgBox(nWnd.ToString())
'show the info
If nWnd.Equals(ceroIntPtr) Then
MsgBox("App Not Running")
Else
MsgBox("App Running")
Application.DoEvents()
SendKeys.Send("{ENTER}")
End If

here i get the error in SendKeys.Send("{Enter"}").

How can i access the button inside this window?

Plz help me with some code

Thanks in advance and urgent plz...
 
You use the FindWindowEx function to get a handle to a child window. It might be a child of the top-level window you found using FindWindow or the child of another child you found using FindWindowEx.
 
Thanks for ur reply..

I found the window and get the title name

my code is
Dim p As Process() = Process.GetProcessesByName("iexplore")
MsgBox(p.Length())
For i = 0 To p.Length - 1
MsgBox("p(i).MainWindowTitle()" & p(i).MainWindowTitle())
Next

I found my required window through the title.

In my window it contains two buttons OK and Cancel. Focus is in ok button. How can i click that button?
 
As an example, here's some code of mine that logs into Microsoft RMS:
VB.NET:
Dim loginDialogueHandle As IntPtr = IntPtr.Zero

'Get the handle of the POS login dialogue.
Do
    Threading.Thread.Sleep(50)
    loginDialogueHandle = NativeMethods.FindWindow(My.Resources.RmsPosFormClassNameString, "Login")
Loop Until loginDialogueHandle <> IntPtr.Zero

'TODO: Hide login window in release.
'NativeMethods.ShowWindow(loginDialogueHandle, NativeConstants.SW_HIDE)

'Get the handles of the user ID and password fields and the OK button.
Dim userIDFieldHandle As IntPtr = NativeMethods.FindWindowEx(loginDialogueHandle, _
                                                             IntPtr.Zero, _
                                                             My.Resources.RmsPosTextBoxClassNameString, _
                                                             String.Empty)
Dim passwordFieldHandle As IntPtr = NativeMethods.FindWindowEx(loginDialogueHandle, _
                                                               userIDFieldHandle, _
                                                               My.Resources.RmsPosTextBoxClassNameString, _
                                                               String.Empty)
Dim frameHandle As IntPtr = NativeMethods.FindWindowEx(loginDialogueHandle, _
                                                       IntPtr.Zero, _
                                                       My.Resources.RmsPosFrameClassNameString, _
                                                       String.Empty)
Dim okButtonHandle As IntPtr = NativeMethods.FindWindowEx(frameHandle, _
                                                          IntPtr.Zero, _
                                                          My.Resources.RmsPosButtonClassNameString, _
                                                          "OK")

If userIDFieldHandle <> IntPtr.Zero Then
    'Enter the user ID.
    NativeMethods.SendMessage(userIDFieldHandle, _
                              NativeConstants.WM_SETTEXT, _
                              0, _
                              "1")
End If

If passwordFieldHandle <> IntPtr.Zero Then
    'Enter the password.
    NativeMethods.SendMessage(passwordFieldHandle, _
                              NativeConstants.WM_SETTEXT, _
                              0, _
                              "password")
End If

If okButtonHandle <> IntPtr.Zero Then
    'Click the OK button.
    NativeMethods.SendMessage(okButtonHandle, _
                              NativeConstants.BM_CLICK, _
                              0, _
                              0)
End If
The NativeMethods and NativeConstants classes are where all the Windows API declarations are.
 
sir,


When i paste this code i get a syntax error as

System.drawing.Nativemethods is not accessible in this context because it is "private"

Is there any reference i want to add here?
 
I specifically said that NativeMethods and NativeConstants are the classes where all the API declarations are. If YOU don't have NativeMethods and NativeConstants classes then you have to change the code to reflect where YOU do have those API declarations. If your declarations are all in the current class then you don't need to qualify the names at all.
 
Back
Top