2015-10-03 34 views
0

我想使用畫布在WPF中創建一個Paint應用程序。我想在繪圖時增加我的形狀的厚度,所以我嘗試增加StrokeThickness。如何增加WPF中的形狀的厚度

這是我希望它是:

enter image description here

而這就是我得到:

enter image description here

正如你所看到的輪廓僅邊界內延伸。我怎樣才能讓它在兩側延伸?

這裏是我的代碼:

在MouseDown事件:

Rectangle rect = new Rectangle(); 
rect.Stroke = _color; 
rect.StrokeThickness = _size; 
Canvas.SetLeft(rect, _startPoint.X); 
Canvas.SetTop(rect, _startPoint.Y); 
cv_PaintBoard.Children.Add(rect); 
isDrawing = true; 

在MouseMove事件:

if (isDrawing == true && e.LeftButton == MouseButtonState.Pressed) 
{ 
    Canvas canvas = (Canvas)sender; 

    Rectangle rect = canvas.Children.OfType<Rectangle>().LastOrDefault(); 

    if (rect != null) 
    { 
     Point endPoint = e.GetPosition((IInputElement)sender); 
     Point startPoint = new Point(
      Math.Min(endPoint.X, _startPoint.X), 
      Math.Min(endPoint.Y, _startPoint.Y) 
      ); 

     rect.Width = Math.Max(endPoint.X, _startPoint.X) - startPoint.X; 
     rect.Height = Math.Max(endPoint.Y, _startPoint.Y) - startPoint.Y; 

     Canvas.SetLeft(rect, startPoint.X); 
     Canvas.SetTop(rect, startPoint.Y); 
    } 
} 
+0

怎麼樣顯示相關代碼的人,而不是讓他們猜 –

+0

看起來像你可以嘗試創建一些輔助的方法來更新'StrokeThickness'在不僅「StrokeThickness」被更新,而且「寬度」和「高度」也被更新。 – Hopeless

回答

1

您需要更新寬度/高度和左/頂過您的具體需要。

以下示例演示了您的需求。如果你可以,請告訴我。

MainWindow.xaml

<Window x:Class="WpfDrawing.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="447.368" Width="606.579"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="13*"/> 
     <RowDefinition Height="7*"/> 
    </Grid.RowDefinitions> 
    <Canvas Grid.RowSpan="1"> 
     <Rectangle x:Name="Rect1" Fill="Transparent" HorizontalAlignment="Left" Height="70" Canvas.Left="248" Canvas.Top="104" Stroke="Black" Width="94" Opacity="0.5" /> 
     <Rectangle x:Name="Rect2" Fill="#FF52E03C" HorizontalAlignment="Left" Height="70" Canvas.Left="248" Canvas.Top="104" Stroke="Black" Width="94" Opacity="0.5" /> 

    </Canvas> 
    <TextBox x:Name="tbThickness" HorizontalAlignment="Left" Height="23" Margin="84,27,0,0" Grid.Row="1" TextWrapping="Wrap" Text="5" VerticalAlignment="Top" Width="120"/> 
    <Button Content="Button" HorizontalAlignment="Left" Margin="237,28,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 
</Grid> 
</Window> 

MainWindow.xaml.cs

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 WpfDrawing 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     double designWidth; 
     double designHeight; 

     double designLeft, designTop; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      designWidth = Rect2.Width; 
      designHeight = Rect2.Height; 

      designLeft = Canvas.GetLeft(Rect2); 
      designTop = Canvas.GetTop(Rect2); 
     }  

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      Rect2.StrokeThickness = double.Parse(tbThickness.Text); 

      Canvas.SetLeft(Rect2,designLeft - (Rect2.StrokeThickness/2)); 
      Canvas.SetTop(Rect2, designTop - (Rect2.StrokeThickness/2));       

      Rect2.Width = designWidth + Rect2.StrokeThickness; 
      Rect2.Height = designHeight + Rect2.StrokeThickness; 

     } 
    } 
} 
+0

哦,這就是我想要的,非常感謝 –

+0

最受歡迎。你有沒有將畫布設計成可調整大小的油漆。 – AnjumSKhan

1

有沒有必要做任何計算它的筆畫粗細的一半糾正矩形的大小。

只要使用路徑與RectangleGeometries而不是矩形控制控制:

private Brush stroke = Brushes.Red; 
private double strokeThickness = 10; 
private Path currentPath; 
private Point startPoint; 

private void Canvas_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    var canvas = (Canvas)sender; 
    if (canvas.CaptureMouse()) 
    { 
     startPoint = e.GetPosition(canvas); 
     currentPath = new Path 
     { 
      Data = new RectangleGeometry(new Rect(startPoint, startPoint)), 
      Stroke = stroke, 
      StrokeThickness = strokeThickness 
     }; 
     canvas.Children.Add(currentPath); 
    } 
} 

private void Canvas_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (currentPath != null) 
    { 
     ((RectangleGeometry)currentPath.Data).Rect 
       = new Rect(startPoint, e.GetPosition((UIElement)sender)); 
    } 
} 

private void Canvas_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    ((UIElement)sender).ReleaseMouseCapture(); 
    currentPath = null; 
} 
+0

謝謝,我也會試試 –