私は、選択したテキストをユーザーのマシン上の開いたフォームから取得しようとしています。現在、私はそれは言うAPIでGetFocus - Win32api help
'[DllImport("user32.dll")]
static extern int GetFocus();'
として定義されているGetFocus
を使用して試してみました - 私のアプリは、外部のthats 1私のアプリの一部厥ウィンドウから選択したテキストをつかむことはできませんが、理由を説明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の")] 静的にextern 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;
}
}
あなたはこれを完全に一般に行うことはできません。たとえ入力フォーカス(とにかく十分です)でコントロールのウィンドウハンドルを得ることができたとしても、そのウィンドウは、選択を取得するための標準のウィンドウメッセージに応答しないカスタムウィンドウです。たとえば、Wordは独自のカスタムコントロールを使用します。どのような問題を解決しようとしていますか? –
ユーザは現在、タスクトレイのアプリケーションウィンドウに手動で検索語を入力します。理想的には、可能であれば、このプロセスを試してシミュレートしたいので、内部のPDFデータを読んで、特定の内部データ項目を強調表示し、手動でコピーして貼り付けることなくシステムで検索することができました。 – rik
私はあなたの現在のアプローチで成功するチャンスがないと思います。私は、現在の選択を保持するための汎用APIはないと確信しています。私は、各アプリケーションが独自の方法でテキスト選択を実装できるので、これを信じています。あなたはクリップボードリスナーを考えることができます。 –