Formular mit eigener Titelleiste

  • WPF

Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von thefiloe.

    Formular mit eigener Titelleiste

    Moin,
    ich bin gerade dabei, mir ein Formular mit eigener Titelleiste zu erstellen. Das klappt auch soweit, jedoch stehe ich jetzt vor dem Problem des Maximierens.
    wenn ich das Programm maximiere, wird das Formular größer als mein Bildschirm.
    Ich hoffe ihr könnt mir helfen, herauszufinden, warum das Formular zu groß wird und wie ich das Formular richtig maximiere.

    Gibt es eigentlich eine einfachere Methode das Formular zu resizen, als alles selbst zu berechnen?

    Gruß
    HamburgerJungeJr

    P.S. Bin relativ neu mit WPF Verbesserungen sind daher erwünscht ;)
    Formular-XAML
    Spoiler anzeigen

    XML-Quellcode

    1. <Window
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:local="clr-namespace:WPF-Test" x:Class="MainWindow"
    5. Title="MainWindow" Height="350" Width="525" WindowStyle="none" AllowsTransparency="True"
    6. Background="Transparent" Icon="Resources/favicon.ico" >
    7. <Window.Resources>
    8. <SolidColorBrush x:Key="formBorderColor" Color="#FFA200" />
    9. <Style x:Key="formBorderStyle" TargetType="Border">
    10. <Setter Property="BorderThickness" Value="3" />
    11. <Setter Property="BorderBrush" Value="{StaticResource formBorderColor}" />
    12. </Style>
    13. </Window.Resources>
    14. <Grid Background="white">
    15. <DockPanel LastChildFill="true" x:Name="dpMain">
    16. <DockPanel LastChildFill="true" Height="30px" x:Name="dpTitlebar" DockPanel.Dock="Top">
    17. <local:TitleBar VerticalAlignment="Stretch"/>
    18. </DockPanel>
    19. <Grid DockPanel.Dock="Bottom">
    20. <Border Style="{StaticResource formBorderStyle}">
    21. </Border>
    22. </Grid>
    23. </DockPanel>
    24. </Grid>
    25. </Window>


    Titelleiste-XAML
    Spoiler anzeigen

    XML-Quellcode

    1. <UserControl x:Class="TitleBar"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    6. mc:Ignorable="d"
    7. d:DesignHeight="30" d:DesignWidth="500" Background="#FFFFA200" MouseLeftButtonDown="UserControl_MouseLeftButtonDown">
    8. <UserControl.Resources>
    9. <SolidColorBrush x:Key="buttonBackground" Color="#FFA200" />
    10. <SolidColorBrush x:Key="buttonBorder" Color="#FFA200" />
    11. <SolidColorBrush x:Key="buttonBackgroundMouseOver" Color="#FFC080" />
    12. <SolidColorBrush x:Key="buttonBorderMouseOver" Color="#FFC080" />
    13. <SolidColorBrush x:Key="buttonBackgroundPressed" Color="#FF8000" />
    14. <SolidColorBrush x:Key="buttonBorderPressed" Color="#FF8000" />
    15. <SolidColorBrush x:Key="buttonForeColor" Color="#000000" />
    16. <SolidColorBrush x:Key="titleForeColor" Color="#FFFFFF" />
    17. <Style x:Key="titleStyle" TargetType="TextBlock">
    18. <Setter Property="FontSize" Value="16" />
    19. <Setter Property="FontFamily" Value="Segoe UI" />
    20. <Setter Property="Foreground" Value="{StaticResource titleForeColor}" />
    21. <Setter Property="HorizontalAlignment" Value="Left" />
    22. <Setter Property="VerticalAlignment" Value="Center" />
    23. <Setter Property="Margin" Value="10,0,0,0" />
    24. </Style>
    25. <Style x:Key="iconStyle" TargetType="Image">
    26. <Setter Property="Margin" Value="3,3,0,3" />
    27. </Style>
    28. <Style x:Key="buttonStyle" TargetType="Button">
    29. <Setter Property="Margin" Value="3,0,0,3" />
    30. <Setter Property="Padding" Value="0,0,0,5" />
    31. <Setter Property="Width" Value="30" />
    32. <Setter Property="HorizontalAlignment" Value="Right" />
    33. <Setter Property="VerticalAlignment" Value="Stretch" />
    34. <Setter Property="BorderThickness" Value="1" />
    35. <Setter Property="Background" Value="{StaticResource buttonBackground}" />
    36. <Setter Property="BorderBrush" Value="{StaticResource buttonBorder}" />
    37. <Setter Property="Template">
    38. <Setter.Value>
    39. <ControlTemplate TargetType="{x:Type Button}">
    40. <Border Background="{TemplateBinding Background}">
    41. <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
    42. </Border>
    43. </ControlTemplate>
    44. </Setter.Value>
    45. </Setter>
    46. <Style.Triggers>
    47. <Trigger Property="IsMouseOver" Value="True">
    48. <Setter Property="Background" Value="{StaticResource buttonBackgroundMouseOver}" />
    49. <Setter Property="BorderBrush" Value="{StaticResource buttonBorderMouseOver}" />
    50. </Trigger>
    51. <Trigger Property="Button.IsPressed" Value="True">
    52. <Setter Property="Background" Value="{StaticResource buttonBackgroundPressed}" />
    53. <Setter Property="BorderBrush" Value="{StaticResource buttonBorderPressed}" />
    54. </Trigger>
    55. </Style.Triggers>
    56. </Style>
    57. <Style x:Key="buttonPathStyle" TargetType="Path">
    58. <Setter Property="Fill" Value="{StaticResource buttonForeColor}" />
    59. <Setter Property="Stretch" Value="Uniform" />
    60. <Setter Property="Width" Value="10" />
    61. <Setter Property="Margin" Value="0,0,0,10" />
    62. </Style>
    63. </UserControl.Resources>
    64. <Grid>
    65. <DockPanel LastChildFill="True">
    66. <Image DockPanel.Dock="Left" Source="{Binding Icon,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}" Style="{StaticResource iconStyle}"/>
    67. <Button x:Name="sClose" DockPanel.Dock="Right" Height="30" Margin="5,0,0,0" Style="{StaticResource buttonStyle}">
    68. <Path Data="F1M54.0573,47.8776L38.1771,31.9974 54.0547,16.1198C55.7604,14.4141 55.7604,11.6511 54.0573,9.94531 52.3516,8.23962 49.5859,8.23962 47.8802,9.94531L32.0026,25.8229 16.1224,9.94531C14.4167,8.23962 11.6511,8.23962 9.94794,9.94531 8.24219,11.6511 8.24219,14.4141 9.94794,16.1198L25.8255,32 9.94794,47.8776C8.24219,49.5834 8.24219,52.3477 9.94794,54.0534 11.6511,55.7572 14.4167,55.7585 16.1224,54.0534L32.0026,38.1745 47.8802,54.0534C49.5859,55.7585 52.3516,55.7572 54.0573,54.0534 55.7604,52.3477 55.763,49.5834 54.0573,47.8776z" Style="{StaticResource buttonPathStyle}" />
    69. </Button>
    70. <Button x:Name="sMaximize" DockPanel.Dock="Right" Height="30" Margin="5,0,0,0" Style="{StaticResource buttonStyle}">
    71. <Path Data="M4.3685131,23.127279L4.3685131,47.283243 47.117023,47.283243 47.117023,23.127279z M0,10.684L53.755001,10.684 53.755001,51.668001 0,51.668001z M8.5679998,0L58.668022,0 64,0 64,5.6864691 64,45.317999 58.668022,45.317999 58.668022,5.6864691 8.5679998,5.6864691z" Style="{StaticResource buttonPathStyle}" />
    72. </Button>
    73. <Button x:Name="sMinimize" DockPanel.Dock="Right" Height="30" Margin="5,0,0,0" Style="{StaticResource buttonStyle}">
    74. <Path Data="M0,20L53.333,20 53.333,8.888 0,8.888z" Style="{StaticResource buttonPathStyle}" />
    75. </Button>
    76. <TextBlock x:Name="tbTitle" Text="{Binding Title,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}" DockPanel.Dock="Left" Style="{StaticResource titleStyle}"/>
    77. </DockPanel>
    78. </Grid>
    79. </UserControl>


    Titelleiste-VB
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Windows.Window
    2. Public Class TitleBar
    3. Private Sub UserControl_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
    4. GetWindow(Me).DragMove()
    5. End Sub
    6. Private Sub sClose_Click(sender As Object, e As RoutedEventArgs) Handles sClose.Click
    7. GetWindow(Me).Close()
    8. End Sub
    9. Private Sub sMaximize_Click(sender As Object, e As RoutedEventArgs) Handles sMaximize.Click
    10. With GetWindow(Me)
    11. If .WindowState = Windows.WindowState.Normal Then
    12. .WindowState = WindowState.Maximized
    13. Else
    14. .WindowState = Windows.WindowState.Normal
    15. End If
    16. End With
    17. End Sub
    18. End Class
    Bilder
    • FormularMaximiert.JPG

      47,21 kB, 1.920×1.080, 122 mal angesehen
    • FormularNormal.JPG

      17,87 kB, 528×355, 118 mal angesehen

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „HamburgerJungeJr“ ()