C# process start focus issue

荒凉一梦 提交于 2019-12-23 12:03:33

问题



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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!