c# - How to use ShowWindow to show already hidden windows? -


i writing c# program use showwindow show or hide windows of other processes. problem not able use program show or hide windows of processes if window hidden before program run.

for example, if run program, hide window of other process, show it, work normal. however, if run program, hide window of other process, terminate program, run program again, not able show window of process anymore.

i able show windows of hidden processes if hidden before program ran. how may achieve this?

program.cs

using system; using system.collections.generic; using system.diagnostics; using system.linq; using system.text; using system.threading.tasks;  namespace consoleapplication1 {     class program     {         static void main(string[] args)         {              if (args.length == 2)             {                  if (args[0] == "showh")                 {                     int handle;                     int.tryparse(args[1], out handle);                     app.showhandle(handle);                 }                  else                  {                     process[] processes = process.getprocesses();                      foreach (process process in processes)                     {                          app app = new app(process);                          if (args[1] == app.name)                         {                             if (args[0] == "show")                             {                                 app.show();                             }                             else                             {                                 app.hide();                             }                         }                     }                   }               }         }     } } 

app.cs

using system; using system.collections.generic; using system.diagnostics; using system.linq; using system.runtime.interopservices; using system.text; using system.threading.tasks;  namespace consoleapplication1 {       public class app     {          [dllimport("user32")]         private static extern int showwindow(int hwnd, int ncmdshow);          private const int sw_hide = 0;         private const int sw_show = 5;          public string name { get; private set; }          private process process { get; set; }          public app(process process)          {             this.name = process.processname;             this.process = process;         }          public void hide()          {             int windowhandle = this.process.mainwindowhandle.toint32();             console.writeline("hiding {0}: has window handle {1}", this.name, windowhandle);             showwindow(windowhandle, sw_hide);         }          public void show()         {             int windowhandle = this.process.mainwindowhandle.toint32();             console.writeline("showing {0}: has window handle {1}", this.name, windowhandle);             showwindow(windowhandle, sw_show);          }          public static void showhandle(int handle)         {             console.writeline("showing window handle {0}", handle);             showwindow(handle, sw_show);         }     } } 

update 1: added minimal , complete code example.

update 2: after further experimentation, processes in fact give me window handle of zero. however, in rare circumstances, non-zero window handle, window handle incorrect.

incorrect in: handle value when process hidden different handle value when attempt show process.

however, if remember process' window handle when hidden, can show window again regardless. have updated code example reflect that.

my question becomes: why unable correct window handle of processes if process hidden begin with? (but able window handle if process visible, , hidden.)

since have not received answers, came following solution:

remember window handle of process , associate process id. save file. when application restarted, read file. can use these saved window handles show hidden windows associated process id.

and can use id process object.

process proc = process.getprocessbyid(processid); 

additionally, once windows shown, once again able window handle with

int windowhandle = this.process.mainwindowhandle.toint32(); 

if has better solution please comment. however, solution going now.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -