c# - How to set focus back to form after opening up a process (Notepad)? -
c# - How to set focus back to form after opening up a process (Notepad)? -
i open notepad programme using process.start() new opened notepad covers screen. want application maintain focus.
i (using same process.start) open ms excel , word focus form need write is:
this.focus(); but quirk notepad: open notepad (and other processes this)
process process = new process(); process.startinfo.useshellexecute = true; process.enableraisingevents = true; process.startinfo.filename = @"abc.log"; process.start(); now notepad takes focus.
i tried these:
this.activate(), this.focus(), needless mention
[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 lengthier solution got here.
all still keeps focus on notepad. why hard simply focus window, application's own window?
edit: @ best can open notepad minimized, still wouldn't give focus form after trying above codes. notepad opens minimized, focus still on notepad (something see in windows xp) , form out of focused.
i tried on net (so sure :)). @ best form on top of other forms, without focus (going @hans passant's method). going heavy blocks of codes over, somehow felt aint gonna easy. used setforegroundwindow() chunks of other code. never thought simply setforegroundwindow() trick.
this worked.
[dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] static extern bool setforegroundwindow(intptr hwnd); private void button1_click(object sender, eventargs e) { process process = new process(); process.startinfo.filename = @...\abc.log"; process.start(); process.waitforinputidle(); //this key!! setforegroundwindow(this.handle); } at times method yields in focus on parent form (in cases desired form modal kid form of parent form); in such cases, add together this.focus() lastly line..
even worked:
microsoft.visualbasic.interaction.shell(@"notepad.exe d:\abc.log", microsoft.visualbasic.appwinstyle.normalnofocus); solution provided here
c# winforms process focus windows-shell
Comments
Post a Comment