画了一个矩形但是笔的颜色不起作用

Drawing a rectangle but the color of the pen will not work

错误在//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

得到这个例子

如何使其适应我的代码?

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

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

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

如果您想使用 OnRender(DrawingContext drawingContext),您必须在 Window 对象中将背景设置为 Transparent 并覆盖 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();
        }
    }
}