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

删除回忆录丶 提交于 2019-12-24 16:16:08

问题


I want to set topmost my SaveFileDialog. But as you know there is no property. Is there any other way to set TopMost at SaveFileDialog?


回答1:


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



回答2:


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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/4666580/how-can-i-set-topmost-at-the-savefiledialog-using-c

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