WPF自定义窗口调整大小问题

时间:2014-02-10 14:54:30

标签: c# wpf resize

我有完全相同的问题,例如one

一切都很好,但是当我从West to EastNorth to South调整大小时,窗口的East/South侧有点不稳定,看起来不太好。

正如我可以看到这篇文章很久很久以前,但仍然没有有效答案,我环顾四周,但找不到任何东西,任何人都可以告诉我这个问题是否已修复我能找到解决方案吗?

[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);
        }

0 个答案:

没有答案