I open the notebook from my program using Process.Start() , but a new open notebook closes the screen. But I want my application to be in the spotlight.
I also (using the same Process.Start) open MS Excel and Word, but in order to return focus to my form, I need to write the following:
this.Focus();
But the quirk with Notepad: I open the notepad (and all other processes like this)
Process process = new Process(); process.StartInfo.UseShellExecute = true; process.EnableRaisingEvents = true; process.StartInfo.FileName = @"abc.log"; process.Start();
Now the notebook focuses.
I tried:
this.Activate() , this.Focus() , optional
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern IntPtr SetFocus(HandleRef hWnd); { IntPtr hWnd = myProcess.Handle; SetFocus(new HandleRef(null, hWnd)); }
[DllImport("User32")] private static extern int SetForegroundWindow(IntPtr hwnd); [DllImportAttribute("User32.DLL")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); private const int SW_SHOW = 5; private const int SW_MINIMIZE = 6; private const int SW_RESTORE = 9; { ShowWindow(Process.GetCurrentProcess().MainWindowHandle, SW_RESTORE); SetForegroundWindow(Process.GetCurrentProcess().MainWindowHandle); }
Another longer solution is obtained from here .
Everything is still focused on notepad. Why is it so difficult to just focus on the window, is this also the applicationโs own window?
EDIT: At best, I can open a notebook that was minimized, but it still didn't give focus to the form after trying all of the above codes. The notebook opens with minimal values, but the focus will still remain in the notebook (something that we sometimes see in xp windows), and the shape will not be focused.
source share