绘制一个矩形但笔的颜色不起作用

时间:2015-03-05 01:47:50

标签: c# system.windows.media

错误发生在//Create pen之后的行上。它声明System.Windows.Media.Color不包含'Black'的定义。我该如何解决?

   public void DrawRectangleRectangle(PaintEventArgs e)
    {

        // Create pen.
        System.Windows.Media.Pen blackPen = new System.Windows.Media.Pen(Color.Black, 3);

        // Create rectangle.
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, 200, 200);

        // Draw rectangle to screen.
        e.Graphics.DrawRectangle(blackPen, rect);
    }

这个怎么样,又回到​​了划痕:

    public void DrawRectangleRectangle(PaintEventArgs e)
    {

        // Create pen.
        Pen blackPen = new Pen(Color.Black, 3);

        // Create rectangle.
        Rectangle rect = new Rectangle(0, 0, 200, 200);

        // Draw rectangle to screen.
        e.Graphics.DrawRectangle(blackPen, rect);
    }

Black上有一个错误,说System.Windows.Media没有'Black'的定义。我从Graphics.DrawRectangle

得到了这个例子

如何使其适应我的代码?

2 个答案:

答案 0 :(得分:2)

对于winforms应用程序,您应该使用System.Drawing命名空间中的类;例如System.Drawing.Pen

System.Windows.Media命名空间包含WPF应用程序的类。

我建议您将using System.Drawing放在文件的顶部(并移除using System.Windows.Media),然后在代码中使用PenRectangle

答案 1 :(得分:1)

如果您想使用OnRender(DrawingContext drawingContext),则必须在Transparent对象中将背景设置为Window并覆盖OnRender方法。

public MainWindow()
{
    InitializeComponent();

    //--workaround: set the background as transparent.
    Background = Brushes.Transparent;
}

然后覆盖OnRender方法。代码假设定义为: _rectBrush,_rectPen,_rect

protected override void OnRender(DrawingContext drawingContext)
{       
    //--set background in white
    Rect bgRect = new Rect(0, 0, ActualWidth, ActualHeight);
    drawingContext.DrawRectangle(Brushes.White, null, bgRect);

    //--draw the rectangle
    drawingContext.DrawRectangle(_rectBrush, _rectPen, _rect);
}

我希望它有所帮助。

编辑:包含一个示例:

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="Rectangle Painting" Height="350" Width="525"
        MouseLeftButtonDown="MainWindow_OnMouseLeftButtonDown"
        MouseLeftButtonUp="MainWindow_OnMouseLeftButtonUp"
        MouseMove="MainWindow_OnMouseMove"
        Background="Transparent">
</Window>

背后的代码:

using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace WpfDrawing
{
    public partial class MainWindow : Window
    {
        private Point _p1;
        private Point _p2;
        private bool _painting;
        private readonly Pen _rectPen = new Pen(Brushes.Blue, 1);
        private readonly SolidColorBrush _rectBrush = new SolidColorBrush
        {
            Color = Colors.SkyBlue
        };

        public MainWindow()
        {
            InitializeComponent();

            //--workaround: set the background as transparent.
            Background = Brushes.Transparent;

            //--Freeze the painting objects to increase performance.
            _rectPen.Freeze();
            _rectBrush.Freeze();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            var rect = new Rect(_p1, _p2);
            Debug.WriteLine("OnRender -> " + rect);

            //--set background in white
            Rect backRect = new Rect(0, 0, ActualWidth, ActualHeight);
            drawingContext.DrawRectangle(Brushes.White, null, backRect);

            //--draw the rectangle
            drawingContext.DrawRectangle(_rectBrush, _rectPen, rect);
        }

        private void MainWindow_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var p = e.GetPosition(this);

            Debug.WriteLine("MainWindow_OnMouseLeftButtonDown -> " + p);

            _p1 = _p2 = p;

            _painting = true;

            InvalidateVisual();
        }

        private void MainWindow_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _painting = false;

            Debug.WriteLine("MainWindow_OnMouseLeftButtonUp");
        }

        private void MainWindow_OnMouseMove(object sender, MouseEventArgs e)
        {
            if (!_painting)
                return;

            var p = e.GetPosition(this);
            Debug.WriteLine("MainWindow_OnMouseMove -> " + p);

            _p2 = p;

            InvalidateVisual();
        }
    }
}