Wednesday, June 4, 2014
Mouse Event & Draw Line
namespace MouseEvents
{
public partial class Form1 : Form
{
Bitmap bmap;
Graphics g;
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
Point p = e.Location;
//display
txtXvalue.Text = x.ToString();
txtYvalue.Text = y.ToString();
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();
}
}
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;
}
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;
}
}
}
Labels:
C#
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment