2011-07-01 2 views
2

사용자 컴퓨터의 열린 양식에서 선택한 텍스트를 가져 오려고합니다. 현재 내가GetFocus - Win32api help

는 말한다 API의
'[DllImport("user32.dll")] 
    static extern int GetFocus();' 

으로 정의된다 GetFocus 사용하여 시도 - 내 응용 프로그램이 창에서 선택한 텍스트를 잡을 수있는 이유를 설명 Retrieves the handle to the window that has the keyboard focus, if the window is attached to the calling thread's message queue.하는 것처럼, 내 응용 프로그램의 일부를 먹으 렴하지만, 외부 그게 아니 하나 예를 들면 pdf.

이 목적에 맞는 대체 Win32 방법을 사용할 수 있습니까?

감사합니다.

편집 :이 순간에 시도

[같이 DllImport ("USER32.DLL")] 정적 통근 용 INT로 GetFocus();

[DllImport("user32.dll")] 
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); 

[DllImport("kernel32.dll")] 
static extern uint GetCurrentThreadId(); 

[DllImport("user32.dll")] 
static extern uint GetWindowThreadProcessId(int hWnd, int ProcessId); 

[DllImport("user32.dll")] 
static extern int GetForegroundWindow(); 

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] 
static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam); 


// second overload of SendMessage 

[DllImport("user32.dll")] 
private static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam, out int lParam); 

const int WM_SETTEXT = 12; 
const int WM_GETTEXT = 13; 

private static string PerformCopy() 
{ 
    try 
    { 
     //Wait 5 seconds to give us a chance to give focus to some edit window, 
     //notepad for example 
     System.Threading.Thread.Sleep(1000); 
     StringBuilder builder = new StringBuilder(500); 

     int foregroundWindowHandle = GetForegroundWindow(); 
     uint remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0); 
     uint currentThreadId = GetCurrentThreadId(); 

     //AttachTrheadInput is needed so we can get the handle of a focused window in another app 
     AttachThreadInput(remoteThreadId, currentThreadId, true); 
     //Get the handle of a focused window 


     int focused = GetFocus(); 




     //Now detach since we got the focused handle 
     AttachThreadInput(remoteThreadId, currentThreadId, false); 

     //Get the text from the active window into the stringbuilder 
     SendMessage(focused, WM_GETTEXT, builder.Capacity, builder); 

     return builder.ToString(); 
    } 
    catch (System.Exception oException) 
    { 
     throw oException; 
    } 
} 
+0

전체적으로 볼 수는 없습니다. 입력 포커스가있는 컨트롤의 창 핸들을 잡을 수는 있지만 (어쨌든 힘든 경우) 해당 창은 선택 항목을 검색하기위한 표준 Windows 메시지에 응답하지 않는 사용자 지정 창일 수 있습니다. 예를 들어 Word는 자체 사용자 지정 컨트롤을 사용합니다. 어떤 문제를 해결하려고합니까? –

+0

사용자는 현재 수동으로 작업 창에있는 응용 프로그램 창에 검색어를 입력합니다. 이상적으로 가능한 경우에 우리는이 프로세스를 시도하고 시뮬레이션하기를 원했기 때문에 내부 pdf 과학 보고서를 읽으면 특정 내부 데이터 항목을 강조 표시하고 수동으로 복사 및 붙여 넣기를하지 않고 Google 시스템으로 검색 할 수있었습니다. – rik

+0

나는 현재 접근 방식으로 성공할 가능성이별로 없다고 생각합니다. 현재 선택 영역을 확보하기위한 단일 범용 API는 없다고 확신합니다. 각 응용 프로그램이 자체적 인 방법으로 텍스트 선택을 구현할 수 있기 때문에 나는 이것을 믿습니다. 클립 보드 리스너를 고려해 볼 수 있습니다. –

답변

0

나는 현재의 접근 방식으로 성공할 가능성이별로 없다고 생각합니다. 현재 선택 영역을 확보하기위한 단일 범용 API는 없다고 확신합니다. 각 응용 프로그램이 자체적 인 방법으로 텍스트 선택을 구현할 수 있기 때문에 나는 이것을 믿습니다.

대체 솔루션으로 clipboard listener을 사용해야합니다. 클립 보드 내용에 대한 변경 내용을 듣고 텍스트가 추가되면 클립 보드에서 빠져 나와 응용 프로그램 창에 넣을 수 있습니다.

0

이것이 UI 자동화 (API 화면 판독기 사용)의 일이라고 생각합니다. 다음은 selected text in C#의 게시물입니다.