12

XAML で次のウィンドウ プロパティを設定して、ボーダレス WPF ウィンドウを作成しました。

... WindowStyle="None" AllowsTransparency="True" ...

これにより、いくつかの問題が発生します。

1) 解決済み:サイズ変更機能が組み込まれていません。

2) 解決済み:ビルトインのドラッグ機能がなくなりました

3) 解決済み:上部のツールバーがないと、最小化/最大化/復元/閉じるボタンがなくなりました

4) 解決済み:エアロ スナップを介して最大化するか、WindowState を設定すると、スナップが解除されなくなります。

5)エアロ スナップを介して最大化するか、WindowState を設定すると、画面全体が境界として使用され、ウィンドウ ツールバーが重なります。

6)エアロ スナップによる最大化または WindowState の設定には -7 のマージンが含まれているようで、ウィンドウの端を超えた各辺に 7 ピクセルがウィンドウに与えられます。

1~3はxamlウィンドウテンプレートを作ることで解決しました。目に見えない四角形をハンドル領域として使用し、その背後にあるコードを OnApplyTemplate() のオーバーライドを介して適用し、サイズ変更/移動/最小化/最大化/復元/閉じるの user32.dll SendMessage(...) を介して機能をアタッチしました。

#4 の答えはこちら

WndProc を介して最大化メッセージをインターセプトし、サイズ/位置を手動で設定して 5-6 を解決しようとしましたが、RestoreRegion を最大化されたサイズ/位置に上書きし、ウィンドウを復元する機能を削除するという問題がありました。

本当に奇妙なのは、上部の境界線から画面の上部までウィンドウのサイズを変更すると、エアロのフルハイトスナップが問題なくトリガーされ、まったく問題がないことです.

だから、私は長い道のりを歩んできましたが、5-6 はまだ問題です.最大化領域を手動で指定する方法はありますか? または、restoreregion プロパティに影響を与えずにウィンドウ サイズを設定する方法はありますか?

4

6 に答える 6

5

私は自分でこのすべてを経験しました。手作業で多くのことを説明しなければならないので、それは本当に面倒でした。面白いことに、私たちは最近、基本的なウィンドウの操作方法と同じくらい単純なものを当然のことと考えています。しかし、私が提供しているこのサンプル コードを見ると、この問題に実際にどれだけのことがかかっているかがよくわかります。

自分でここにたどり着くのに少し時間がかかったので、これが役に立てば幸いです。

MainWindow.Xaml

<Window x:Class="WpfApp1.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:WpfApp1"
    Background="Transparent"
    WindowStartupLocation="CenterScreen"
    ResizeMode="CanResizeWithGrip"
    AllowsTransparency="True"
    WindowStyle="None"
    mc:Ignorable="d"
    Title="Test Window Behavior" Height="768" Width="1024" StateChanged="Window_StateChanged" PreviewMouseLeftButtonDown="Window_PreviewMouseLeftButtonDown">

<Grid>
    <DockPanel Grid.Column="1" Grid.Row="1">
        <DockPanel x:Name="titleBar" Background="White" DockPanel.Dock="Top">
            <Rectangle Width="32" Height="32" DockPanel.Dock="Left" Fill="Red" Margin="2"/>
            <StackPanel Orientation="Horizontal" DockPanel.Dock="Right" Margin="2">

                <!-- Minimize Button -->
                <Border Width="24" Height="24" Margin="2" HorizontalAlignment="Right" MouseLeftButtonUp="OnMinimizeWindow" Grid.Column="2">
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="Background" Value="Transparent" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#FFD0D0D0" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <TextBlock FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" Text="0" FontFamily="Webdings" />
                </Border>

                <!-- Maximize Button -->
                <Border Width="24" Height="24" Margin="2" HorizontalAlignment="Right" MouseLeftButtonUp="OnMaximizeWindow" Grid.Column="3">
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="Background" Value="Transparent" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#FFD0D0D0" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <TextBlock x:Name="IsMaximized" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Webdings">
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Setter Property="Text" Value="1" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=InternalWindowState, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Value="Maximized">
                                        <Setter Property="Text" Value="2" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </Border>

                <!-- Close Button -->
                <Border Width="24" Height="24" Margin="2" HorizontalAlignment="Right" MouseLeftButtonUp="OnCloseWindow" Grid.Column="4">
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="Background" Value="Transparent" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="Red" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <TextBlock FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" Text="r" FontFamily="Webdings" />
                </Border>
            </StackPanel>

            <Label MouseLeftButtonDown="OnDragMoveWindow" MouseDoubleClick="OnMaximizeWindow" Margin="8 0 0 0" FontSize="12" VerticalContentAlignment="Center" Content="{Binding Path=Title, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, FallbackValue='Main Window'}" />
        </DockPanel>

        <Grid Background="White" DockPanel.Dock="Bottom" Height="32">
            <Label VerticalContentAlignment="Center" Content="Statusbar Text Goes Here ..." />
        </Grid>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="100" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="100" />
                <RowDefinition Height="*" />
                <RowDefinition Height="100" />
            </Grid.RowDefinitions>

            <!-- Top 3 -->
            <Border Background="Gray" Grid.Row="0" Grid.Column="0" />
            <Border Background="Gray" Grid.Row="0" Grid.Column="1" BorderBrush="Black" BorderThickness="0 0 0 1" />
            <Border Background="Gray" Grid.Row="0" Grid.Column="2" />

            <!-- Middle 2 -->
            <Border Background="Gray" Grid.Row="1" Grid.Column="0" BorderBrush="Black" BorderThickness="0 0 1 0" />
            <Border Background="Gray" Grid.Row="1" Grid.Column="2" BorderBrush="Black" BorderThickness="1 0 0 0" />

            <!-- Bottom 3 -->
            <Border Background="Gray" Grid.Row="2" Grid.Column="0" />
            <Border Background="Gray" Grid.Row="2" Grid.Column="1" BorderBrush="Black" BorderThickness="0 1 0 0" />
            <Border Background="Gray" Grid.Row="2" Grid.Column="2" />
        </Grid>
    </DockPanel>
    <Grid>
        <Grid.Resources>
            <Style TargetType="Thumb">
                <Style.Setters>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Border Background="Transparent" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="25" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="*" />
            <RowDefinition Height="25" />
        </Grid.RowDefinitions>

        <!-- Top/Left -->
        <DockPanel LastChildFill="False" Grid.Row="0" Grid.Column="0">
            <Thumb DockPanel.Dock="Left" Width="4" Cursor="SizeNWSE" Tag="0" DragDelta="Thumb_DragDelta" />
            <Thumb DockPanel.Dock="Top" Height="4" Cursor="SizeNWSE" Tag="0" DragDelta="Thumb_DragDelta" />
        </DockPanel>

        <!-- Top/Right -->
        <DockPanel LastChildFill="False" Grid.Row="0" Grid.Column="2">
            <Thumb DockPanel.Dock="Right" Width="4" Cursor="SizeNESW" Tag="0" DragDelta="Thumb_DragDelta" />
            <Thumb DockPanel.Dock="Top" Height="4" Cursor="SizeNESW" Tag="0" DragDelta="Thumb_DragDelta" />
        </DockPanel>

        <!-- Bottom/Left -->
        <DockPanel LastChildFill="False" Grid.Row="2" Grid.Column="0">
            <Thumb DockPanel.Dock="Left" Width="4" Cursor="SizeNESW" Tag="1" DragDelta="Thumb_DragDelta" />
            <Thumb DockPanel.Dock="Bottom" Height="4" Cursor="SizeNESW" Tag="1" DragDelta="Thumb_DragDelta" />
        </DockPanel>

        <!-- Bottom/Right -->
        <DockPanel LastChildFill="False" Grid.Row="2" Grid.Column="2">
            <Thumb DockPanel.Dock="Right" Width="4" Cursor="SizeNWSE" Tag="1" DragDelta="Thumb_DragDelta" />
            <Thumb DockPanel.Dock="Bottom" Height="4" Cursor="SizeNWSE" Tag="1" DragDelta="Thumb_DragDelta" />
        </DockPanel>

        <!-- Left -->
        <Thumb Grid.Row="1" Grid.Column="0" Width="4" Cursor="SizeWE" Tag="0" HorizontalAlignment="Left" DragDelta="Thumb_DragDelta" />

        <!-- Top -->
        <Thumb Grid.Row="0" Grid.Column="1" Height="4" Cursor="SizeNS" Tag="0" VerticalAlignment="Top" DragDelta="Thumb_DragDelta" />

        <!-- Right -->
        <Thumb Grid.Row="1" Grid.Column="2" Width="4" Cursor="SizeWE" Tag="1" HorizontalAlignment="Right" DragDelta="Thumb_DragDelta" />

        <!-- Bottom -->
        <Thumb Grid.Row="2" Grid.Column="1" Height="4" Cursor="SizeNS" Tag="1" VerticalAlignment="Bottom" DragDelta="Thumb_DragDelta" />
    </Grid>
</Grid>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
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 WpfApp1
{
    public partial class MainWindow : Window
    {
        #region --- Declarations ---
        private Rect _location { get; set; }
        #endregion

        #region --- Constructors ---
        public MainWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region --- Properties ---
        private Rect DesktopArea
        {
            get
            {
                var c = System.Windows.Forms.Cursor.Position;
                var s = System.Windows.Forms.Screen.FromPoint(c);
                var a = s.WorkingArea;
                return new Rect(a.Left, a.Top, a.Width, a.Height);
            }
        }
        #endregion

        #region --- Dependency Properties ---
        public static readonly DependencyProperty InternalWindowStateProperty = DependencyProperty.Register("InternalWindowState", typeof(WindowState), typeof(MainWindow), new PropertyMetadata(WindowState.Normal, new PropertyChangedCallback(OnInternalWindowStateChanged)));

        public WindowState InternalWindowState
        {
            get { return (WindowState)GetValue(InternalWindowStateProperty); }
            set { SetValue(InternalWindowStateProperty, value); }
        }

        private static void OnInternalWindowStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MainWindow instance = (MainWindow)d;
            instance.SetInternalWindowState((WindowState)e.NewValue);
        }
        #endregion

        #region --- Private Methods ---
        private void StoreLocation()
        {
            _location = new Rect(this.Left, this.Top, this.Width, this.Height);
        }

        private void RestoreLocation()
        {
            this.Width = _location.Width;
            this.Height = _location.Height;
            this.Top = _location.Top >= 0 ? _location.Top : 0;
            this.Left = _location.Left;
        }

        private void SetMaximizedState()
        {
            this.Width = DesktopArea.Width;
            this.Height = DesktopArea.Height;
            this.Top = DesktopArea.Top;
            this.Left = DesktopArea.Left;
        }

        private void SetInternalWindowState(WindowState state)
        {
            InternalWindowState = state;

            switch (InternalWindowState)
            {
                case WindowState.Normal:
                    this.WindowState = WindowState.Normal;
                    RestoreLocation();
                    break;
                case WindowState.Maximized:
                    this.WindowState = WindowState.Normal;
                    SetMaximizedState();
                    break;
                case WindowState.Minimized:
                    this.WindowState = WindowState.Minimized;
                    break;
            }
        }
        #endregion

        #region --- Sizing Routines --- 
        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Thumb thumb = (Thumb)sender;
            int tag = Convert.ToInt32(thumb.Tag);
            if (thumb.Cursor == Cursors.SizeWE) HandleSizeWE(tag, e);
            if (thumb.Cursor == Cursors.SizeNS) HandleSizeNS(tag, e);
            if (thumb.Cursor == Cursors.SizeNESW) HandleSizeNESW(tag, e);
            if (thumb.Cursor == Cursors.SizeNWSE) HandleSizeNWSE(tag, e);
        }

        private void HandleSizeNWSE(int tag, DragDeltaEventArgs e)
        {
            if (tag == 0)
            {
                this.Top += e.VerticalChange;
                this.Height -= e.VerticalChange;
                this.Left += e.HorizontalChange;
                this.Width -= e.HorizontalChange;
            }
            else
            {
                this.Width += e.HorizontalChange;
                this.Height += e.VerticalChange;
            }
        }

        private void HandleSizeNESW(int tag, DragDeltaEventArgs e)
        {
            if (tag == 0)
            {
                this.Top += e.VerticalChange;
                this.Height -= e.VerticalChange;
                this.Width += e.HorizontalChange;
            }
            else
            {
                this.Left += e.HorizontalChange;
                this.Width -= e.HorizontalChange;
                this.Height += e.VerticalChange;
            }
        }

        private void HandleSizeNS(int tag, DragDeltaEventArgs e)
        {
            if (tag == 0)
            {
                this.Top += e.VerticalChange;
                this.Height -= e.VerticalChange;
            }
            else
                this.Height += e.VerticalChange;
        }

        private void HandleSizeWE(int tag, DragDeltaEventArgs e)
        {
            if (tag == 0)
            {
                this.Left += e.HorizontalChange;
                this.Width -= e.HorizontalChange;
            }
            else
                this.Width += e.HorizontalChange;
        }
        #endregion

        #region --- Event Handlers ---
        private void OnDragMoveWindow(Object sender, MouseButtonEventArgs e)
        {
            if (this.InternalWindowState == WindowState.Maximized)
            {
                var c = System.Windows.Forms.Cursor.Position;
                this.InternalWindowState = WindowState.Normal;
                this.Height = _location.Height;
                this.Width = _location.Width;
                this.Top = c.Y - (titleBar.ActualHeight / 2);
                this.Left = c.X - (_location.Width / 2);
            }
            this.DragMove();
        }

        private void OnMaximizeWindow(Object sender, MouseButtonEventArgs e)
        {
            if (this.InternalWindowState == WindowState.Maximized)
                this.InternalWindowState = WindowState.Normal;
            else
                this.InternalWindowState = WindowState.Maximized;
        }

        private void OnMinimizeWindow(Object sender, MouseButtonEventArgs e)
        {
            this.InternalWindowState = WindowState.Minimized;
        }

        private void OnCloseWindow(Object sender, MouseButtonEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.InternalWindowState = WindowState.Maximized;
            }
        }

        private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.InternalWindowState != WindowState.Maximized)
                StoreLocation();
        }
        #endregion
    }
}
于 2018-02-05T15:33:26.540 に答える
3

ポイント番号 5 については、これを使用します。

public WindowName() // Constructor for your window
{
this.MaxHeight = SystemParameters.WorkArea.Height;
this.MaxWidth = SystemParameters.WorkArea.Width;
}

これにより、ウィンドウが最大化されたときにタスク バーと重ならなくなります。

于 2015-07-25T19:34:09.227 に答える
2

WM_GETMINMAXINFOWin32 メッセージを処理することで、最大化領域を指定できます。ここのコードは、その方法を示しています。これにより、問題 5 と 6 が解決されます。

IntPtr.ZeroWindowProc で (System.IntPtr)0 の代わりに戻りMONITOR_DEFAULTTONEAREST、定数を作成するなど、別の方法で行うことがいくつかあることに注意してください。しかし、それはコーディング スタイルの変更にすぎず、最終的な結果には影響しません。

また、イベントWindowProc中に ではなく がフックされるアップデートにも注意してください。それはそれを行うのに適した場所です。Window から派生したクラスを実装している場合、もう 1 つのオプションは、イベントにアタッチする代わりに、オーバーライドしてフックすることです。それが私が通常していることです。SourceInitializedOnApplyTemplateOnSourceInitializedWindowProc

于 2013-06-08T02:45:07.517 に答える
1

これらすべての問題について、私はこれしかお勧めできません:

MahApps.Metro: http://mahapps.com/MahApps.Metro/

ソースコード: https://github.com/MahApps/MahApps.Metro

素敵なテーマで使いやすい素敵なライブラリです!

それが役立つことを願っています

于 2012-11-16T19:08:29.777 に答える