Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Mouse Event

Started by Hot_Tamalle Jul 10, 2006 at 6:52 AM 4 replies 850+ views
Original Post
Hot_Tamalle
Hot_Tamalle
Hi, I've recently made a paint program. It draws a rectangle whenever a mouse button is pressed on the pictureBox. It,however, doesnot draw if you keep the mouse button pressed while dragging the mouse around. My mouse event is something like this: switch (e.Button) { case MouseButtons.Left: { //Code } } So my question here is that how can I keep drawing the rectangles while the mouse, changes it's X and Y co-ordinates.
Redburn
Redburn
You could set a boolean variable to true onMouseDown and back to false onMouseUp and do the drawing While(onMouseDownBool).

(Sorry for my bad pseudo.)
Hot_Tamalle
Hot_Tamalle
This way the program is going into a loop. It's drawing the same rectangle again and again and is not waiting for the X and Y values to change.
Redburn
Redburn
Grab the mouses X and Y values in onMouseMove then. And do the rectangle drawing in your loop within "if (onMouseDownBool)"
Hot_Tamalle
Hot_Tamalle
Which ever way i try the program goes into a loop and crashes. My original code is "something" like this:

private void pictureBox2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
{
X = e.X;
Y = e.Y;
Graphics g = pictureBox2.CreateGraphics();

g.DrawImageUnscaled(theImage, Convert.ToInt32(X),Convert.ToInt32(Y));

}

break;
}





}
Machaira
Machaira
        int _x, _y;        bool _mouseDown = false;        Graphics _gr;        private void Form1_Load(object sender, EventArgs e)        {            _gr = this.CreateGraphics();        }        private void Form1_MouseDown(object sender, MouseEventArgs e)        {            _x = e.X;            _y = e.Y;            _mouseDown = true;        }        private void Form1_MouseUp(object sender, MouseEventArgs e)        {            _mouseDown = false;        }        private void Form1_MouseMove(object sender, MouseEventArgs e)        {            if (_mouseDown)            {                _gr.Clear(this.BackColor);                _gr.DrawRectangle(Pens.Red, new Rectangle(_x,_y,e.X-_x,e.Y-_y));            }        }
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.