1

(The proposed duplicate deals with a Window that starts with WindowState.Normal.)

If an application has

WindowStartupLocation = WindowStartupLocation.CenterScreen;

and starts with

WindowState = WindowState.Minimized;

and only then has

WindowState = WindowState.Normal;

The Window is not shown in the center of the screen.

Is there a way to fix this besides manually calculating the screen's center?

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 1
    Considering the property is `WindowStartupLocation` and you start it minimized, this makes sense to me. – Ryan Wilson May 13 '19 at 18:51
  • Possible duplicate of [How do you center your main window in WPF?](https://stackoverflow.com/questions/4019831/how-do-you-center-your-main-window-in-wpf) – Ryan Wilson May 13 '19 at 18:54
  • 3
    The question specifically precludes any solutions in the "duplicate". – Andy May 13 '19 at 18:57
  • @Andy Actually, no. Title vs title, perhaps, but [this answer](https://stackoverflow.com/a/4019865/1633308) does state the reality of OP's problem. – DonBoitnott May 13 '19 at 19:03
  • @DonBoitnott No. Nominally that answer assumes a later time as opposed to ***the first time*** the Window is shown. I'm referring to the first time the Windows is shown. – ispiro May 13 '19 at 19:05
  • @ispiro No, you're not. By definition, once the window is in its minimized state, startup is over. So, yes, it _is later_ that you're asking about. – DonBoitnott May 13 '19 at 19:18

2 Answers2

1

One option is to start the window center screen, but minimise it before it is actually shown on the screen, like this:

WindowStartupLocation = WindowStartupLocation.CenterScreen;
SourceInitialized += (s, e) => WindowState = WindowState.Minimized;

The SourceInitialized event is raised after the window position is set, but before it is shown.

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
0

No, there isn't. As the property suggests, it's a startup location, not one that is applied at every transition of WindowState. Once it's been displayed, you calculate.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • `Once it's been displayed, you calculate.` - Yes. But my question _is_ about the first time. – ispiro May 13 '19 at 19:06
  • 3
    @ispiro That makes no sense. The window is displayed. Startup over. Once you set `WindowState` later, it's _no longer startup_, therefore the property does not apply. You cannot wish it so. – DonBoitnott May 13 '19 at 19:17
  • Even if you're right, after a Window is in a certain location, minimizing and normalizing it will show it in the same location again. This should have happened here. – ispiro May 13 '19 at 19:31