Google Answers Logo
View Question
 
Q: Programming C# ( No Answer,   2 Comments )
Question  
Subject: Programming C#
Category: Computers > Programming
Asked by: zaibar-ga
List Price: $100.00
Posted: 29 Jun 2006 18:59 PDT
Expires: 04 Jul 2006 11:55 PDT
Question ID: 742209
I am looking for a sequence of code that will retrieve the current
selected text from any active window in the system.

For example if I select a paragraph in a Word document or Excel or IE
and I run this code, it will capture the selected text.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Programming C#
From: eyanson-ga on 30 Jun 2006 07:26 PDT
 
here is partial solution for VB6.
it sample work only with textbox but don't work with document of IE.
if you wan't pay u$s 100 I dedicate more time for this solution.
bye

Option Explicit


Private Declare Function AttachThreadInput Lib "user32" (ByVal
idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As
Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal
hWnd As Long, lpdwProcessId As Long) 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
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE

Private mLngCurrThread As Long
Private Sub Form_Load()
    Timer1.Interval = 500
    Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
    If SetWindowInput = True Then
        Me.Caption = GetWindowTextSel(GetFocus)
    End If
End Sub
Private Function GetHiLoWord(ByVal lParam As Long, LOWORD As Long,
HIWORD As Long) As Boolean
      LOWORD = lParam And &HFFFF&
      HIWORD = lParam \ &H10000 And &HFFFF&
      GetHiLoWord = True
End Function
Private Function GetWindowText(ByVal hWnd As Long) As String
    Dim strBuff As String, lngLen As Long
    lngLen = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0)
    If lngLen > 0 Then
        lngLen = lngLen + 1
        strBuff = Space$(lngLen)
        lngLen = SendMessage(hWnd, WM_GETTEXT, lngLen, ByVal strBuff$)
        If lngLen > 0 Then
            GetWindowText = Left$(strBuff, lngLen)
        End If
    End If
End Function
Private Function GetWindowTextSel(ByVal hWnd As Long) As String
    Dim lngRet As Long, lngLo As Long, lngHi As Long, strSel As String
    lngRet = SendMessage(hWnd, EM_GETSEL, 0, ByVal 0&)
    If lngRet <> -1 Then
        Call GetHiLoWord(lngRet, lngLo, lngHi)
        strSel = GetWindowText(hWnd)
        strSel = Mid$(strSel, lngLo + 1, lngHi - lngLo)
        GetWindowTextSel = strSel
    End If
End Function
Private Function SetWindowInput() As Boolean
    Dim hWnd1 As Long, lngThread1 As Long
    Dim hWnd2 As Long, lngThread2 As Long
    hWnd1 = Me.hWnd
    hWnd2 = GetForegroundWindow
    lngThread1 = GetWindowThreadProcessId(hWnd1, 0)
    lngThread2 = GetWindowThreadProcessId(hWnd2, 0)
    If (lngThread1 <> 0) And (lngThread2 <> 0) Then
        If lngThread1 = lngThread2 Then
            SetWindowInput = True
            Exit Function
        End If
        If mLngCurrThread <> lngThread2 Then
            Call AttachThreadInput(lngThread1, mLngCurrThread, False)
        End If
        If AttachThreadInput(lngThread1, lngThread2, True) <> 0 Then
            mLngCurrThread = lngThread2
        End If
        SetWindowInput = True
    End If
End Function
Subject: Re: Programming C#
From: zaibar-ga on 01 Jul 2006 12:50 PDT
 
Thanks for you comment eyanson-ga,

Please go ahead and do the research. I'll pay you 100.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy