关闭打开文件对话框/ SaveFileDialog [英] Closing OpenFileDialog/SaveFileDialog

查看:131
本文介绍了关闭打开文件对话框/ SaveFileDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个条件,关闭子窗体为自动注销的一部分。我们可以通过定时器线程迭代Application.OpenForms关闭子窗体。我们不能用Application.OpenForms为打开文件对话框未列出关闭打开文件对话框/ SaveFileDialog。

We have a requirement to close child form as part of auto logoff. We can close the child forms by iterating Application.OpenForms from the timer thread. We are not able to close OpenFileDialog/SaveFileDialog using Application.OpenForms as the OpenFileDialog is not listed.

如何关闭打开文件对话框,并CloseFileDialog?

How can I close OpenFileDialog and CloseFileDialog?

推荐答案

这将需要的PInvoke,该对话框不是形式而是原生的Windows对话框。基本方法是枚举所有的顶层窗口,并检查它们的类名是#32770,对于由Windows所拥有的所有对话框中的类名。并迫使对话框关闭通过发送WM_CLOSE消息。

This is going to require pinvoke, the dialogs are not Forms but native Windows dialogs. The basic approach is to enumerate all toplevel windows and check if their class name is "#32770", the class name for all dialogs owned by Windows. And force the dialog to close by sending the WM_CLOSE message.

添加一个新类到您的项目并粘贴下面所示的code。呼叫DialogCloser.Execute()时,注销计时器到期。 然后的封闭形式。在code将工作的MessageBox,OpenFormDialog,的FolderBrowserDialog,PrintDialog类,ColorDialog类,FontDialog类类,PageSetupDialog和SaveFileDialog。

Add a new class to your project and paste the code shown below. Call DialogCloser.Execute() when the logout timer expires. Then close the forms. The code will work for MessageBox, OpenFormDialog, FolderBrowserDialog, PrintDialog, ColorDialog, FontDialog, PageSetupDialog and SaveFileDialog.

using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

static class DialogCloser {
    public static void Execute() {
        // Enumerate windows to find dialogs
        EnumThreadWndProc callback = new EnumThreadWndProc(checkWindow);
        EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero);
        GC.KeepAlive(callback);
    }

    private static bool checkWindow(IntPtr hWnd, IntPtr lp) {
        // Checks if <hWnd> is a Windows dialog
        StringBuilder sb = new StringBuilder(260);
        GetClassName(hWnd, sb, sb.Capacity);
        if (sb.ToString() == "#32770") {
            // Close it by sending WM_CLOSE to the window
            SendMessage(hWnd, 0x0010, IntPtr.Zero, IntPtr.Zero);
        }
        return true;
    }

    // P/Invoke declarations
    private delegate bool EnumThreadWndProc(IntPtr hWnd, IntPtr lp);
    [DllImport("user32.dll")]
    private static extern bool EnumThreadWindows(int tid, EnumThreadWndProc callback, IntPtr lp);
    [DllImport("kernel32.dll")]
    private static extern int GetCurrentThreadId();
    [DllImport("user32.dll")]
    private static extern int GetClassName(IntPtr hWnd, StringBuilder buffer, int buflen);
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

这篇关于关闭打开文件对话框/ SaveFileDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆