c# - Force close all open popups from code -


i want cause open popups (with staysopen == false) close code. want simulate user clicking mouse (which close popups) code.

i don't need simulate click, need resulting behavior. i've thought going through visual tree looking popups , closing each one, doesn't seem cleanest approach.

thanks in advance or opinions.

a wpf popup creates new window (a win32 window, not wpf window instance). can't find in application.windows collection, can find using win32 api enumchildwindows.

once have handle, can retrieve associated hwndsource. think rootvisual of hwndsource popup (didn't check, might have deeper in visual tree).

so code should similar (completely untested):

public static class popupcloser {     public static void closeallpopups()     {         foreach(window window in application.current.windows)         {             closeallpopups(window);         }     }      public static void closeallpopups(window window)     {         intptr handle = new windowinterophelper(window).handle;         enumchildwindows(handle, closepopup, intptr.zero);     }      private static bool closepopup(intptr hwnd, intptr lparam)     {         hwndsource source = hwndsource.fromhwnd(hwnd);         if (source != null)         {             popup popup = source.rootvisual popup;             if (popup != null)             {                 popup.isopen = false;             }         }         return true; // continue enumeration     }      private delegate bool enumwindowsproc(intptr hwnd, intptr lparam);      [dllimport("user32.dll")]     [return: marshalas(unmanagedtype.bool)]     private static extern bool enumchildwindows(intptr hwndparent, enumwindowsproc lpenumfunc, intptr lparam);  } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -