c# - When set a process handle window position to screen center it's working but not in all cases why? -
in form1 if process not yet running run bring front , center screen center. if process running bring front , center it:
if (autoit.autoitx.winexists(existingprocessname, "") == 0) // window not found { int processid = autoit.autoitx.run(processfilename, "", autoit.autoitx.sw_show); setprocesswindow.bringtofront(processid); setprocesswindow.centerprocesswindow(processid); thread.sleep(10000); autoit.autoitx.mouseclick("left", 358, 913, 1, -1); } else { process[] processes = process.getprocessesbyname(processname); setprocesswindow. bringtofront(processes[0].id); setprocesswindow.centerprocesswindow(processes[0].id); thread.sleep(10000); autoit.autoitx.mouseclick("left", 358, 913, 1, -1); } and class setprocesswindow:
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 usingautoit { class setprocesswindow { [dllimport("user32.dll")] public static extern bool setforegroundwindow(intptr hwnd); [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; setforegroundwindow(handle); } public static void centerprocesswindow(int processid) { process process = process.getprocessbyid(processid); intptr handle = process.mainwindowhandle; if (handle != intptr.zero) { 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); } } } } the problem if process running , drag process window 1 of screen corners click on empty in screen process in background , running program bring process handle window front , center it.
but if drag process window somewhere in screen not center , quit process running program , it's running process bring front not center it.
why when process not running , program running process dosent center ?
your process window shown immediately, it's not set won't results debugging because if go through debugger enough time pass , handle created work fine you. need wait mainwindowhandle set. can modify centerprocesswindow method this:
public static void centerprocesswindow(int processid) { process process = process.getprocessbyid(processid); while (process.mainwindowhandle == intptr.zero) process.refresh(); 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); }
Comments
Post a Comment