wpf - Check if something is overlapping my window? -
how check if overlapping window?
i found winforms code should trick:
public static bool isoverlapped(iwin32window window) { if (window == null) throw new argumentnullexception("window"); if (window.handle == intptr.zero) throw new invalidoperationexception("window not yet exist"); if (!iswindowvisible(window.handle)) return false; intptr hwnd = window.handle; hashset<intptr> visited = new hashset<intptr> { hwnd }; // set used make calling getwindow in loop stable checking if have // visited window returned getwindow. avoids possibility of infinate loop. rect thisrect; getwindowrect(hwnd, out thisrect); while ((hwnd = getwindow(hwnd, gw_hwndprev)) != intptr.zero && !visited.contains(hwnd)) { visited.add(hwnd); rect testrect, intersection; if (iswindowvisible(hwnd) && getwindowrect(hwnd, out testrect) && intersectrect(out intersection, ref thisrect, ref testrect)) return true; } return false; } [dllimport("user32.dll")] private static extern intptr getwindow(intptr hwnd, int ucmd); [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] private static extern bool getwindowrect(intptr hwnd, [out] out rect lprect); [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] private static extern bool intersectrect([out] out rect lprcdst, [in] ref rect lprcsrc1, [in] ref rect lprcsrc2); [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] private static extern bool iswindowvisible(intptr hwnd); private const int gw_hwndprev = 3; [structlayout(layoutkind.sequential)] private struct rect { public int left; public int top; public int right; public int bottom; }
but not sure how make work on wpf, can me out? tried lot of things already..
if want handle given wpf window can use system.windows.interop.windowinterophelper
class. update isoverlapped
allow interact winforms code:
public static bool isoverlapped(window window) { if (window == null) throw new argumentnullexception("window"); var hwnd = new windowinterophelper(window).handle; if (hwnd == intptr.zero) throw new invalidoperationexception("window not yet exist"); if (!iswindowvisible(hwnd)) return false; hashset<intptr> visited = new hashset<intptr> { hwnd }; // set used make calling getwindow in loop stable checking if have // visited window returned getwindow. avoids possibility of infinate loop. rect thisrect; getwindowrect(hwnd, out thisrect); while ((hwnd = getwindow(hwnd, gw_hwndprev)) != intptr.zero && !visited.contains(hwnd)) { visited.add(hwnd); rect testrect, intersection; if (iswindowvisible(hwnd) && getwindowrect(hwnd, out testrect) && intersectrect(out intersection, ref thisrect, ref testrect)) return true; } return false; }
Comments
Post a Comment