System.Drawing

07/04/2010 20:17 xScott#1
Not strong in this :L
Not good with Forms, Events and system.Drawing
Im making a key event draw a new Rectangle when the key is (Down) is pressed, but its not drawing a new rectangle when i press down continuously:

Code:
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Graphics GE = Graphics.FromHwnd(this.Handle);
            Pen PE = new Pen(Color.Black);
            Rectangle NE = new Rectangle(15, 15, 20, 50);

            if (e.KeyCode == Keys.Down)
            {
                NE.Y++;
                GE.DrawRectangle(PE, NE);
            }
        }
Can you please point me in the right direction, even though i've probably got this completely wrong :L
Shouldn't this be drawing a new rectangle, but displaced in Y Direction by 1 everytime the Down key is pressed?

EDIT:
Tried doing:

Code:
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Graphics GE = Graphics.FromHwnd(this.Handle);
            Pen PE = new Pen(Color.Black);
            Rectangle NE = new Rectangle(15, 15, 20, 50);
            GE.DrawRectangle(PE, NE);

            if (e.KeyCode == Keys.Down)
            {

                  NE.OffSet(17,17);
            }
        }
To move the current shape drawn, but it doesn't move at all.
07/04/2010 21:03 Ian*#2
Quote:
Originally Posted by xScott View Post
Not strong in this :L
Not good with Forms, Events and system.Drawing
Im making a key event draw a new Rectangle when the key is (Down) is pressed, but its not drawing a new rectangle when i press down continuously:

Code:
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Graphics GE = Graphics.FromHwnd(this.Handle);
            Pen PE = new Pen(Color.Black);
            Rectangle NE = new Rectangle(15, 15, 20, 50);

            if (e.KeyCode == Keys.Down)
            {
                NE.Y++;
                GE.DrawRectangle(PE, NE);
            }
        }
Can you please point me in the right direction, even though i've probably got this completely wrong :L
Shouldn't this be drawing a new rectangle, but displaced in Y Direction by 1 everytime the Down key is pressed?

EDIT:
Tried doing:

Code:
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Graphics GE = Graphics.FromHwnd(this.Handle);
            Pen PE = new Pen(Color.Black);
            Rectangle NE = new Rectangle(15, 15, 20, 50);
            GE.DrawRectangle(PE, NE);

            if (e.KeyCode == Keys.Down)
            {

                  NE.OffSet(17,17);
            }
        }
To move the current shape drawn, but it doesn't move at all.
Try doing this.

Code:
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Graphics GE = Graphics.FromHwnd(this.Handle);
            Pen PE = new Pen(Color.Black);
[B]            int X = 10;
            int Y = 10;[/B]
            Rectangle NE = new Rectangle(15, 15, [B]X, Y[/B]);

            if (e.KeyCode == Keys.Down)
            {
                Y++;
                GE.DrawRectangle(PE, NE);
            }
        }
07/05/2010 01:26 xScott#3
Quote:
Originally Posted by Ian* View Post
Try doing this.

Code:
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Graphics GE = Graphics.FromHwnd(this.Handle);
            Pen PE = new Pen(Color.Black);
[B]            int X = 10;
            int Y = 10;[/B]
            Rectangle NE = new Rectangle(15, 15, [B]X, Y[/B]);

            if (e.KeyCode == Keys.Down)
            {
                Y++;
                GE.DrawRectangle(PE, NE);
            }
        }
didn't work ><
07/05/2010 02:42 Ian*#4
Do you have a Form1_Paint event?

Try putting that code in there and use key presses to draw.

haven't used System.Drawing for anything besides dmaps :P
07/06/2010 04:06 I.P#5
Graphics are cleared each frame, so if you want a new one + the older ones, you'll have to store them in an array and redraw them all each frame.

edit: Surprised Ian did not know this :P
07/06/2010 11:03 Ian*#6
Quote:
Originally Posted by I.P View Post
Graphics are cleared each frame, so if you want a new one + the older ones, you'll have to store them in an array and redraw them all each frame.

edit: Surprised Ian did not know this :P
His problem is making it draw while he hold the key down :P

Don't think you have to clear anything, he'd just be drawing a rectangle that eventually becomes solid since it fills up so fast :p
07/06/2010 11:53 I.P#7
No but he will have to store his variables in an array and call them every time he needs them if you get what I mean
07/06/2010 12:12 Ian*#8
Ah I see, I've only used Drawing for loading dmaps :p

So never had to do it that way lol.

Speaking of which i need to add those to my new bot >.>
07/06/2010 12:14 I.P#9
Fun dun ^.^

better than watching video tutorials trust me ;D
07/08/2010 02:19 s.bat#10
What does this have to do with CO2?

The problem with your code and Ian*'s is that you're both redeclaring and reinstantiating the variables that depend on the Rectangle's location. You may alter those values at the bottom of that method, but you're losing those changes because either your Rectangle variable or your X and Y variables need to be declared at the class level.

It may be more preferable to use the [Only registered and activated users can see links. Click Here To Register...] and [Only registered and activated users can see links. Click Here To Register...] instead of using the UI's event dispatch thread. [Only registered and activated users can see links. Click Here To Register...] may provide you with some useful examples.