Thursday, June 5, 2014

Using Color Dialog


namespace MouseEvents
{
    public partial class Form1 : Form
    {
        Bitmap bmap;
        Graphics g;
        Point pOne;



        public Form1()
        {
            InitializeComponent();
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bmap);
        }

        // bool draw;
        int x;
        int y;
        Pen p;
        Color penColor = Color.White;

        private void btnMouseLeaveNEnter_MouseEnter(object sender, EventArgs e)
        {
            //change the button color and backcolor
            btnMouseLeaveNEnter.BackColor = Color.Yellow;
            btnMouseLeaveNEnter.ForeColor = Color.Green;

            btnMouseLeaveNEnter.Font = new Font("Arial", 15, FontStyle.Bold);
        }

        private void btnMouseLeaveNEnter_MouseLeave(object sender, EventArgs e)
        {
            btnMouseLeaveNEnter.BackColor = Color.Orange;
            btnMouseLeaveNEnter.ForeColor = Color.White;

            btnMouseLeaveNEnter.Font = new Font("Arial", 8, FontStyle.Regular);
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            //get the x, y coordinates where the mouse was pressed down
            //within the picturebox1

            x = e.X;
            y = e.Y;
            //or you could use the location property which return a Point
           

            //display
            txtXvalue.Text = x.ToString();
            txtYvalue.Text = y.ToString();

            pOne = e.Location;

           //draw = true;
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
           //draw = false;

            if (e.Button == MouseButtons.Left)
            {
                int x = e.X;
                int y = e.Y;

                txtXvalue.Text = x.ToString();
                txtYvalue.Text = y.ToString();

                Point pTwo = e.Location;
                //Pen pen = new Pen(Color.Red);
                System.Drawing.Drawing2D.LinearGradientBrush brush =
                    new System.Drawing.Drawing2D.LinearGradientBrush(pOne, pTwo, btnColor1.BackColor, btnColor2.BackColor);
                Pen pen = new Pen(brush);
                g.DrawLine(pen, pOne, pTwo);
                pictureBox1.Image = bmap;
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            //if (draw)
            //{
            //    p = new Pen((penColor), 2);
            //    g.DrawLine(p, x, y, e.X, e.Y);
            //    x = e.X;
            //    y = e.Y;
            //}
            //pictureBox1.Image = bmap;
            if (e.Button == MouseButtons.Left)
            {
                Point pTwo = e.Location;
                //Pen pen = new Pen(Color.Red);
               //g.DrawLine(pen, pOne, pTwo);
                System.Drawing.Drawing2D.LinearGradientBrush brush =
                    new System.Drawing.Drawing2D.LinearGradientBrush(pOne, pTwo, btnColor1.BackColor, btnColor2.BackColor);
                Pen pen = new Pen(brush);
                g.DrawLine(pen, pOne, pTwo);
                pictureBox1.Image = bmap;
            }
        }

        private void btnClear_Click_1(object sender, EventArgs e)
        {
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bmap);
            pictureBox1.Image = bmap;
        }

        private void colorBox_MouseClick(object sender, MouseEventArgs e)
        {
            PictureBox s = sender as PictureBox;
            penColor = s.BackColor;
        }

        private void btnColor1_Click(object sender, EventArgs e)
        {
            DialogResult dr = colorDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                btnColor1.BackColor = colorDialog1.Color;
            }
        }

        private void btnColor2_Click(object sender, EventArgs e)
        {
            DialogResult dr = colorDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                btnColor2.BackColor = colorDialog1.Color;
            }
        }
    }
}

No comments:

Post a Comment