简体   繁体   English

使用WPF,要以全屏方式不在主屏幕中打开第二个窗口

[英]With WPF, want to open a second window full screen not in the primary screen

With WPF, want to open a second window full screen. 使用WPF,要全屏打开第二个窗口。 The problem is when the main window is not in the primary screen, the second window doesn't open in the same screen like the main window. 问题是,当主窗口不在主屏幕中时,第二个窗口无法在与主窗口相同的屏幕中打开。 It popups inside the primary screen. 它会在主屏幕中弹出。 Is there anyway I can get it open inside the same screen like the main window? 无论如何,我可以在与主窗口相同的屏幕中打开它吗? Thanks. 谢谢。

You should be able to do this by setting the startup location for the new window to center on its owner. 您应该能够通过将新窗口的启动位置设置为以其所有者为中心来执行此操作。 The gotcha to doing that is that you don't get the window it's opened from set as the owner automatically so you need to do it yourself. 这样做的陷阱是,您不会自动从设置为所有者的窗口中打开该窗口,因此您需要自己进行操作。 Doing this from another Window should open the Window2 instance full screen on the same monitor: 从另一个Window执行此操作应在同一监视器上全屏打开Window2实例:

Window2 newWindow = new Window2
{
    Owner = this,
    WindowStartupLocation = WindowStartupLocation.CenterOwner,
    WindowState = WindowState.Maximized
};
newWindow.Show();

given window is the instance of the window you want to center 给定的窗口是您要居中的窗口的实例

var window = new MyWpfWindow();

var handle = Process.GetCurrentProcess().MainWindowHandle;
var helper = new WindowInteropHelper(window);
helper.Owner = handle; 
window.WindowState = WindowState.Maximized;         
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;

window.ShowDialog();

this will automatically retrieve the current main window and centers and maximize the child in the same screen, it works for me in an Excel VSTO add-ins 这将自动检索当前的主窗口并居中,并在同一屏幕上最大化子窗口,它在Excel VSTO加载项中对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM