Question_Guy
Question_Guy

Reputation: 333

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

The error is on the line after //Create pen. It states that System.Windows.Media.Color does not contain a definition for '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'. I got this example from Graphics.DrawRectangle

How do I adapt it to my code?

Upvotes: 1

Views: 1867

Answers (2)

denys-vega
denys-vega

Reputation: 3697

If you want to use OnRender(DrawingContext drawingContext) you must set the background to Transparent in your Window object and override the OnRender method.

public MainWindow()
{
    InitializeComponent();

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

Then override OnRender method. A code assume the definition of: _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:

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

Upvotes: 1

Blorgbeard
Blorgbeard

Reputation: 103467

For a winforms application, you should be using classes from the System.Drawing namespace; e.g. System.Drawing.Pen.

The System.Windows.Media namespace contains classes for WPF applications.

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.

Upvotes: 2

Related Questions