1

错误就在后面的线上//Create pen。它指出 System.Windows.Media.Color 不包含“黑色”的定义。我该如何解决?

   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 没有“黑色”的定义时出错。我从Graphics.DrawRectangle得到了这个例子

如何使其适应我的代码?

4

2 回答 2

2

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

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

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

于 2015-03-05T01:49:38.727 回答
1

如果要使用OnRender(DrawingContext drawingContext),必须TransparentWindow对象中设置背景并覆盖该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();
        }
    }
}
于 2015-03-05T02:49:34.353 回答