简体   繁体   English

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

[英]Drawing a rectangle but the color of the pen will not work

The error is on the line after //Create pen . 错误在//Create pen之后的行上。 It states that System.Windows.Media.Color does not contain a definition for 'Black'. 它声明System.Windows.Media.Color不包含'Black'的定义。 How do I fix this? 我该如何解决?

   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);
    }

How about this, and going back to scratch: 怎么样,又回到​​了划痕:

    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);
    }

There is an error on Black saying System.Windows.Media has no definition for 'Black'. Black上有一个错误说System.Windows.Media没有'Black'的定义。 I got this example from Graphics.DrawRectangle 我从Graphics.DrawRectangle得到了这个例子

How do I adapt it to my code? 如何使其适应我的代码?

For a winforms application, you should be using classes from the System.Drawing namespace; 对于winforms应用程序,您应该使用System.Drawing命名空间中的类; eg System.Drawing.Pen . 例如System.Drawing.Pen

The System.Windows.Media namespace contains classes for WPF applications. System.Windows.Media命名空间包含WPF应用程序的类。

I suggest you put using System.Drawing at the top of your file (and remove using System.Windows.Media ), and then simply use Pen and Rectangle in your code. 我建议你using System.Drawing在文件的顶部(并using System.Windows.Media删除),然后在代码中使用PenRectangle

If you want to use OnRender(DrawingContext drawingContext) you must set the background to Transparent in your Window object and override the OnRender method. 如果要使用OnRender(DrawingContext drawingContext) ,则必须在Window对象中将背景设置为Transparent ,并覆盖OnRender方法。

public MainWindow()
{
    InitializeComponent();

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

Then override OnRender method. 然后重写OnRender方法。 A code assume the definition of: _rectBrush, _rectPen, _rect 代码假定: _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);
}

I hope it helps. 我希望它有所帮助。

EDIT: Including an example: 编辑:包括一个例子:

The XAML part: 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>

And the code behind: 而背后的代码:

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM