question

youki avatar image
0 Votes"
youki asked youki commented

WinUI Window doesn't resize, if moving to other monitor with different resolution/dpi

Hi,
the window doesn't adapt it's size to the other monitor.
I'm defining the size in the grid within the window and set the size for the window in the MainWindow. See the images and code below:

First monitor:

204384-image.png


2nd monitor:

204318-image.png

Window:

 <Window
     x:Class="App1.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="using:App1"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     mc:Ignorable="d">
    
     <Grid  HorizontalAlignment="Left" 
            VerticalAlignment="Top" 
            Height="610" 
            MinWidth="1100"
            MinHeight="800">
         <NavigationView x:Name="nvSample" ItemInvoked="nvSample_ItemInvoked" PaneDisplayMode="Top" IsBackButtonVisible="Collapsed" IsSettingsVisible="False">
             <NavigationView.MenuItems>
                 <NavigationViewItem  Content="test 1" Tag="SamplePage1" />
                 <NavigationViewItem  Content="test 2" Tag="SamplePage2" />
                 <NavigationViewItem  Content="test 3" Tag="SamplePage3" />
                 <NavigationViewItem  Content="test 4" Tag="SamplePage4" />
                 <NavigationViewItem  Content="test 5" Tag="SamplePage4" />
             </NavigationView.MenuItems>
             <!--<Frame x:Name="contentFrame"/>-->
         </NavigationView>
         <Button x:Name="myButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,110,0,0" Click="myButton_Click" Content="Clic"/>
         <Button x:Name="btnClose" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,380,0,0"  Click="btnClose_Click" Content="Clic"/>
    
         <!--<StackPanel x:Name="pnlSettings" Height="570" Width="190" HorizontalAlignment="Right" VerticalAlignment="Top" Background="Yellow"></StackPanel>-->
            
         <SplitView x:Name="splitView"                
                    IsPaneOpen="False" 
                    OpenPaneLength="300" 
                    CompactPaneLength="48"
                    DisplayMode="Overlay"
                    PanePlacement="Right">
             <SplitView.Pane>
                 <Grid>
                     <Grid.RowDefinitions>
                         <RowDefinition Height="Auto"/>
                         <RowDefinition Height="*"/>
                         <RowDefinition Height="Auto"/>
                     </Grid.RowDefinitions>
                     <ToggleSwitch AutomationProperties.Name="simple ToggleSwitch"/>
                     <TextBlock Text="PANE CONTENT" x:Name="PaneHeader" Margin="60,12,0,0" Style="{StaticResource BaseTextBlockStyle}"/>
                     <!--<ListView x:Name="NavLinksList" Margin="0,12,0,0" SelectionMode="Single" Grid.Row="1" VerticalAlignment="Stretch"
                     ItemClick="NavLinksList_ItemClick" IsItemClickEnabled="True"/>-->
                     <StackPanel Orientation="Horizontal" Grid.Row="2" Margin="14,24,0,24" >
                         <SymbolIcon Symbol="Setting" />
                         <TextBlock Text="Settings" Margin="24,0,0,0" VerticalAlignment="Center"/>
                     </StackPanel>
                 </Grid>
             </SplitView.Pane>
    
             <!--<Grid>
                 <Grid.RowDefinitions>
                     <RowDefinition Height="Auto"/>
                     <RowDefinition Height="*"/>
                 </Grid.RowDefinitions>
                 <TextBlock Text="SPLITVIEW CONTENT" Margin="12,12,0,0" Style="{StaticResource BaseTextBlockStyle}"/>
                 <TextBlock x:Name="content" Grid.Row="1" Margin="12,12,0,0" Style="{StaticResource BodyTextBlockStyle}" />
             </Grid>-->
         </SplitView>
    
     </Grid>
 </Window>


MainWindow():

         // Retrieve the window handle (HWND) of the current WinUI 3 window.
         auto windowNative{ this->try_as<::IWindowNative>() };
         winrt::check_bool(windowNative);   
         HWND hWnd{ 0 };
         windowNative->get_WindowHandle(&hWnd);       
         s_handle = hWnd;
    
         // Make it not resizable.  
         Microsoft::UI::WindowId windowId = Microsoft::UI::GetWindowIdFromWindow(hWnd); // Retrieve the WindowId that corresponds to hWnd.
         Microsoft::UI::Windowing::AppWindow appWindow = Microsoft::UI::Windowing::AppWindow::GetFromWindowId(windowId); // Retrieve the AppWindow for the current (XAML) WinUI 3 window.      
         Microsoft::UI::Windowing::OverlappedPresenter presenter = appWindow.Presenter().as<Microsoft::UI::Windowing::OverlappedPresenter>();
         presenter.IsResizable(false);
    
         // Set window size
         winrt::Windows::Graphics::SizeInt32 size{};
         size.Height = 800;
         size.Width = 1190;
         appWindow.Resize(size);

Added 05/24/2022 - 6.20 am

204829-image.png

204830-image.png


c++windows-app-sdk
image.png (32.0 KiB)
image.png (15.8 KiB)
image.png (14.4 KiB)
image.png (16.1 KiB)
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Uhmm, WinUi sucks!
https://github.com/microsoft/microsoft-ui-xaml/issues/6720

What's the alternative for c++?

0 Votes 0 ·

Have you tried using Stretch?

 HorizontalAlignment="Stretch" 
 VerticalAlignment="Stretch" 
0 Votes 0 ·
youki avatar image youki JunjieZhu-MSFT ·

I've tried it and i think i understand the idea behind it but i'd like to have the normal behaviour.

I've tried the code of dotMorten in the WM_DPICHANGED and it's exactly the same from here:
https://docs.microsoft.com/en-us/windows/win32/hidpi/wm-dpichanged

I don't really understand it, do i also have to handle the title bar because the WM_DPICHANGED code alone doesn't work.


0 Votes 0 ·
Show more comments

1 Answer

youki avatar image
1 Vote"
youki answered youki commented

Ok,
I'm a donk. I don't want to say something wrong but it works by SetWindowPos.
I positioned it wrong in the message loop. :(
Thanks you!

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Although I also use C++ a lot, I still recommend using C# in winui3. :)

0 Votes 0 ·
youki avatar image youki JunjieZhu-MSFT ·

I know there has been a lot of discussion about this and I can just confirm an argument as I've been working in a huge corporation for 1 month where it would be a problem to consider the .Net Framework version or to get the installation approved. I've seen that in previous companies. I've had my tool in Windows Forms for maybe a year now and I can't use it anymore, which really annoys me.

Regards

1 Vote 1 ·