dotnet / dotnet/wpf

`CenterOwner` with Win32 owner

Open
#9,018 1 comment 3 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

I see that a code path exists for using `WindowStartupLocation.CenterOwner` with a Win32 owner, namely the `else` branch after https://source.dot.net/#PresentationFramework/System/Windows/Window.cs,3659.

And indeed, this works, _as long as_ that owner isn't maximized. If it _is_ maximized, the window position isn't what I would expect.

### Reproduction Steps

Take a WPF app template.

Enable WinForms interop in the `csproj`:

```xml
true
```

Modify `App.xaml` to not have a `StartupUri`:

```xml



```

Modify `MainWindow.xaml` to be a little smaller:

```xml

```

Finally, add a constructor to `App` that creates a WinForms form and uses `MainWindow`:

```csharp
using System.Windows;
using System.Windows.Interop;

namespace WpfApp1
{
///
/// Interaction logic for App.xaml
///
public partial class App : Application
{
public App()
{
var form = new System.Windows.Forms.Form
{
Width = 1_000,
Height = 1_000
};
form.WindowState = System.Windows.Forms.FormWindowState.Maximized;
form.Show();

var window = new MainWindow
{
WindowStartupLocation = WindowStartupLocation.CenterOwner
};

_ = new WindowInteropHelper(window)
{
Owner = form.Handle
};

window.ShowDialog();
}
}
}
```

### Expected behavior

The WPF window should show up as concentric to the form.

### Actual behavior

If we comment out `form.WindowState = System.Windows.Forms.FormWindowState.Maximized;` and thus the form shows as a regular window, this does work.

But if the form is maximized, the WPF window instead is concentric to _where the form would be if it weren't maximized_. I don't believe this behavior makes sense.

### Regression?

None.

Reproducible in `net47`, `net7.0-windows`, `net8.0-windows`.

### Known Workarounds

I would like one, especially one that works with .NET Framework 4.7.2.

I'm guessing I need to call `SetupInitialState`, perhaps by calling `CreateSourceWindow(duringShow: false)`, then fixing the location, _then_ call `Show()`?

### Impact

We have a legacy primarily WinForms app that we're adding WPF stuff to bit by bit, including by adding WPF-based dialogs — which, preferably, would center correctly.

### Configuration

- `net472`, `net7.0-windows`, `net8.0-windows`
- Windows 11 22H2 23612
- x64 (because of `net472`), ARM64

I don't think this is specific to the above configuration.

### Other information

It appears `CalculateWindowLocation` _does_ consider the case of "what if the owning WPF window is maximized", but _not_ "what if the owning Win32 form is maximized".

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.