SHARE
TWEET

Untitled

okanfeim May 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace MiniPaint
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.             g = pnl_Draw.CreateGraphics();
  13.         }
  14.         bool startPaint = false;
  15.         Graphics g;
  16.         //nullable int for storing Null value
  17.         int? initX = null;
  18.         int? initY = null;
  19.         bool drawSquare = false;
  20.         bool drawRectangle = false;
  21.         bool drawCircle = false;
  22.         //Event fired when the mouse pointer is moved over the Panel(pnl_Draw).
  23.         private void pnl_Draw_MouseMove(object sender, MouseEventArgs e)
  24.         {
  25.             if(startPaint)
  26.             {
  27.                 //Setting the Pen BackColor and line Width
  28.                 Pen p = new Pen(btn_PenColor.BackColor,float.Parse(cmb_PenSize.Text));
  29.                 //Drawing the line.
  30.                 g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
  31.                 initX = e.X;
  32.                 initY = e.Y;
  33.             }
  34.         }
  35.         //Event Fired when the mouse pointer is over Panel and a mouse button is pressed
  36.         private void pnl_Draw_MouseDown(object sender, MouseEventArgs e)
  37.         {
  38.             startPaint = true;
  39.             if (drawSquare)
  40.             {
  41.                 //Use Solid Brush for filling the graphic shapes
  42.                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
  43.                 //setting the width and height same for creating square.
  44.                 //Getting the width and Heigt value from Textbox(txt_ShapeSize)
  45.                 g.FillRectangle(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
  46.                 //setting startPaint and drawSquare value to false for creating one graphic on one click.
  47.                 startPaint = false;
  48.                 drawSquare = false;
  49.             }
  50.             if(drawRectangle)
  51.             {
  52.                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
  53.                 //setting the width twice of the height
  54.                 g.FillRectangle(sb, e.X, e.Y, 2*int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
  55.                 startPaint = false;
  56.                 drawRectangle = false;
  57.             }
  58.             if(drawCircle)
  59.             {
  60.                 SolidBrush sb = new SolidBrush(btn_PenColor.BackColor);
  61.                 g.FillEllipse(sb, e.X, e.Y, int.Parse(txt_ShapeSize.Text), int.Parse(txt_ShapeSize.Text));
  62.                 startPaint = false;
  63.                 drawCircle = false;
  64.             }
  65.         }
  66.         //Fired when the mouse pointer is over the pnl_Draw and a mouse button is released.
  67.         private void pnl_Draw_MouseUp(object sender, MouseEventArgs e)
  68.         {
  69.             startPaint = false;
  70.             initX = null;
  71.             initY = null;
  72.         }
  73.         //Button for Setting pen Color
  74.         private void button1_Click(object sender, EventArgs e)
  75.         {
  76.             //Open Color Dialog and Set BackColor of btn_PenColor if user click on OK
  77.             ColorDialog c = new ColorDialog();
  78.             if(c.ShowDialog()==DialogResult.OK)
  79.             {
  80.                 btn_PenColor.BackColor = c.Color;
  81.             }
  82.         }
  83.         //New
  84.         private void newToolStripMenuItem_Click(object sender, EventArgs e)
  85.         {
  86.             //Clearing the graphics from the Panel(pnl_Draw)
  87.             g.Clear(pnl_Draw.BackColor);
  88.             //Setting the BackColor of pnl_draw and btn_CanvasColor to White on Clicking New under File Menu
  89.             pnl_Draw.BackColor = Color.White;
  90.             btn_CanvasColor.BackColor = Color.White;
  91.         }
  92.        //Setting the Canvas Color
  93.         private void btn_CanvasColor_Click_1(object sender, EventArgs e)
  94.         {
  95.             ColorDialog c = new ColorDialog();
  96.             if(c.ShowDialog()==DialogResult.OK)
  97.             {
  98.                 pnl_Draw.BackColor = c.Color;
  99.                 btn_CanvasColor.BackColor = c.Color;
  100.             }
  101.         }
  102.         private void btn_Square_Click(object sender, EventArgs e)
  103.         {
  104.             drawSquare = true;
  105.         }
  106.         private void btn_Rectangle_Click(object sender, EventArgs e)
  107.         {
  108.             drawRectangle = true;
  109.         }
  110.         private void btn_Circle_Click(object sender, EventArgs e)
  111.         {
  112.             drawCircle = true;
  113.         }
  114.         //Exit under File Menu
  115.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  116.         {
  117.             if(MessageBox.Show("Do you want to Exit?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
  118.             {
  119.                 Application.Exit();
  120.             }
  121.         }
  122.         //About under Help Menu
  123.         private void aboutMiniPaintToolStripMenuItem_Click(object sender, EventArgs e)
  124.         {
  125.             About a = new About();
  126.             a.ShowDialog();
  127.         }
  128.     }
  129. }
RAW Paste Data
Want to get better at C#?
Learn to code C# in 2017
Top