[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Findwindow Classes

Posted on 2005-04-13
5
Medium Priority
?
565 Views
Last Modified: 2008-01-09
I have created a little app, that will find a window then find the controls on  that form.
I can find the window fine, but i have two text boxes on the form, and of course the class name is thundertextbox well , i have to text boxes, i am able to get the text from the text2 box but how do i make it get the text from the text1 box? I seems to only get text from the text box with the highes number.. I know you can put the text in the vbnullstring area BUT they don't contain text so both are blank .


Dim mystr As String * 255
'the below will find multiple of same class names
fwin = FindWindow(vbNullString, "Form1")
fwin1 = FindWindowEx(fwin, 0, "thundertextbox", vbNullString)

'now sendmessage to get the text
Call SendMessage(fwin1, WM_GETTEXT, 255, mystr)
Label1.Caption = mystr



0
Comment
Question by:jlbryant1024
  • 3
3 Comments
 
LVL 10

Accepted Solution

by:
edwardiii earned 2000 total points
ID: 13778144
Hi, jlbryant1024.

I had a similar situation--two RichEdit windows with no captions.  The good news is that Windows always cycles through a window's sub-components in the same order.  So, if you've got two identical thundertextboxes, you'll need to use an Enumeration routine to list all the sub-windows of your Form1.  Then you'll use a loop and counter to arrive at the handle of the thundertextbox you want. :

Dim mystr As String * 255
fwin = FindWindow(vbNullString, "Form1")
EnumChildWindows fwin, AddressOf EnumChildWindow, ByVal 0&

Then, add the following to a module (you've already got at least the SendMessage and FindWindow/Ex APIs):

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

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

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Public Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

In the same module, declare the following variables as public, including mystr:

     Public Const WM_GETTEXT = &HD
     Public Const WM_SETTEXT = &HC
     Public mystr, wText As String
     Public x As Integer

Still in the same module, add the Enumeration routine:

     Function EnumChildWindow(ByVal hChild As Long, ByVal lParam As Long) As Long

         Dim wClass As String
         Dim j As Integer
   
         wClass = Space(64)
         j = GetClassName(hChild, wClass, 63)
         wClass = Left(wClass, j)
         
         wText = Space(256)
         j = SendMessageByString(hChild, WM_GETTEXT, 255, wText)
         wText = Left(wText, j) '
   
         Debug.Print "Enum " & hChild; ", "; wClass;
         If Len(wText) Then Debug.Print ", """; wText; """";
         Debug.Print
   
         If wClass = "ThunderRT6TextBox" Then
              x = x + 1
              If x = 2 Then      'NOTE: you can change x = 1 to get your Text2.text
                  mystr = wText
              End If
         End If
   
         EnumChildWindow = 1  ' Continue enumeration
         End Function

The code in your command button should be:

     Dim fwin As Long    
    fwin = FindWindow(vbNullString, "Form1")
    EnumChildWindows fwin, AddressOf EnumChildWindow, ByVal 0&
    Label1.Caption = mystr

0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13778152
I apologize--the first part of the code I shared is a duplicate--please delete the following lines after my first paragraph:

     Dim mystr As String * 255
     fwin = FindWindow(vbNullString, "Form1")
     EnumChildWindows fwin, AddressOf EnumChildWindow, ByVal 0&

The below is correct:

     "The code in your command button should be:"

          Dim fwin As Long    
         fwin = FindWindow(vbNullString, "Form1")
         EnumChildWindows fwin, AddressOf EnumChildWindow, ByVal 0&
         Label1.Caption = mystr
0
 
LVL 10

Expert Comment

by:edwardiii
ID: 13791249
Also, if your textbox is other than a "ThunderRT6TextBox" class, replace "ThunderRT6TextBox" in the following section of the code above with the exact name of your class:

         If wClass = "ThunderRT6TextBox" Then
              x = x + 1
              If x = 2 Then      'NOTE: you can change x = 1 to get your Text2.text
                  mystr = wText
              End If
         End If
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question