Results 1 to 6 of 6

Thread: Get text from a foreign grid

  1. #1

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Post

    How can I get the text currently highlighted in a grid. The grid is on a non vb form in another application.
    I tried the GetWindowText api and sending a WM_GETTEXT message, but these do not work.

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    I'm assuming you have the handle to the control. If not, I can tell you how to enumerate the windows to get that. Then you can use:
    In your declarations:
    Code:
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
    Const WM_GETTEXTLENGTH = &HE
    Const WM_GETTEXT = &HD
    In your form:
    Code:
    Dim wintext As String  ' receives the copied text from the target window
    Dim slength As Long  ' length of the window text
    Dim retval As Long  ' return value of message'
    'lControlhwnd is the Handle to Control - Pass This
    
    ' First, determine how much space is necessary for the buffer. (1 is added for the terminating null character.)
    slength = SendMessage(lControlhwnd, WM_GETTEXTLENGTH, ByVal CLng(0), ByVal CLng(0)) + 1
    'Make enough room in the buffer to receive the text.
    wintext = Space(slength)
    ' Copy the target window's text into the buffer.
    retval = SendMessage(lControlhwnd, WM_GETTEXT, ByVal slength, ByVal wintext)
    ' Remove the terminating null and extra space from the buffer.
    wintext = Left(wintext, retval)
    MsgBox "The Control's Text Is: " & wintext, vbOkOnly + vbInformation
    Wade

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    The code above will not work, becuse in order for you to get the text, you would have to know the hWnd of the individual cell.

  4. #4
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363

    Post

    This does work for most controls such as textboxes, listboxes, or comboboxes. However,
    using Spy++, you can tell that only the grid itself has a handle but not each individual
    cell (works the same way for tabs).

    Edited by WadeD on 03-13-2000 at 04:13 PM
    Wade

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    The thing is, there are no chidren physically in the grid.
    If you use Spy++ to retrieve messages for the Grid (I used MSFlexGrid in that test), it looks like it's painting the part of the grid based on the coordinates. So, it is not using WM_SETTEXT (with SendMessage), but rather painting the text (in this case an image) to the Grid.

    I'm sure it can be done, it's just a matter of time finding that out.

  6. #6

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Post

    Well, I knew the WM_GETTEXT message didn't work. And I knew also the grid didn't have any child windows. So if the grid uses graphical methods to paint the text, I'm affraid it can't be done with subclassing either. And to use some kind of OCR techniques is a bit overdone for me. If there are any other ideas, I would like to hear them.

    Thanks so far.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width