0

次のプロパティを割り当てて全画面表示にしたWPF ウィンドウがあります。

WindowState = Maximized
WindowStyle = None
Topmost = true

これはこれまでのところ非常にうまく機能しています。今TextBlocks、私Windowは両方とも水平方向に中央揃えしたいものを2つ手に入れました。フルスクリーンなので、画面の解像度から位置を計算するだけでした。だから私は次のことを試しました:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    IntPtr handle = WinApi.getWindowByName("myWindow");
    int height = Screen.FromHandle(handle).Bounds.Height;
    int width = Screen.FromHandle(handle).Bounds.Width;
    textBlock1.Margin = new Thickness(width / 2 - textBlock1.ActualWidth, height / 10, width / 2 - textBlock1.ActualWidth, height / 1.5);
    textBlock2.Margin = new Thickness(width / 2 - textBlock2.ActualWidth, height / 10, width / 2 - textBlock2.ActualWidth, height / 3);
}

WinApiをカプセル化する私からのクラスですWinApiActualWidth両方の幅TextBlocksが に設定されているので使用していAutoます。画面の寸法を取得することは、これまでのところうまくいきます。ただし、textBlocks は画面の真ん中に正確にレンダリングされません。どちらも異なる水平位置でレンダリングされているため、確かにわかります。

私のXAML:

<Window x:Class="MyApp.MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="test" Height="300" Width="300" WindowState="Maximized" WindowStyle="None" Topmost="True" Background="#FF0000DC" Foreground="#FFF4FCF8" Loaded="Window_Loaded">

    <Grid>
        <TextBlock x:Name="textBlockHeader" HorizontalAlignment="Center" Background="White" Foreground="#FF0C04DB" FontWeight="Bold" FontFamily="Lucida Console" Width="Auto" Height="Auto" Text="Header" TextAlignment="Center"/>
        <TextBlock x:Name="textBlockText" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" FontFamily="Lucida Console" Text="text"/>
    </Grid>
</Window>
4

2 に答える 2