Загрузка...

Как пофиксить углы в C# WPF приложении?

Тема в разделе C# создана пользователем ap1k_Voprosik 20 апр 2025. 165 просмотров

Загрузка...
  1. ap1k_Voprosik
    ap1k_Voprosik Автор темы 20 апр 2025 0 4 июн 2024
    Я убрал стандартную верхнюю панель от Windows и наверстал свою. Но при запуске почему-то у неё вот такие вот углы слева и справа. Хотел бы узнать как можно это исправить и с чем это связано. Нейросети мне не помогли
    [IMG]
     
  2. lobotomyuser
    в верстке есть такая же хуйня, из за того что не добавил в css файл дефолтный код который фиксит эту хуйню была такая же залупа. Мб у тебя тоже чето с размерами не то друг
     
  3. xellan
    xellan 28 апр 2025 Пишу софты на C#, Bas. В тг по всем вопросам =) 237 30 мар 2020
    Не правильно переопределил стиль, и без кода на какую помощь рассчитываешь?
    Я использую следующий код для кастомного окна windows.

    HTML
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    x:Class="LearnMvvm.Styles.CustomWindow"
    xmlns:local="clr-namespace:LearnMvvm.Styles"
    xmlns:sys="clr-namespace:System;assembly=netstandard"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!--Стиль для кнопок у заголовка-->
    <Style x:Key="TopPanelBtn" TargetType="{x:Type Button}">
    <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
    <Border x:Name="root" Background="{DynamicResource Brush1}">
    <ContentPresenter/>
    </Border>
    <ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" Value="#EAEAEA" TargetName="root"/>
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>

    <!--Контент для кнопки развернуть окно-->
    <DataTemplate x:Key="ContentBtnRestore">
    <Grid>
    <Grid.Resources>
    <Viewbox x:Key="MaximizedCop" Visibility="Collapsed" Width="12" Height="12">
    <Path Stroke="#000000" StrokeThickness="1"
    Data="M8.77778 8.77778H11V1H3.22222V3.22222M8.77778 8.77778V3.22222H3.22222M8.77778 8.77778V11H1V3.22222H3.22222"></Path>
    </Viewbox>
    </Grid.Resources>
    <Border x:Name="Normal" BorderBrush="#000000" BorderThickness="1" Width="11" Height="11"></Border>
    <Viewbox x:Name="Maximized" Visibility="Collapsed" Width="12" Height="12">
    <Path Stroke="#000000" StrokeThickness="1"
    Data="M8.77778 8.77778H11V1H3.22222V3.22222M8.77778 8.77778V3.22222H3.22222M8.77778 8.77778V11H1V3.22222H3.22222"></Path>
    </Viewbox>
    </Grid>
    <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource AncestorType=Window}}" Value="Maximized">
    <Setter Property="Visibility" Value="Collapsed" TargetName="Normal"></Setter>
    <Setter Property="Visibility" Value="Visible" TargetName="Maximized"></Setter>
    </DataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>

    <!--Контент для кнопки свернуть окно-->
    <DataTemplate x:Key="ContentBtnMinimize">
    <Viewbox Width="12" Height="2">
    <Path Stroke="Black" StrokeThickness="0.8" StrokeEndLineCap="Round" StrokeStartLineCap="Round">
    <Path.Data>M11 1L1 0.999935</Path.Data>
    </Path>
    </Viewbox>
    </DataTemplate>

    <!--Стиль для кнопки закрыть окно-->
    <Style x:Key="StyleBtnClose" TargetType="Button">
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
    <Border x:Name="root" Background="{DynamicResource Brush1}">
    <Viewbox Width="14" Height="12">
    <Grid>
    <Path x:Name="s1" Stroke="Black" StrokeThickness="0.8" StrokeStartLineCap="Round" StrokeEndLineCap="Round"
    Data="M1.3999 1.60447L10.3999 10.1951"></Path>
    <Path x:Name="s2" Stroke="Black" StrokeThickness="0.8" StrokeStartLineCap="Round" StrokeEndLineCap="Round"
    Data="M10.2003 1.40485L1.60034 10.3955" Cursor=""></Path>
    </Grid>
    </Viewbox>
    </Border>
    <ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" Value="#F14C4C" TargetName="root"/>
    <Setter Property="Stroke" Value="#FFFFFF" TargetName="s1"></Setter>
    <Setter Property="Stroke" Value="#FFFFFF" TargetName="s2"></Setter>
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>

    <!--Стиль окна Window-->
    <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
    <Setter Property="WindowChrome.WindowChrome">
    <Setter.Value>
    <WindowChrome CaptionHeight="35"
    GlassFrameThickness="-2"
    NonClientFrameEdges="None"
    ResizeBorderThickness="5"
    UseAeroCaptionButtons="True" />
    </Setter.Value>
    </Setter>
    <Setter Property="Background" Value="{DynamicResource Brush1}" />
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type Window}">
    <Border x:Name="Title" Background="{TemplateBinding Background}">
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="auto"></RowDefinition>
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" VerticalAlignment="Top" Height="35" Margin="230 0 0 0" Background="{DynamicResource Brush1}">
    <StackPanel x:Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right">
    <Button Name="btnMinimize" Style="{StaticResource TopPanelBtn}" ContentTemplate="{StaticResource ContentBtnMinimize}" Width="45" Command="{x:Static local:CustomWindow.MinimizeCommand}"></Button>
    <Button Name="btnRestore" Style="{DynamicResource TopPanelBtn}" ContentTemplate="{StaticResource ContentBtnRestore}" Width="45" Command="{x:Static local:CustomWindow.MaximizeRestoreCommand}"></Button>
    <Button Name="btnClose" Style="{StaticResource StyleBtnClose}" Width="45" Command="{x:Static local:CustomWindow.CloseCommand}" WindowChrome.IsHitTestVisibleInChrome="True"></Button>
    </StackPanel>
    </Grid>
    <AdornerDecorator Grid.Row="1">
    <ContentPresenter/>
    </AdornerDecorator>
    </Grid>
    </Border>
    <ControlTemplate.Triggers>
    <!--Тригер срабатывает при разворачивание окна на весь экран и для панели с контентом задаётся BorderThickness равный толщине системной рамке-->
    <Trigger Property="WindowState" Value="Maximized">
    <Setter TargetName="Title" Property="BorderThickness" Value="{Binding Source={x:Static SystemParameters.WindowResizeBorderThickness}}"/>
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
    </ResourceDictionary>


    CSHARP
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    namespace LearnMvvm.Styles
    {
    public partial class CustomWindow : ResourceDictionary
    {

    static CustomWindow()
    {
    CommandManager.RegisterClassCommandBinding(typeof(Window), new CommandBinding(MinimizeCommand, MinimizeExecuted));
    CommandManager.RegisterClassCommandBinding(typeof(Window), new CommandBinding(MaximizeRestoreCommand, MaximizeRestoreExecuted));
    CommandManager.RegisterClassCommandBinding(typeof(Window), new CommandBinding(CloseCommand, CloseExecuted));

    }
    public static readonly RoutedCommand MinimizeCommand = new("Minimize", typeof(WindowStyle));

    public static readonly RoutedCommand MaximizeRestoreCommand = new("MaximizeRestore", typeof(WindowStyle));

    public static readonly RoutedCommand CloseCommand = new("Close", typeof(WindowStyle));

    private static void MinimizeExecuted(object sender, ExecutedRoutedEventArgs e)
    {
    var window = (Window)sender;
    window.WindowState = WindowState.Minimized;
    }
    private static void MaximizeRestoreExecuted(object sender, ExecutedRoutedEventArgs e)
    {
    var window = (Window)sender;
    if (window.WindowState == WindowState.Normal)
    {
    window.WindowState = WindowState.Maximized;
    }
    else
    {
    window.WindowState = WindowState.Normal;
    }
    }
    private static void CloseExecuted(object sender, ExecutedRoutedEventArgs e)
    {
    var window = (Window)sender;
    window.Close();
    }
    }
    }
    В App.xaml указываем словарь

    HTML
    <Application.Resources>
    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="CustomWindow.xaml"></ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Application.Resources>
    И подключаем стиль к окну
    HTML
      Style="{StaticResource CustomWindowStyle}"
    Пример результата
    [IMG]
    Края закруглены, кнопки в стиле можем менять как захотим, заголовок тоже можем кастомить как захотим в стиле
     
    28 апр 2025 Изменено
Top