c# - Why the explorer handle window trying bring it to the screen front doesn't do anything? -


it's working other handles windows.

process[] processes = process.getprocessesbyname(processname); setprocesswindow.bringtofront(processes[0].id); setprocesswindow.centerprocesswindow(processes[0].id); 

if processname example taskmgr bring front of screen , center task manager.

but if example processname explorer (not internet explorer explorer in case directory on hard disk) won't bring front not anything.

this setprocesswindow class:

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.runtime.interopservices; using system.diagnostics; using system.windows.forms; using system.drawing;  namespace automation {     class setprocesswindow     {         [dllimport("user32.dll", charset = charset.auto, exactspelling = true)]         private static extern intptr getforegroundwindow();          [dllimport("user32.dll", charset = charset.auto, setlasterror = true)]         private static extern int getwindowthreadprocessid(intptr handle, out int processid);          [dllimport("user32.dll")]         public static extern bool setforegroundwindow(intptr hwnd);          [dllimport("user32.dll")]         private static extern bool showwindow(intptr handle, int ncmdshow);          private const int sw_showmaximized = 3;         private const int sw_shownormal = 1;          [dllimport("user32.dll")]         [return: marshalas(unmanagedtype.bool)]         static extern bool getwindowrect(intptr hwnd, out rect lprect);          [structlayout(layoutkind.sequential)]         public struct rect         {             public int left;        // x position of upper-left corner             public int top;         // y position of upper-left corner             public int right;       // x position of lower-right corner             public int bottom;      // y position of lower-right corner         }          private const int swp_nosize = 0x0001;         private const int swp_nozorder = 0x0004;         private const int swp_showwindow = 0x0040;          [dllimport("user32.dll", setlasterror = true)]         static extern bool setwindowpos(intptr hwnd, intptr hwndinsertafter, int x, int y, int cx, int cy, int uflags);          public static void bringtofront(int processid)         {             process process = process.getprocessbyid(processid);             intptr handle = process.mainwindowhandle;              if (handle == intptr.zero)                 return;              showwindow(handle, sw_shownormal);             setforegroundwindow(handle);         }          public static void centerprocesswindow(int processid)         {             process process = process.getprocessbyid(processid);              while (process.mainwindowhandle == intptr.zero)             {                 process.refresh();                 break;             }              intptr handle = process.mainwindowhandle;              rect rct;             getwindowrect(handle, out rct);             rectangle screen = screen.fromhandle(handle).bounds;             point pt = new point(screen.left + screen.width / 2 - (rct.right - rct.left) / 2, screen.top + screen.height / 2 - (rct.bottom - rct.top) / 2);             setwindowpos(handle, intptr.zero, pt.x, pt.y, 0, 0, swp_nozorder | swp_nosize | swp_showwindow);         }     } } 

not sure maybe doesn't bring windows front since changed in project properties unchecked prefer 32-bit ?

anyway windows bring front , not.

maybe have same problem handle creation had inside centerprocesswindow method. try adding piece of code bringtofront method aswell.

while (process.mainwindowhandle == intptr.zero)     process.refresh(); 

before line : intptr handle = process.mainwindowhandle;. also, don't need break; inside while loop. if place break there go out of while loop if mainwindowhandle still intptr.zero. way wrote same this:

if (process.mainwindowhandle == intptr.zero)     process.refresh(); 

if it's intptr.zero, refresh , continue, you're willing wait value, therefore while loop.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -