Results 1 to 5 of 5

Thread: How Much Text Is Selected

  1. #1

    Thread Starter
    Hyperactive Member MikeBAM's Avatar
    Join Date
    Sep 2000
    Location
    Metro Detroit
    Posts
    284
    Is there a way you can tell when text is selected any where on the computer? And just how much? anyone know how to do it?
    ~* )v( ! /< E *~

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    You can send keys ^ C
    then do Len("Copied Stuff")

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    If you need to find out at your own form in a textbox

    Code:
    MsgBox Text1.SelLength
    or if it's another window use findwindow and windowfrompos to find the window, then use findwindowex, then use sendmessage to get the selected text (with Const EM_GETSEL = &HB0) then check the length of that variable


    ....

    or you could just use my class:
    http://www.geocities.com/despotez/WH/

    many examples included!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4
    Guest
    To get the selected text from any window:

    Add to a Form with a CommandButton. This example tells how much text is selected from Notepad. (so make sure to open Notepad).
    Code:
    Private 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
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const EM_GETSEL = &HB0
    
    Function LoWord(ByVal LongVal As Long) As Integer
        LoWord = LongVal And &HFFFF&
    End Function
    
    Function HiWord(ByVal LongVal As Long) As Integer
        If LongVal = 0 Then
            HiWord = 0
            Exit Function
        End If
        HiWord = LongVal \ &H10000 And &HFFFF&
    End Function
    
    Private Sub Command1_Click()
        Dim iStart As Integer, iLength As Integer, lResult As Long, hEdit As Long
        
        hEdit = FindWindowEx(FindWindowEx(0, 0, "Notepad", vbNullString), 0, "Edit", vbNullString)
        If hEdit <> 0 Then
            lResult = SendMessage(hEdit, EM_GETSEL, iStart, iLength)
            Print HiWord(lResult) - LoWord(lResult)
        End If
    End Sub

  5. #5
    Guest
    This next example will detect the selected text of the window the mouse is over, just as long as it's class is "Edit".

    Add to a Form with a Timer.
    Code:
    Private 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
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    Private Const EM_GETSEL = &HB0
    Private PrevHwnd As Long
    
    Function LoWord(ByVal LongVal As Long) As Integer
        LoWord = LongVal And &HFFFF&
    End Function
    
    Function HiWord(ByVal LongVal As Long) As Integer
        If LongVal = 0 Then
            HiWord = 0
            Exit Function
        End If
        HiWord = LongVal \ &H10000 And &HFFFF&
    End Function
    
    Private Sub Form_Load()
        Timer1.Interval = 1
        AutoRedraw = True
    End Sub
    
    Private Sub Timer1_Timer()
        
        Dim PT As POINTAPI
        Dim Wnd As Long
        Dim sClass As String * 255
        GetCursorPos PT
        Wnd = WindowFromPoint(PT.x, PT.y)
        GetClassName Wnd, sClass, 255
        If Left(sClass, InStr(1, sClass, vbNullChar) - 1) = "Edit" Then
            
            Dim iStart As Integer, iLength As Integer, lResult As Long, hEdit As Long
        
            hEdit = FindWindowEx(FindWindowEx(0, 0, "Notepad", vbNullString), 0, "Edit", vbNullString)
            If hEdit <> 0 Then
                lResult = SendMessage(hEdit, EM_GETSEL, iStart, iLength)
                temp = HiWord(lResult) - LoWord(lResult)
                If temp = PrevHwnd Then Exit Sub
                Cls
                PrevHwnd = temp
                Print PrevHwnd
            End If
        End If
    End Sub

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