Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF accessing opened print dialog and close them

I have a WPF application, which needs to log out user after 5 min of inactivity.

But if user open a print dialog of any page, and do not touch screen for 5 minutes, even if I log out user and clear all child elements, print dialog still stays on top of WPF form and somebody can come and continue to print what ever page user stayed.

I tried to use;

Window window = Application.Current.MainWindow;

or

FocusManager.GetFocusedElement();

but could not achieve to access to PrintDialog and close it.

Is there any way to access it and close if user did not respond to print dialog?

like image 393
Teoman shipahi Avatar asked Jun 24 '13 18:06

Teoman shipahi


2 Answers

I fixed this weird problem by using

white project. http://white.codeplex.com/wikipage?title=Working%20with%20window&referringTitle=Programming%20using%20white

By using application class, I am able to access all ModalDialogs in WPF project, and close them.

  Application application = White.Core.Application.Attach(Process.GetCurrentProcess().Id);

private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        White.Core.UIItems.WindowItems.Window window = application.GetWindow("MainWindow");
        List<White.Core.UIItems.WindowItems.Window> modalWindows = window.ModalWindows();
        foreach (White.Core.UIItems.WindowItems.Window modalWindow in modalWindows)
        {
            modalWindow.Close();
        }
    }
like image 98
Teoman shipahi Avatar answered Sep 29 '22 13:09

Teoman shipahi


You can use p/invoke for this. Use findwindow to find any window and destroywindow to close it.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx

like image 41
user582734 Avatar answered Sep 29 '22 13:09

user582734