F
Frank Uray
Hi all
I am having some trouble with select the text of the active
window ... I would like to have the same effect like when you
press Ctrl+c in Windows or just get the selected Text back.
Because I am having a background application and because
I am at this point within the void "KeyUp(object sender, KeyEventArgs e)"
I am unable to use "System.Windows.Forms.SendKeys.Send("^(c)");".
I have tried with the following code,
but my knowledge of win API is not good and I dont see the problem:
It works for example in Notepad, but it does not work when the active
window is WordPad or Word ... ???
Thanks for any help !!
Best regards
Frank Uray
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError
= true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint
lpdwProcessId);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo,
bool fAttach);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetFocus();
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam,
StringBuilder lParam);
// second overload of SendMessage
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam,
out int lParam);
const uint WM_GETTEXT = 0x0D;
const uint WM_GETTEXTLENGTH = 0x0E;
const uint EM_GETSEL = 0xB0;
public string GetCurrentSelection()
{
try
{
// Variables
System.IntPtr hWnd = GetForegroundWindow();
uint processId;
uint activeThreadId = GetWindowThreadProcessId(hWnd, out
processId);
uint currentThreadId = GetCurrentThreadId();
AttachThreadInput(activeThreadId, currentThreadId, true);
System.IntPtr focusedHandle = GetFocus();
AttachThreadInput(activeThreadId, currentThreadId, false);
int len = SendMessage(focusedHandle, WM_GETTEXTLENGTH, 0,
null);
System.Text.StringBuilder sb = new
System.Text.StringBuilder(len);
int numChars = SendMessage(focusedHandle, WM_GETTEXT, len +
1, sb);
int start, next;
SendMessage(focusedHandle, EM_GETSEL, out start, out next);
string selectedText = sb.ToString().Substring(start, next -
start);
return selectedText;
} // End: try
catch (Exception ex)
{ symmetric.clsMessages.ErrorHandling(ex.Message, ex.Source,
ex.StackTrace); } // End: catch
return "";
}
I am having some trouble with select the text of the active
window ... I would like to have the same effect like when you
press Ctrl+c in Windows or just get the selected Text back.
Because I am having a background application and because
I am at this point within the void "KeyUp(object sender, KeyEventArgs e)"
I am unable to use "System.Windows.Forms.SendKeys.Send("^(c)");".
I have tried with the following code,
but my knowledge of win API is not good and I dont see the problem:
It works for example in Notepad, but it does not work when the active
window is WordPad or Word ... ???
Thanks for any help !!
Best regards
Frank Uray
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError
= true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint
lpdwProcessId);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo,
bool fAttach);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetFocus();
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam,
StringBuilder lParam);
// second overload of SendMessage
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint Msg, out int wParam,
out int lParam);
const uint WM_GETTEXT = 0x0D;
const uint WM_GETTEXTLENGTH = 0x0E;
const uint EM_GETSEL = 0xB0;
public string GetCurrentSelection()
{
try
{
// Variables
System.IntPtr hWnd = GetForegroundWindow();
uint processId;
uint activeThreadId = GetWindowThreadProcessId(hWnd, out
processId);
uint currentThreadId = GetCurrentThreadId();
AttachThreadInput(activeThreadId, currentThreadId, true);
System.IntPtr focusedHandle = GetFocus();
AttachThreadInput(activeThreadId, currentThreadId, false);
int len = SendMessage(focusedHandle, WM_GETTEXTLENGTH, 0,
null);
System.Text.StringBuilder sb = new
System.Text.StringBuilder(len);
int numChars = SendMessage(focusedHandle, WM_GETTEXT, len +
1, sb);
int start, next;
SendMessage(focusedHandle, EM_GETSEL, out start, out next);
string selectedText = sb.ToString().Substring(start, next -
start);
return selectedText;
} // End: try
catch (Exception ex)
{ symmetric.clsMessages.ErrorHandling(ex.Message, ex.Source,
ex.StackTrace); } // End: catch
return "";
}