WPF accessing opened print dialog and close them

只愿长相守 提交于 2019-12-05 04:50:29

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();
        }
    }

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

If I understand correctly, you're able to do the user logout at the 5 minute mark (I presume via the timer event handler), but if the print dialog was open, then the printout can still end up happening.

The question is, what's really the important thing here, closing the dialog, or preventing the subsequent printout? Also, are you logging out the user but keeping the program going, or is the program quitting?

If the app is supposed to shut down, then your code can call the Application object's Shutdown() method, which will close any modal dialogs as part of the shutdown process.

If the app is supposed to keep running without a current user, then I would think the White library is your best bet. It's unclear why you've moved away from this.

With or without closing the print dialog, however, the printing code should check to see if the user is still logged in before it actually prints something. If the login expired while the print dialog was on screen, the printing code should simply exit without printing anything.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!