2014-02-10 139 views
0

我正是这样oneWPF自定义窗口调整大小问题

一切同样的问题是好的,但是当我从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

如果您想获得答案,请[显示您的相关代码](http://stackoverflow.com/help/mcve)。 – Sheridan

+0

有你去先生,我希望你能帮助我! – jovanMeshkov

+0

我给你的xaml的ControlTemplate,它不能被减少更多... 关于c#代码,只有8个函数,它们从ControlTemplate向边界添加侦听器,它足以读取第一个函数,并且您将知道我在做什么......关于其他网站上的代码,我认为这对于这篇文章来说太大了,我可以在这里重新放置它,它不是一个问题... – jovanMeshkov

回答

-1

如果你只是希望添加调整手柄像这样一个自定义WPF窗口,

Custom WPF Window

然后看看http://alski.net/post/2013/09/14/WPF-Re-creating-VS2012Office-2013-window-glow.aspx

您可以直接通过https://www.nuget.org/packages/WinChrome/将其拉入,或者从http://winchrome.codeplex.com/SourceControl/latest盗取代码。

+0

我看过这个,但是在哪个命名空间和类中有这个窗口? – jovanMeshkov

+0

那么如果你正在寻找我处理调整大小的地方,我不会。我仍然让窗户做,它快得多,因此避免了跳跃。首先阅读文章,因为它会引导您完成整个过程。 – AlSki

+0

这里的窗口是一个'SearchableNavigationWindow',但是工作是在窗口的样式中完成的。您可能想要查看'WinChrome.Modern \ MainWindow.xaml',它的样式在'WinChrome \ UI \ VS2012window.xaml'中,以及'Winchrome \ VS2012.cs中的附加属性' – AlSki