问题
When I start a new process, it automatically gets the focus. how can I prevent it from getting the focus or instead get back the focus to my application?
here is the code I'm using:
string path = @"c:\temp\myprocess.exe";
ProcessStartInfo info = new ProcessStartInfo(path);
info.WorkingDirectory = path;
Process p = Process.Start(info);
I just need the executed process not to get the focus.
Thank you very much,
Adi Barda
回答1:
can you do
myForm.Focus();
where myForm is the form on your main application
回答2:
Maybe setting the WindowStyle property to Minimized can help.
回答3:
If you don't need to show the process at all, try this:
string path = @"c:\temp\myprocess.exe";
ProcessStartInfo info = new ProcessStartInfo(path);
info.WorkingDirectory = path;
info.WindowStyle = ProcessWindowStyle.Hidden;
Or set WindowStyle to ProcessWindowStyle.Minimized if you want it visible but minimized, as Uwe Keim said.
回答4:
What i´ve done was to wait a little delay until the other application was succesfully loaded, then focus my application window.
//Test window
const string strCmdText = "/C cd C:\\sqlcipher";
Process.Start("CMD.exe", strCmdText);
//Delay
int liMilliseconds = 50;
Thread.Sleep(liMilliseconds);
//Code to bring window to front
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
来源:https://stackoverflow.com/questions/4499239/c-sharp-process-start-focus-issue