LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 666 users online 179049 members 1855 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Video | Dictionary | News | FAQ
You have 1 new message.
Emergency Help
Until you sign up you can't do much. Yes, it's free.

Sign Up Now
Membername:
Password:
Already have an account?
Invite Friends
Active Members
Groups
Contests
Moderators
8 online / 28 MPM
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic

VB.net SendMessage
Button Click Simulation
Replies: 3Last Post Dec. 29, 2007 12:30pm by j3100
Single page for this topic Email Print Favorite
( j3100 )


Executive
Reply
I have a simple application consisting of a form and two buttons, both of which do nothing more than close the form. I'm trying to make another application that will close the form.  It seems to obtain the window and button handles fine, but cannot seem to click them.  The code I have so far is listed below. If anyone has any idea why it's not working, I'd appreciate it.  "Joe" is the name of the other Program, and "Exit" is the caption of the button I want to press.

   Const GW_CHILD As Integer = 5
   Const GW_HWNDNEXT As Integer = 2

   Const WM_GETTEXT As Integer = &HD
   Const WM_GETTEXTLENGTH As Integer = &HE

   Const BM_SETSTATE As Integer = &HF3
   Const WM_LBUTTONUP As Integer = &H202
   Const WM_LBUTTONDOWN As Integer = &H201

   Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

   Declare Auto Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Integer) As IntPtr

   Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
       ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr

   Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
       ByVal wParam As Integer, ByRef lParam As IntPtr) As Integer

   Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, _
       ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr

   Public Function GetWindows(ByVal ParentWindowHandle As IntPtr) As IntPtr()

       Dim ptrChild As IntPtr
       Dim ptrRet() As IntPtr
       Dim iCounter As Integer

       'get first child handle...
       ptrChild = GetWindow(ParentWindowHandle, GW_CHILD)

       'loop through and collect all child window handles...
       Do Until ptrChild.Equals(IntPtr.Zero)
           'process child...
           ReDim Preserve ptrRet(iCounter)
           ptrRet(iCounter) = ptrChild
           'get next child...
           ptrChild = GetWindow(ptrChild, GW_HWNDNEXT)
           iCounter += 1
       Loop

       'return...
       Return ptrRet

   End Function

   Public Function GetWindowText(ByVal WindowHandle As IntPtr) As String

       Dim ptrRet As IntPtr
       Dim ptrLength As IntPtr

       'get length for buffer...
       ptrLength = SendMessage(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)

       'create buffer for return value...
       Dim sbText As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)

       'get window text...
       ptrRet = SendMessage(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sbText)

       'get return value...
       Return sbText.ToString

   End Function

   Public Sub ClickButton(ByVal ButtonHandle As IntPtr)

       'send the left mouse button "down" message to the button...
       Call SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, IntPtr.Zero)

       'send the left mouse button "up" message to the button...
       Call SendMessage(ButtonHandle, WM_LBUTTONUP, 0, IntPtr.Zero)

       'send the button state message to the button, telling it to handle its events...
       Call SendMessage(ButtonHandle, BM_SETSTATE, 1, IntPtr.Zero)

   End Sub

   Private Sub btnPress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPress.Click
       Dim hwnd As IntPtr

       hwnd = FindWindow(vbNullString, "Joe")
       Dim ptrChildWindows() As IntPtr = GetWindows(hwnd)
       MsgBox(ptrChildWindows.Length)
       For iCounter As Integer = 0 To ptrChildWindows.Length - 1
           'grab the current handle to process...
           Dim ptrCurrent As IntPtr = ptrChildWindows(iCounter)
           'get the window text...
           Dim sText As String = GetWindowText(ptrCurrent)
           MsgBox(sText)

           'check to see if this is the button we are looking for...
           If sText = "Exit" Then
               'click the button to close the dialog...
               ClickButton(ptrCurrent)
               'done deal...
               Exit For
           Else
               MsgBox("Not Clicked")

           End If
       Next

   End Sub


1:16 am on Dec. 29, 2007 | Joined May 2004 | 326 Days Active
Join to learn more about j3100 California, United States | Straight Male | 123 Posts | 3409 Points
Post from this position was omitted due to content violations
Ashrkewl


Professional
Reply
Quote: from ricke at 1:19 am on Dec. 29, 2007

wtf is this shit...

That's what I've been trying to figure out mate...

-------
No man is sane who does not know how to be insane on proper occasions.


2:14 am on Dec. 29, 2007 | Joined April 2007 | 83 Days Active
Join to learn more about Ashrkewl Washington, United States | Straight Female | 1747 Posts | 2673 Points
( j3100 )


Executive
Reply
Nevermind, i figuered it out, thanks anyway guys.

Post edited at 1:53 pm on Dec. 29, 2007 by j3100


12:30 pm on Dec. 29, 2007 | Joined May 2004 | 326 Days Active
Join to learn more about j3100 California, United States | Straight Male | 123 Posts | 3409 Points
Single page for this topic Email Print Favorite

Quick Reply

You are signed in as our guest.

Looking for something else?
 

  LiveWire / Technical Forums / Programming & Application Development / Viewing Topic