【问题标题】:Displaying modal window from WPF Browser Application, e.g. MessageBox从 WPF 浏览器应用程序显示模式窗口,例如消息框
【发布时间】:2015-12-05 20:56:45
【问题描述】:

原生 WPF MessageBox 调用 UnsafeNativeMethods.GetActiveWindow(),这似乎是为了定位 Internet Explorer 并将 IE 设置为对话框的所有者。 Evk 提供了访问此方法的说明,但窗口仍然不是模态的。还有更多我不明白的事情,我想锁定 IE,直到用户从我自己的自定义对话框返回。

public DialogResult ShowInput<T>(string messageText, string caption, out T input)
{
    var window = new InputDialog<T>(messageText, caption);

    var helper = new WindowInteropHelper(window);
    helper.Owner = NativeMethods.GetActiveWindow();

    if (window.ShowDialog() == true)
    {
        input = window.Input;
        return DialogResult.OK;
    }
    else
    {
        input = default(T);
        return DialogResult.Cancel;
    }
}

public static class NativeMethods
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetActiveWindow();
}

【问题讨论】:

  • WPF 消息框与 IE 有什么关系?如果您想显示一个模态对话框,just use what WPF offers you... 如果这不是您要求的,请更具体地提出您的问题。
  • @elgonzo 如果您创建 WPF 浏览器应用程序,并在窗口上调用 ShowDialog() 时使用 IE 启动它,它不会将 IE 作为父级,MessageBox.Show 会尊重 IE。我想知道如何复制此功能。
  • 好吧,我不得不承认我从来没有在浏览器中使用过 WPF :( 所以你想阻止整个 IE,而不仅仅是你的应用程序的页面......嗯......

标签: c# .net wpf


【解决方案1】:

如果我对您的理解正确,您只想像 UnsafeNativeMethods.GetActiveWindow 那样做。然后你可以使用:

public static class NativeMethods {
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetActiveWindow();
}

从 user32.dll 调用原生 GetActiveWindow windows api(这正是 UnsafeNativeMethods.GetActiveWindow 所做的)。

【讨论】:

  • 谢谢,我可以用它来设置所有者。不幸的是,设置所有者似乎并不是整个解决方案。窗口现在位于最上面,但没有模式。我可以与我的页面和 IE 交互。
猜你喜欢
  • 1970-01-01
  • 2011-04-05
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
相关资源
最近更新 更多