0

I have a game in WPF, and when the user loses this particular game a window shows containing all the game details and game statistics:

The problem is that the window is set as a fixed size, so if the user would have had a smaller screen it would look like this:

enter image description here

I'm unsure how to make it so the Window fits nicely even in smaller screen dimensions. I'm not sure if this is even possible in WPF - but any help regarding this would be appreciated.

Thanks,

Tom Joney
  • 35
  • 1
  • 12
  • If you don't set any dimensions but only set to center to screen it should have reasonable defaults IIRC. – aybe Jul 17 '20 at 02:30

1 Answers1

0

For centering, use Window.WindowStartupLocation and Window.Owner:

//"dialog" being your child window
//"this" being your calling window
dialog.Owner = this;
dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;

Alternatively, you could set WindowStartupLocation="CenterOwner" in the XAML of your child window instead of in code.

For resizing, you can set your child window to be some percentage of your main window's size, or of the user's screen:

//Child window would 25% the size of the caller.
double scale = 0.25;

dialog.Width = this.Width * scale;
dialog.Height = this.Height * scale;

//Or 25% the size of the screen
dialog.Width = System.Windows.SystemParameters.WorkArea.Width * scale;
dialog.Height = System.Windows.SystemParameters.WorkArea.Height * scale;

Note that SystemParameters.WorkArea gives you the dimentions of the "primary screen", so this might not work right in a multi-monitor setup. If that's a problem you can look at this question.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36