如何使用C#在SaveFileDialog中设置最顶层?

时间:2011-01-12 08:18:43

标签: c# winforms savefiledialog

我想设置SaveFileDialog的最顶层。但是你知道没有财产。 还有其他方法可以在SaveFileDialog中设置TopMost吗?

4 个答案:

答案 0 :(得分:2)

class ForegroundWindow : IWin32Window
{
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

    static ForegroundWindow obj = null;
    public static ForegroundWindow CurrentWindow { 
        get { 
           if (obj == null) 
                obj = new ForegroundWindow(); 
           return obj; 
        } 
    }
    public IntPtr Handle {
        get { return GetForegroundWindow(); }
    }
}

SaveFileDialog dlg=new SaveFileDialog();
dlg.ShowDialog(ForegroundWindow.CurrentWindow);

答案 1 :(得分:0)

我只能在黑客上思考这样做。创建一个新表单并将其设置为TopMost。如果要显示对话框,请从中调用:

Form1.cs的

private void Form1_Load(object sender, EventArgs ev)
{
    var f2 = new Form2() { TopMost = true, Visible = false };
    var sv = new SaveFileDialog();

    MouseDown += (s, e) =>
    {
        var result = f2.ShowSave(sv);
    };
}

Form2.cs

public DialogResult ShowSave(SaveFileDialog saveFileDialog)
{
    return saveFileDialog.ShowDialog(this);
}

答案 2 :(得分:0)

我解决了这个参考布鲁诺的答案:)

我的代码就是这个......

public System.Windows.Forms.DialogResult ShowSave(System.Windows.Forms.SaveFileDialog saveFileDialog)
{
    System.Windows.Forms.DialogResult result = new System.Windows.Forms.DialogResult();

    Window win = new Window();
    win.ResizeMode = System.Windows.ResizeMode.NoResize;
    win.WindowStyle = System.Windows.WindowStyle.None;
    win.Topmost = true;
    win.Visibility = System.Windows.Visibility.Hidden;
    win.Owner = this.shell;

    win.Content = saveFileDialog;
    win.Loaded += (s, e) =>
    {
        result = saveFileDialog.ShowDialog();
    };
    win.ShowDialog();

    return result;
}

答案 3 :(得分:0)

对于任何类型的FileDialog,都是更为通用的WPF形式:

public static class ModalFileDialog
{
    /// <summary>
    /// Open this file dialog on top of all other windows
    /// </summary>
    /// <param name="fileDialog"></param>
    /// <returns></returns>
    public static bool? Show(Microsoft.Win32.FileDialog fileDialog)
    {
        Window win = new Window();
        win.ResizeMode = System.Windows.ResizeMode.NoResize;
        win.WindowStyle = System.Windows.WindowStyle.None;
        win.Topmost = true;
        win.Visibility = System.Windows.Visibility.Hidden;
        win.Content = fileDialog;

        bool? result = false;
        win.Loaded += (s, e) =>
        {
            result = fileDialog.ShowDialog();
        };
        win.ShowDialog();
        return result;
    }
}