简体   繁体   中英

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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