Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to write a windows application in C#
when i select a text or highligte a text out site windows form (every text in windows,web , ...) , i get the text

What I have tried:

i haw a translator void
i want when i select a text ,i get the text and translate text
but i donot know how to get the selected text when i selecting text
Posted
Updated 8-Aug-20 6:57am
v2
Comments
[no name] 24-May-18 17:14pm    
This does not give any details? You are selecting the text from where?
vahidsj2 25-May-18 0:56am    
every text in windows , web , notpad , word , ...
j snooze 24-May-18 17:30pm    
well if this is a textbox where the text is highlighted you can just do the textbox name and use the SelectedText property.
vahidsj2 25-May-18 0:58am    
i want to do this work out of windows form
Richard MacCutchan 25-May-18 4:01am    
If you mean you want to capture text that is displayed in other Windows on the display, that is quite a complex task. You can copy the actual image from the screen but that will not be in text form so you will need to use OCR to convert it.

1 solution

There are two steps to do this:

1. Get the focused control. We can get the current activated window via GetForegroundWindow API, then call GetWindowThreadProcessId API to get the thread id of the current window. If it is not the current thread, call AttachThreadInput API to attach the thread to the current thread. With all these jobs done, we can call GetFocus API to get the focused control handle.

2. Get the text of the focused control. We can call SendMessageW API to send a WM_GETTEXT message to do this.

You can get detail from the code below:

private System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();

private void Form2_Load(object sender, EventArgs e)
{
//Initialize a timer to update the control text.
_timer.Interval = 1000;
_timer.Tick += new EventHandler(_timer_Tick);
}
void _timer_Tick(object sender, EventArgs e)
{
try
{
textBox1.Text = GetTextFromFocusedControl();
}
catch (Exception exp)
{
textBox1.Text += exp.Message;
}
}

//Start to monitor and show the text of the related control.
private void button1_Click(object sender, EventArgs e)
{
_timer.Start();
}

//Get the text of the focused control
private string GetTextFromFocusedControl()
{
try
{
int activeWinPtr = GetForegroundWindow().ToInt32();
int activeThreadId = 0, processId;
activeThreadId = GetWindowThreadProcessId(activeWinPtr, out processId);
int currentThreadId = GetCurrentThreadId();
if (activeThreadId != currentThreadId)
AttachThreadInput(activeThreadId, currentThreadId, true);
IntPtr activeCtrlId = GetFocus();

return GetText(activeCtrlId);
}
catch (Exception exp)
{
return exp.Message;
}
}

//Get the text of the control at the mouse position
private string GetTextFromControlAtMousePosition()
{
try
{
Point p;
if (GetCursorPos(out p))
{
IntPtr ptr = WindowFromPoint(p);
if (ptr != IntPtr.Zero)
{
return GetText(ptr);
}
}
return "";
}
catch (Exception exp)
{
return exp.Message;
}
}

//Get the text of a control with its handle
private string GetText(IntPtr handle)
{
int maxLength = 100;
IntPtr buffer = Marshal.AllocHGlobal((maxLength + 1) * 2);
SendMessageW(handle, WM_GETTEXT, maxLength, buffer);
string w = Marshal.PtrToStringUni(buffer);
Marshal.FreeHGlobal(buffer);
return w;
}

[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pt);

[DllImport("user32.dll", EntryPoint = "WindowFromPoint", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr WindowFromPoint(Point pt);

[DllImport("user32.dll", EntryPoint = "SendMessageW")]
public static extern int SendMessageW([InAttribute] System.IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
public const int WM_GETTEXT = 13;

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern IntPtr GetFocus();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowThreadProcessId(int handle, out int processId);

[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
internal static extern int AttachThreadInput(int idAttach, int idAttachTo, bool fAttach);
[DllImport("kernel32.dll")]
internal static extern int GetCurrentThreadId();

[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
Let me know if this does not help.
Aland Li
[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900