开发者

How can I set topmost at the SaveFileDialog using C#?

I want to set topmost my SaveFileDialog. But as you know there is no property. Is th开发者_如何学编程ere any other way to set TopMost at SaveFileDialog?


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


I can only think on a hack to do this. Make a new Form and set it TopMost. When you want to show the dialog, call from it:

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


I solved this ref Bruno's answer :)

My code is this...

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


As a more generic WPF-ish for any type of FileDialog:

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;
    }
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜