C# WPF Titelleiste Drag and Drop

johannismueller

Cadet 3rd Year
Registriert
Aug. 2018
Beiträge
59
Hallo Liebe Community, mein Name ist Johannis und ich bin neu hier.



Ich habe eine eigene Titelleiste programmiert, da die Standard Titelleiste sich nicht Individuell sich anpassen lässt im Design.



Wenn das Programm im Vollbild Ansicht ist, möchte ich bei Klicken und Ziehen der Titelleiste das dass Programm wieder im Normalen WindowsState.Normal zustand ist, so wie man das von einer Titelleiste anderer Anwendungen kennt.

könnt Ihr mir da helfen?

XAML

XML:
 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Vertical" Grid.Row="0" VerticalAlignment="Top">
            <ContentControl Background="#262626" Height="50" MouseDoubleClick="ContentControl_MouseDoubleClick">
                <Rectangle Fill="#262626" Height="50" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown">
                    <Rectangle.ContextMenu>
                        <ContextMenu x:Name="MenuTitelleiste">
                            <MenuItem Header="Anwendung Minimieren" Click="ProgrammMinimieren_Click"/>
                            <MenuItem Header="Anwendung Maximieren" Click="ProgrammMaximieren_Click"/>
                            <MenuItem Header="Anwendung beenden" Click="ProgrammClose_Click"/>
                        </ContextMenu>
                    </Rectangle.ContextMenu>
                </Rectangle>
            </ContentControl>
        </StackPanel>
        <StackPanel Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" >
            <Label Content="Meine Titelleiste" FontSize="18" Width="150" Height="35" FontWeight="DemiBold" Foreground="Gray"></Label>
        </StackPanel>
        <StackPanel Grid.Row="0" VerticalAlignment="Top" Cursor="Hand" HorizontalAlignment="Right">
            <Button x:Name="ProgrammClose" Content="X" Width="50" Height="40" Background="Transparent" Foreground="Gray" BorderThickness="0" FontSize="20" FontWeight="Bold" Cursor="Hand" Click="ProgrammClose_Click"/>
        </StackPanel>
        <StackPanel Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0 0 50 0">
            <Button x:Name="ProgrammMaximieren" Content="◻" Width="50" Height="40" Background="Transparent" Foreground="Gray" BorderThickness="0" FontSize="25" FontWeight="Bold" Cursor="Hand" Click="ProgrammMaximieren_Click" />
        </StackPanel>
        <StackPanel Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Right" Cursor="Hand" Margin="0 0 100 0">
            <Button x:Name="ProgrammMinimieren" Content="_" Width="50" Height="40" Background="Transparent" Foreground="Gray" BorderThickness="0" FontSize="28" FontWeight="ExtraBold" Cursor="Hand" Click="ProgrammMinimieren_Click"/>
        </StackPanel>
    </Grid>

Code Behind C#

C#:
namespace Titelleiste
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ContentControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }

            else if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
        }

        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void ProgrammClose_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void ProgrammMaximieren_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }

            else if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
        }

        private void ProgrammMinimieren_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }      
    }
}
 
Du solltest dir die WindowChrome Klasse ansehen. Mit der ist es möglich, die Titelleiste zu überschreiben ohne die Standard-Features neu implementieren zu müssen. So kannst du damit zb. das Fenster auch wieder an die Seiten des Desktops docken.

Ansonsten musst du dir Gedanken machen und alles selbst implementieren :)




Code:
<Window>
<WindowChrome.WindowChrome>
  <WindowChrome CaptionHeight="35" />
</WindowChrome.WindowChrome>

</Window>
 
Zuletzt bearbeitet:
SomeDifferent schrieb:
Du solltest dir die WindowChrome Klasse ansehen. Mit der ist es möglich, die Titelleiste zu überschreiben ohne die Standard-Features neu implementieren zu müssen. So kannst du damit zb. das Fenster auch wieder an die Seiten des Desktops docken.

Ansonsten musst du dir Gedanken machen und alles selbst implementieren :)




Code:
<Window>
<WindowChrome.WindowChrome>
  <WindowChrome CaptionHeight="35" />
</WindowChrome.WindowChrome>

</Window>
Vielen Lieben dank, das hat mir sehr weitergeholfen, so das ich mein Problem in der Zukunft gelöst habe, und dies in einfacher Variante, ansonsten hätte ich noch eine Drag & Drop Funktion Implementieren müssen, schade das man hier kein Digitalen Kaffee ausgeben kann ;-)

Lösungsbeispiel

XAM-Code

XML:
<Window x:Class="NeueTitelleiste.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:NeueTitelleiste"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" WindowStyle="None" AllowsTransparency="True" WindowStartupLocation="CenterScreen">
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="35"/>
    </WindowChrome.WindowChrome>
    <Grid>
        <Grid VerticalAlignment="Top" Background="Black">
            <StackPanel Height="35" WindowChrome.IsHitTestVisibleInChrome="True" VerticalAlignment="Center" HorizontalAlignment="Right" Orientation="Horizontal">
                <Button Background="Transparent" Foreground="White" FontSize="16" FontWeight="Bold" BorderThickness="0" Name="ApplicationMinimized" Content="-" Width="35" Click="ApplicationMinimized_Click"/>
                <Button Background="Transparent" Foreground="White" FontSize="16" FontWeight="Bold" BorderThickness="0"  Name="ApplicationMaximizedAndNormal" Content="◻" Width="35" Click="ApplicationMaximizedAndNormal_Click" />
                <Button Background="Transparent" Foreground="White" FontSize="16" FontWeight="Bold" BorderThickness="0"  Name="ApplicationClose" Content="X" Width="35" Click="ApplicationClose_Click"/>
            </StackPanel>
        </Grid>
    </Grid>
</Window>

Der Code-Behind Code (C#)

C#:
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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NeueTitelleiste
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ApplicationMinimized_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void ApplicationMaximizedAndNormal_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }

            else if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
        }

        private void ApplicationClose_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }      
    }
}
 

Anhänge

  • beispiel.jpg
    beispiel.jpg
    13,5 KB · Aufrufe: 419
Zurück
Oben