c# - GetFocus - Win32api help -
i trying grab selected text open form on users machine. have tried using getfocus
defined as
'[dllimport("user32.dll")] static extern int getfocus();'
in api says - retrieves handle window has keyboard focus, if window attached calling thread's message queue.
explains why app can grab selected text window thats part of app, not 1 thats external, pdf example.
what alternative win32 method available me use fit purpose?
thanks.
edit: attempt @ moment
[dllimport("user32.dll")] static 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 give chance give focus edit window, //notepad example system.threading.thread.sleep(1000); stringbuilder builder = new stringbuilder(500); int foregroundwindowhandle = getforegroundwindow(); uint remotethreadid = getwindowthreadprocessid(foregroundwindowhandle, 0); uint currentthreadid = getcurrentthreadid(); //attachtrheadinput needed can handle of focused window in app attachthreadinput(remotethreadid, currentthreadid, true); //get handle of focused window int focused = getfocus(); //now detach since got focused handle attachthreadinput(remotethreadid, currentthreadid, false); //get text active window stringbuilder sendmessage(focused, wm_gettext, builder.capacity, builder); return builder.tostring(); } catch (system.exception oexception) { throw oexception; } }
check getforegroundwindow
.
Comments
Post a Comment