LiveWire Network Peer Answers Peer Support Teen Forums Tech Forums College Forums 518 users online 225211 members 1244 active today Advertise Here Sign In
TeenCollegeTechPhotos | Quizzes | LiveSecret | Memberlist | Dictionary | News | FAQ
Member Spotlight
Muffinman
Favs: Spiderman dies when hit by semitruck.
Mood: Speechless
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
6 online / 23 MPM
Christmas Eve
Fresh Topics
  LiveWire / Technical Forums / Programming & Application Development / Adding Reply

Adding Reply
Archived Topic: It will not be bumped to the top of the forum.
Topic VB.net SendMessage
Membername   Not a member? Sign Up Free (takes 20 seconds)
Password   Forgotten your password?
Post

Font:   Size:   Color:

FAQ Keyword Search:
Post Options
Favorites Manager
Notify me of new replies to this topic by email
Notify me of new replies to this topic by private message
Original Post
j3100 Posted at 1:16 am on Dec. 29, 2007
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

Replies
j3100 Posted at 12:30 pm on Dec. 29, 2007
Nevermind, i figuered it out, thanks anyway guys.
Ashrkewl Posted at 2:14 am on Dec. 29, 2007
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...

ricke Posted at 1:19 am on Dec. 29, 2007
Post from this position was omitted due to content violations
All 3 previous replies displayed.