Results 1 to 20 of 20

Thread: How to grab the selected text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    Hi

    I need to be able to detect when a word is selected with mouse on other applications and grab the selected text.

    Even if I could detect the mouse events on other applications (that is mouse buttons pressed etc.) I might be able to work something out.

    Tahnks
    Mass

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Just a thought:
    Subclass for WM_NCLBUTTONUP, store the clipboard content and send a ctrl+C, and then get clipboard content and then reset it again.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    I don't know what WM_NCLBUTTONUP is to start with.
    could you please be a bit simpler?
    Mass

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here you go, I haven't tested it but should work...
    Code:
    Option Explicit
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Public Const WM_NCLBUTTONUP = &HA2
    
    Dim saveHWnd As Long
    Dim oldProcAddr As Long
    
    Sub startsubclassing(ByVal hwnd As Long)
        saveHWnd = hwnd
        oldProcAddr = SetWindowLong(hwnd, -4, AddressOf WndProc)
    End Sub
    
    Sub stopsubclassing()
        SetWindowLong saveHWnd, -4, oldProcAddr
    End Sub
    
    Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long: Dim temp$
        WndProc = CallWindowProc(oldProcAddr, hwnd, uMsg, wParam, lParam)
        If uMsg = WM_NCLBUTTONUP Then
            temp = Clipboard.GetText
            SendKeys ("^C")
            Debug.Print Clipboard.GetText '<replace this with what you want to do with the text
            Clipboard.SetText temp
        End If
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    Ked, how would you use that piece of code?
    Mako Shark
    Great White

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Oh! I always forget the most important part, how to use the code. Ok here goes:
    Code:
    'to start subclassing
    startsubclassing hwnd
    'to stop subclassing
    stopsubclassing
    You should put the first one in form load and the other in unload.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    I tried the code but I get the following error

    Invalid use of AddressOf operator

    Mass

  8. #8
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    I have tryed this code and it works nice. Is this also possible for a ListBox ?

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Mass, put that in code in a standard module
    Karl, What do you mean? This code copies a selection, that depends wheather copying a listitem in a foreign listbox is connected to clipboard or not.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Addicted Member
    Join Date
    May 2000
    Posts
    247
    Ked, I tried what you said. Place it in the form Load and Unload, but what exactly is it suppose to do?

    In the form_load I have the following:
    Code:
    Private Sub Form_Load()
      Dim lng_Return As Long
      lng_Return = FindWindow(0&, "Microsoft Word - Document1")
      Call startsubclassing(lng_Return)
    End Sub
    Sorry if i sound inexperience because I am.
    Mako Shark
    Great White

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    Ked, I know it is very stupid but I could'nt make it work
    I put the code in a standard module and ran it
    I did not get aynthing
    Mass

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Ok, i need to tell you, it's called subclassing and it get's events vb didn't fire, in this case your subclassed window fires theses events. Mass, shark showed you how to use it
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Addicted Member
    Join Date
    May 2000
    Posts
    247

    I understand the term SubClassing but what is the purpose and procedure of your codes

    So, once I run your code, do I go to MS Word and highlight something? I placed a break on WndProc, but it never break there. I assumed that it never ran that procedure.

    Thanks.
    Mako Shark
    Great White

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's odd, it should be fired all the time, if youre moving your mouse over it. Maybe youre hwnd argument isn't valid. what do you get in lng_Return
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    Ked, When I said I ran it I did like Shark suggested
    I think if I tell you exactly what I did, you may get my view of the picture. I put yoyr code in a standard module
    and put the startsubclassing call with the handel of my notepad and put a textbox on my form and altered the debug print part to the textbox. then I selected some text in the notepad and got nothing in the textbox.

    Sorry to bother you so much and thanks





    Mass

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    place a break in wndproc like shark did, if you don't get an event, you probably have the handle wrong, otherwise it's a problem with my code, anyway i have a subclassing control, i'll upload it on my homepage later...
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    I did and it dosent seem to get the event.
    I will check to make sure the handle is right. As I coulden't make your code to work I don't know if it answers
    my other question (mouse events).

    Just a thought, if we could detect the mouse down anywhere
    then we can get the handle of the window and by using Appactivate and sendkey we could grab the text. I know the idea is not a good one but what do you expect?

    I got some code (I can't remember where but I could look it up) which is suppos to do the job but I can't understand it.
    Could I Email it to you and see what you think?

    Thanks
    Mass

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Sure thing
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Sorry, I was getting a bit busy and forgot, well you did get the handle did you?

    Well you could get an event for clickin anywhere by using the code at, posted by Fran C, i didn't find anything else:
    http://forums.vb-world.net/showthrea...threadid=15494
    You should replace WM_MOUSEMOVE with WM_NCLBUTTONUP

    Using getforegroundwindow api you could get the handle for the window your selecting the text from
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jun 2000
    Posts
    175
    I used the code but I can't detect double click with WM_LBUTTONDBLCLK can anyone tell me why?
    Mass

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