使用拖动鼠标绘制矩形c#

时间:2015-03-08 23:39:46

标签: c# graphics

我想使用鼠标(未填充)绘制和拖动来绘制矩形,三角形,圆形和饼形图形。

using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Runtime.InteropServices;
        using System.Drawing.Imaging;
        using System.IO;

        namespace mainpaint
        {
            public partial class Form1 : Form
            {
                Color paintcolor;
                bool choose = false;

                bool draw = false;
                int x, y, lx, ly = 0;
                Item currItem;
                public Form1()
                {
                    InitializeComponent();
                    this.DoubleBuffered = true;
                    this.Cursor = System.Windows.Forms.Cursors.Cross;

                }

                public enum Item
                {
                    Rectangle, Ellipse, Line,Pencil,eraser, Triangle
                }

                private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
                {
                    choose = true;
                }

                private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
                {
                    choose = false;
                }

                private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
                {

                    if (choose)
                    {

                        Bitmap bmp = (Bitmap)pictureBox2.Image.Clone();
                        paintcolor = bmp.GetPixel(e.X, e.Y);
                        red.Value = paintcolor.R;
                        green.Value = paintcolor.G;
                        blue.Value = paintcolor.B;
                        alpha.Value = paintcolor.A;
                        redval.Text = paintcolor.R.ToString();
                        greenval.Text = paintcolor.G.ToString();
                        blueval.Text = paintcolor.B.ToString();
                        alphaval.Text = paintcolor.A.ToString();
                        pictureBox3.BackColor = paintcolor;

                    }
                }

                private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
                {
                    draw = true;
                    x = e.X;
                    y = e.Y;
                }

                private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
                {
                    draw = false;
                    lx = e.X;
                    ly = e.Y;

                    if (currItem == Item.Line)
                    {
                        Graphics g = pictureBox1.CreateGraphics();
                        g.DrawLine(new Pen(new SolidBrush(paintcolor),3), new Point(x, y), new Point(lx, ly));
                        g.Dispose();

                    }
                }

                private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
                {
                    if (draw)
                    {
                        Graphics g = pictureBox1.CreateGraphics();



                        switch (currItem)
                        {
                            case Item.Rectangle:


                                g.DrawRectangle(new Pen(new SolidBrush(paintcolor),3), x, y, e.X - x, e.Y - y); 

                               // g.FillRectangle(new SolidBrush(paintcolor), x, y, e.X - x, e.Y - y); these is to draw a filled rectangle not a problem
                                break;
                            case Item.Ellipse:
                                g.DrawEllipse(new Pen(new SolidBrush(paintcolor), 3), x, y, e.X - x, e.Y - y);
                                //g.FillEllipse(new SolidBrush(paintcolor), x, y, e.X - x, e.Y - y);
                                break;
                            case Item.Pencil:
                                g.FillEllipse(new SolidBrush(paintcolor), e.X - x + x, e.Y - y + y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
                                break;
                            case Item.eraser:
                                g.FillEllipse(new SolidBrush(pictureBox1.BackColor), e.X - x + x, e.Y - y + y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
                                break;
                            case Item.Triangle:
                                 g.FillPolygon(new SolidBrush(paintcolor), new Point[3] { new Point(x-e.X,y+e.Y), new Point(x,y), new
                                   Point(x+e.X,y+e.Y) });

                                break;


                        }
                        g.Dispose();
                    }
                }


                private void toolStripButton9_Click(object sender, EventArgs e)
                {
                    currItem = Item.Rectangle;
                }

                private void toolStripButton10_Click(object sender, EventArgs e)
                {
                    currItem = Item.Ellipse;
                }

                private void toolStripButton13_Click(object sender, EventArgs e)
                {
                    currItem = Item.Pencil;
                }

                private void toolStripButton15_Click(object sender, EventArgs e)
                {
                    currItem = Item.eraser;
                }

                private void toolStripButton11_Click(object sender, EventArgs e)
                {
                    currItem = Item.Line;
                }

           private void toolStripButton6_Click(object sender, EventArgs e)
                {
                    currItem = Item.Triangle;
                }

                private void Form1_Load(object sender, EventArgs e)
                {
                        FontFamily[] family = FontFamily.Families;

                }

                private void red_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    redval.Text = "R: " + paintcolor.R.ToString();
                }

                private void green_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    greenval.Text = "G: " + paintcolor.G.ToString();
                }

                private void blue_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    blueval.Text = "B: " + paintcolor.B.ToString();
                }

                private void alpha_Scroll(object sender, EventArgs e)
                {
                    paintcolor = Color.FromArgb(alpha.Value, red.Value, green.Value, blue.Value);
                    pictureBox3.BackColor = paintcolor;
                    alphaval.Text = "A: " + paintcolor.A.ToString();
                }



            }
        }

我有这些结果:

矩形和圆圈:

enter image description here

我的三角形甚至没有形式(我在这里使用填充方法多边形,绘制方法应该相同?)

enter image description here

我想要这样的事情:

enter image description here

最后,我怎么画一个饼形图?

谢谢!

0 个答案:

没有答案