Rysowanie w panelu C# WinForms

Program pozwala rysować linie w kontrolce Panel (panel1). Przycisk button1 pozwala czyścić narysowane linie, a przycisk button2 pozwala zmieniać kolor linii. hScrollBar1 i label pozwala zmieniać i wyświetlać szerokość pędzla.


namespace Drawing
{
    public partial class Form1 : Form
    {
        Graphics gr;
        Pen pen;
        bool isPainting;
        Point startPos;
        Point endPos;
        ColorDialog colorDialog;
        int penDefWidth = 6;

        public Form1()
        {
            InitializeComponent();
            gr = panel1.CreateGraphics();
            pen = new Pen(Color.Black, penDefWidth);
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            colorDialog = new();
            hScrollBar1.Value = penDefWidth;
            penWidthLabel.Text = hScrollBar1.Value.ToString();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            isPainting = true;
            startPos = new Point(e.X, e.Y);

        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            isPainting = false;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isPainting)
            {
                endPos = new Point(e.X, e.Y);
                gr.DrawLine(pen, startPos, endPos);
                startPos = endPos;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Invalidate();
            //gr.Clear(Color.White);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                pen.Color = colorDialog.Color;
            }
        }

        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            pen.Width = hScrollBar1.Value;
            penWidthLabel.Text = hScrollBar1.Value.ToString();
        }
    }
}

Event Paint w WinForms wywołuje się zawsze kiedy wygląd kontrolki musi zostać narysowany ponownie (zmieni się grafika, tekst, itd)

W WinForms można rysować kształty za pomocą wbudowanych metod, np:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        Graphics gr;
        Pen pen;

        public Form1()
        {
            InitializeComponent();
            gr = panel1.CreateGraphics();
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            pen = new Pen(Color.Black, 5);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //gr.Clear(Color.White);
            panel1.Invalidate();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            gr.DrawRectangle(pen, 100, 100, 100, 100);
            gr.DrawPie(pen, 300, 100, 100, 100, -90, 120);
            gr.DrawArc(pen, 500, 100, 100, 100, 0, 90);
        }
    }
}

Rysowanie wypełnionych kształtów polygon z obrysem

private void panel1_Paint(object sender, PaintEventArgs e)
{
    DrawM();
}

void DrawM()
{
    Point[] p = new Point[]{
        new Point(0, 0),
        new Point(50, 25),
        new Point(100, 0),
        new Point(100, 50),
        new Point(0, 50),
        new Point(0, 0),
    };

    System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
    gp.AddPolygon(p);

    Region r = new Region(gp);
    Graphics gr = panel1.CreateGraphics();
    gr.FillRegion(Brushes.BlueViolet, r);
    Pen outlinePen = new Pen(Color.Red, 4);
    gr.DrawPath(outlinePen, gp);
}

Rysowanie kształtów za pomocą myszy

namespace HelloTemp1
{
    public partial class Form1 : Form
    {
        Graphics gr;
        Pen pen;
        int penDefWidth = 6;
        Point startPos;
        Point endPos;
        Rectangle rect;
        bool isDrawing;

        public Form1()
        {
            InitializeComponent();
            gr = panel1.CreateGraphics();
            pen = new Pen(Color.Black, penDefWidth);
            gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
            startPos = new Point(e.X, e.Y);
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            gr.DrawRectangle(pen, GetRect());
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if(isDrawing)
            {
                panel1.Invalidate();
                endPos = new Point(e.X, e.Y);
            }
        }
        Rectangle GetRect()
        {
            rect = new Rectangle();
            rect.X = Math.Min(startPos.X, endPos.X);
            rect.Y = Math.Min(startPos.Y, endPos.Y);
            rect.Width = Math.Abs(endPos.X - startPos.X);
            rect.Height = Math.Abs(endPos.Y - startPos.Y);
            return rect;
        }

    }
}
Scroll to Top