Zincup
Zincup

Reputation: 1

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.

Upvotes: 0

Views: 2001

Answers (2)

Mosè Bottacini
Mosè Bottacini

Reputation: 4216

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

Upvotes: 0

John Bowen
John Bowen

Reputation: 24453

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

Upvotes: 1

Related Questions