private void button1_Click(object sender, EventArgs e) { var prs = new ProcessStartInfo("chrome.exe"); prs.Arguments = "http://translate.google.com" + " --new-window"; Process p = Process.Start(prs); Program.setFocusToProcess(p); //setFocusToThisProcessChromeName("chrome"); } public static void setFocusToThisProcessChromeName(string name) { Process[] Processes = Process.GetProcessesByName(name); //if (Processes.Length == 0) //{ // MessageBox.Show("The program: '" + name + "' isn't running and can't be focused on.", "TranslateProgram"); // return; //} foreach (Process process in Processes) { setFocusToProcess(process); } } static public void setFocusToProcess(Process process) { //if (process.MainWindowHandle == (IntPtr)0x00000000) setFocusToHandleAndUnmaximize(process.MainWindowHandle); setFocusToHandleAndUnmaximize(process.Handle); } private static void setFocusToHandleAndUnmaximize(IntPtr i) { if (i == IntPtr.Zero) { return; } int SWP_SHOWWINDOW = 0x0040; const int SW_RESTORE = 9; int HWND_TOPMOST = -1; //const int SW_SHOWNORMAL = 1; //if (IsIconic(process.MainWindowHandle) != 0) //{ // ShowWindow(process.MainWindowHandle, SW_RESTORE); //} ShowWindow(i, SW_RESTORE); SetForegroundWindow(i); BringWindowToTop(i); SetFocus(new HandleRef(null, i)); SetWindowPos(i, 0, Cursor.Position.X - 100, Cursor.Position.Y - 100, 0, 0, SWP_SHOWWINDOW | HWND_TOPMOST| SW_RESTORE); } [DllImport("User32.dll")] static extern int SetForegroundWindow(IntPtr point); [DllImport("user32.dll")] private static extern int IsIconic(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr SetFocus(HandleRef hWnd); [DllImport("user32.dll")] private static extern int ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] private static extern int BringWindowToTop(IntPtr hWnd); [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)