I open a Google Translate page and bring it to front, but it doesn't always get focus on the source textbox for the user to type immediately. It gets focus always when I run it in VS debugging but not as and independent exe file.
Always after pressing the button on my mouse that runs this exe file after a long time of not pressing the button, the "source" textbox gets selected, but if I press not after a long time from the last press, the textbox "source" doesn't get selected.
What I have tried:
Process[] Processes = Process.GetProcessesByName("dragon");
foreach (Process p in Processes)
{
ShowWindow(p.MainWindowHandle, SW_RESTORE);
}
Processes = Processes.Where(p => p.MainWindowHandle != IntPtr.Zero).ToArray();
if (Processes.Length == 0)
{
ProcessStartInfo prs = new ProcessStartInfo("dragon.exe");
prs.Arguments = "http://translate.google.com" + " --new-window";
prs.WindowStyle = ProcessWindowStyle.Normal;
Process p = Process.Start(prs);
}
Processes = Process.GetProcessesByName("dragon");
foreach (Process p in Processes)
{
ShowWindow(p.MainWindowHandle, SW_MINIMIZE);
}
foreach (Process p in Processes)
{
ShowWindow(p.MainWindowHandle, SW_RESTORE);
SetWindowPos(p.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
BringWindowToTop(p.MainWindowHandle);
SetForegroundWindow(p.MainWindowHandle);
}
foreach (Process p in Processes)
{
SetWindowPos(p.MainWindowHandle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(p.MainWindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
}
foreach (Process p in Processes)
{
ShowWindow(p.MainWindowHandle, SW_RESTORE);
BringWindowToTop(p.MainWindowHandle);
SetFocus(new HandleRef(null, p.MainWindowHandle));
}
foreach (Process p in Processes)
{
SetForegroundWindow(p.MainWindowHandle);
}