WPF Custom Window Resize Issue

我的未来我决定 提交于 2019-12-25 02:01:53

问题


I have exactly the same problem like this one

Everything is fine, but when i resize from West to East or North to South, the East/South side of the windows is kinda shaky, and it doesn't look very good.

As i can see this post is from long, long time ago, and still there is no valid answer in it, i was looking around, but couldn't find anything, anyone can tell me if this issue is fixed and where i can find the solution?

[xaml]:

<ControlTemplate>
    <!-- MainGrid -->
    <Grid x:Name="PART_MainGrid">
        <!-- MainGrid.Rows -->
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" x:Name="PART_MainGridRow0" />
            <RowDefinition Height="Auto" x:Name="PART_MainGridRow1" />
            <RowDefinition Height="*" x:Name="PART_MainGridRow2" />
            <RowDefinition Height="Auto" x:Name="PART_MainGridRow3" />
            <RowDefinition Height="Auto" x:Name="PART_MainGridRow4" />
        </Grid.RowDefinitions>
        <!-- MainGrid.Columns -->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" x:Name="PART_MainGridCol0"/>
            <ColumnDefinition Width="Auto" x:Name="PART_MainGridCol1"/>
            <ColumnDefinition Width="*" x:Name="PART_MainGridCol2"/>
            <ColumnDefinition Width="Auto" x:Name="PART_MainGridCol3"/>
            <ColumnDefinition Width="Auto" x:Name="PART_MainGridCol4"/>
        </Grid.ColumnDefinitions>
        <!-- MainGrid.ShadowBorders -->
        <Border x:Name="PART_ShadowBorderWest"      Grid.Row="1" Grid.Column="0" Grid.RowSpan="3"/>
        <Border x:Name="PART_ShadowBorderNorth"     Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3"/>            
        <Border x:Name="PART_ShadowBorderEast"      Grid.Row="1" Grid.Column="4" Grid.RowSpan="3"/>
        <Border x:Name="PART_ShadowBorderSouth"     Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="3"/>
        <Border x:Name="PART_ShadowBorderNorthWest" Grid.Row="0" Grid.Column="0"/>
        <Border x:Name="PART_ShadowBorderNorthEast" Grid.Row="0" Grid.Column="4"/>
        <Border x:Name="PART_ShadowBorderSouthEast" Grid.Row="4" Grid.Column="4"/>                
        <Border x:Name="PART_ShadowBorderSouthWest" Grid.Row="4" Grid.Column="0"/>
        <!-- MainGrid.Borders -->
        <Border x:Name="PART_BorderWest"      Grid.Row="2" Grid.Column="1"/>
        <Border x:Name="PART_BorderNorth"     Grid.Row="1" Grid.Column="2"/>
        <Border x:Name="PART_BorderEast"      Grid.Row="2" Grid.Column="3"/>
        <Border x:Name="PART_BorderSouth"     Grid.Row="3" Grid.Column="2"/>
        <Border x:Name="PART_BorderNorthWest" Grid.Row="1" Grid.Column="1"/>
        <Border x:Name="PART_BorderNorthEast" Grid.Row="1" Grid.Column="3"/>
        <Border x:Name="PART_BorderSouthEast" Grid.Row="3" Grid.Column="3">                
        <Border x:Name="PART_BorderSouthWest" Grid.Row="3" Grid.Column="1"/>
    </Grid>
</ControlTemplate>

[c#]:

   // Global Variable
   private Point cursorOffset;
   #region All Resize Events and Functions
        public void AddNorthWestResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeNWSE;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.MouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.X = cursorLocation.X;
                    cursorOffset.Y = cursorLocation.Y;

                    element.CaptureMouse();
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                    double nVerticalChange = (cursorLocation.Y - cursorOffset.Y);

                    if (Width - nHorizontalChange > MinWidth)
                    {
                        Left += nHorizontalChange;
                        Width -= nHorizontalChange;
                    }
                    if (Height - nVerticalChange > MinHeight)
                    {
                        Top += nVerticalChange;
                        Height -= nVerticalChange;
                    }
                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }
        public void AddNorthResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeNS;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.MouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.Y = cursorLocation.Y;

                    element.CaptureMouse();
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                    double nVerticalChange = (cursorLocation.Y - cursorOffset.Y);

                    if (Height - nVerticalChange > MinHeight)
                    {
                        Top += nVerticalChange;
                        Height -= nVerticalChange;
                    }
                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }
        public void AddNorthEastResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeNESW;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.MouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.X = (Width - cursorLocation.X);
                    cursorOffset.Y = cursorLocation.Y;

                    element.CaptureMouse();
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                    double pHorizontalChange = (cursorLocation.X + cursorOffset.X);
                    double nVerticalChange = (cursorLocation.Y - cursorOffset.Y);
                    double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);

                    if (pHorizontalChange > MinWidth)
                        Width = pHorizontalChange;
                    if (Height - nVerticalChange > MinHeight)
                    {
                        Top += nVerticalChange;
                        Height -= nVerticalChange;
                    }
                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }
        public void AddEastResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeWE;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.MouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.X = (Width - cursorLocation.X);

                    element.CaptureMouse();
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double pHorizontalChange = (cursorLocation.X + cursorOffset.X);

                    if (pHorizontalChange > MinWidth)
                        Width = pHorizontalChange;
                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }
        public void AddSouthEastResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeNWSE;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.MouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.X = (Width - cursorLocation.X);
                    cursorOffset.Y = (Height - cursorLocation.Y);

                    element.CaptureMouse();
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double pHorizontalChange = (cursorLocation.X + cursorOffset.X);
                    double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);

                    if (pHorizontalChange > MinWidth)
                        Width = pHorizontalChange;
                    if (pVerticalChange > MinHeight)
                        Height = pVerticalChange;
                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }
        public void AddSouthResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeNS;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.MouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.Y = (Height - cursorLocation.Y);

                    element.CaptureMouse();
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);

                    if (pVerticalChange > MinHeight)
                        Height = pVerticalChange;

                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }
        public void AddSouthWestResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeNESW;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.MouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.X = cursorLocation.X;
                    cursorOffset.Y = (Height - cursorLocation.Y);

                    element.CaptureMouse();
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                    double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);

                    if (Width - nHorizontalChange > MinWidth)
                    {
                        Left += nHorizontalChange;
                        Width -= nHorizontalChange;
                    }
                    if (pVerticalChange > MinHeight)
                        Height = pVerticalChange;
                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }
        public void AddWestResizeEvents(FrameworkElement element)
        {
            if (element == null)
                return;

            element.MouseEnter += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    element.Cursor = Cursors.SizeWE;
                else
                    element.Cursor = Cursors.Arrow;
            };

            element.PreviewMouseLeftButtonDown += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    cursorOffset.X = cursorLocation.X;

                    element.CaptureMouse();                    
                }
            };

            element.MouseMove += (sender, e) =>
            {
                if (WindowState != WindowState.Maximized && element.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                {
                    Point cursorLocation = e.GetPosition(this);

                    double nHorizontalChange = (cursorLocation.X - cursorOffset.X);

                    if (Width - nHorizontalChange > MinWidth)
                    {
                        Left += nHorizontalChange;
                        Width -= nHorizontalChange;
                    }
                }
            };

            element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
        }

来源:https://stackoverflow.com/questions/21680401/wpf-custom-window-resize-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!