0

Background: My notebook is normally placed in a docking station, to which a big screen is connected. The icon for a (WPF) app is located on the screen of the notebook. Windows (7, 64 bit) opens the app on the notebook monitor instead of the big monitor.

I can move the icon onto the big monitor, and Windows will open the app on that monitor. But after starting the notebook not attached to the docking station, the icon is placed on the notebook's monitor again (and I'd actually prefer to have it there on the small screen)...

I found some code to programmatically set the location of the main window of the application, see How to set WPF window position in secondary display That works - but: only for the main window. All further windows are opened on the screen where the icon resides.

Now, I could specify the position for all other windows, too. Or I could write some code which stores the position when a window gets closed, remembering screen (preferred or other) also, and thus re-set it when the window gets loaded depending on available screens.

But that's overkill: actually, I just want to specify that the windows get opened some where on the big screen when that is available - I do not even care for the exact position on the screen (Windows could determine that the way it does now).

What simple solution can you suggest?

Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33
  • I am wondering what you mean by "overkill". You can surely write some code to accomplish your purpose but you don't want it right? – emoacht Feb 27 '21 at 22:49

2 Answers2

0

I suggest you create a new class which is inherits "Window" class, put your codes there in the new window , after that use it in all your windows.

Example:

using System.Windows;

namespace WpfApp2
{
    public class Custom_Window_Class : Window
    {
        // Your code to reposition window (timer ...)
    }
}

=================================

<local:Custom_Window_Class x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        
    </Grid>
</local:Custom_Window_Class>

======================================

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Custom_Window_Class
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
  • Thanks for trying to help. Unfortuantely, the issue is even more complicated. Apart from having to touch all windows, it cannot be applied with MessageBoxes, OpenFileDialog, SplashScreen, ... And also the code for repositioning is sub-optimal (I have to specify Top and Left, though I am interested in screen only). – Bernhard Hiller Mar 01 '21 at 08:38
0

Unfortunately, I had to touch every single window to get things right - I'd like to call that a WTF.

In case of the type System.Windows.Window, the Owner property has to be set, plus its window.WindowStartupLocation = WindowStartupLocation.CenterOwner;. Again, that specifies more details than I wanted to specify.

In case of OpenFileDialog, the owner has to be provided in its ShowDialogmethod; with a MessageBox, in its Showmethod.

I think that it is far too much code for such a simple problem, and much more had to be specified than I actually wanted to specify. In the end, many WPF issues turn out to be WTFs.

Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33