Help with drawing
-
Hi, I'm pretty new to programing and drawing is something I've only touched on I need to get the program to draw a shape (done that) that's as large as the form is (minus some padding) and re sizes as the form size is changed. I know about using *Whatever* = ((width > height) ? height : Width); but I don't know where to put it and I know about this.height and this.width but don't know where to put that. So in a nutshell I need help with getting my shape to fill up the maximum possible room in the form and resizing as the form size is changed. Thanks in advance for any help P.S. I know this is a very rudimentary question for many (if not all) of you out there so please don't scoff at me
-
Hi, I'm pretty new to programing and drawing is something I've only touched on I need to get the program to draw a shape (done that) that's as large as the form is (minus some padding) and re sizes as the form size is changed. I know about using *Whatever* = ((width > height) ? height : Width); but I don't know where to put it and I know about this.height and this.width but don't know where to put that. So in a nutshell I need help with getting my shape to fill up the maximum possible room in the form and resizing as the form size is changed. Thanks in advance for any help P.S. I know this is a very rudimentary question for many (if not all) of you out there so please don't scoff at me
If you are drawing directly onto the surface of the
Form
you need to put your code in thePaint event handler
of theForm
. Just in case you don't know how to do that. Make sure yourForm
is selected in the designer, then in the properties window, click the events button (at the top, lightening bolt), then double-click on thePaint
item. A handler stub is created in the editor, put your code there.private void MyForm\_Paint(object sender, PaintEventArgs e) { // Put your code here }
By putting the code there, it guarantees that your code gets executed every time the Form is drawn, such as when it is resized.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hi, I'm pretty new to programing and drawing is something I've only touched on I need to get the program to draw a shape (done that) that's as large as the form is (minus some padding) and re sizes as the form size is changed. I know about using *Whatever* = ((width > height) ? height : Width); but I don't know where to put it and I know about this.height and this.width but don't know where to put that. So in a nutshell I need help with getting my shape to fill up the maximum possible room in the form and resizing as the form size is changed. Thanks in advance for any help P.S. I know this is a very rudimentary question for many (if not all) of you out there so please don't scoff at me
Hi Johnny, . You can to use the resize event of the form , on resize event resize shape , hope you would be able to find event , events can be found using property window , there is small yellow colored , lighting shape button , it will show all event related to the selected object (in our case form , as we have kept it selected) , there You need to find resize and double click on to white space (cell) next to resize , designer will take you to event handler , where you should place your code . Hope i have not made it more confusing , let me know if you need more information
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
-
Hi, I'm pretty new to programing and drawing is something I've only touched on I need to get the program to draw a shape (done that) that's as large as the form is (minus some padding) and re sizes as the form size is changed. I know about using *Whatever* = ((width > height) ? height : Width); but I don't know where to put it and I know about this.height and this.width but don't know where to put that. So in a nutshell I need help with getting my shape to fill up the maximum possible room in the form and resizing as the form size is changed. Thanks in advance for any help P.S. I know this is a very rudimentary question for many (if not all) of you out there so please don't scoff at me
I'd recommend not doing this directly on the form, but deriving a new control from Panel. This control you can place on your form with docking set to fill and it will resize. This way, you gain some portability in your control (for use on other forms). You can even add properties to help define the shape etc. As far as the drawing goes, there are two handlers you're interested in OnPaint and OnResize. You can hook events for these, or if you do go down the derived control route just override them as protected overrides. The Resize will just let you know when the control changes size - don't attempt to draw in this. I doubt you even need this really, as you've always got access to the Width and Height in the Paint method. (Actually, you may want to Invalidate() the whole control as the OnPaint handler will clip to just new areas that weren't there prior to the resize - if you need the whole shape to resize you will need to do this). All drawing should always be done in the OnPaint method. The Width and Height tell you the dimensions. If ever you want to redraw something in Windows, Invalidate() the area and get the OnPaint method to do the drawing.
Regards, Rob Philpott.
-
Hi, I'm pretty new to programing and drawing is something I've only touched on I need to get the program to draw a shape (done that) that's as large as the form is (minus some padding) and re sizes as the form size is changed. I know about using *Whatever* = ((width > height) ? height : Width); but I don't know where to put it and I know about this.height and this.width but don't know where to put that. So in a nutshell I need help with getting my shape to fill up the maximum possible room in the form and resizing as the form size is changed. Thanks in advance for any help P.S. I know this is a very rudimentary question for many (if not all) of you out there so please don't scoff at me
Thanks for the help guys (and 4 not flaming :D) I already knew about the resize and paint events and have handled them correctly (or at least i think i have) what i specifically need help with is getting the shapes to appear with a button click (multiple shapes each on a separate button) and the resizing of the shape as the form is resized just a note in the resize event i have the code
this.refresh();
is that all that goes in that section or is that where i put my code to resize the shape as well? thanks again
-
Thanks for the help guys (and 4 not flaming :D) I already knew about the resize and paint events and have handled them correctly (or at least i think i have) what i specifically need help with is getting the shapes to appear with a button click (multiple shapes each on a separate button) and the resizing of the shape as the form is resized just a note in the resize event i have the code
this.refresh();
is that all that goes in that section or is that where i put my code to resize the shape as well? thanks again
you should not need to specifically handle resizing the shape if every time it is drawn it uses the width and height of the form. In paint event...
e.Graphics.DrawElipse(Pens.Red, 0, 0, this.Width, this.Height);
In resize event...
this.Invalidate();
Can you please explain in more details what your buttons are required to do?
Life goes very fast. Tomorrow, today is already yesterday.
-
you should not need to specifically handle resizing the shape if every time it is drawn it uses the width and height of the form. In paint event...
e.Graphics.DrawElipse(Pens.Red, 0, 0, this.Width, this.Height);
In resize event...
this.Invalidate();
Can you please explain in more details what your buttons are required to do?
Life goes very fast. Tomorrow, today is already yesterday.
ok, the buttons are meant to trigger the drawing of the object (shape) I am using a static class to hold the code to draw the shape and then calling it to the form in the paint event to draw them (I think that's how to explain it) so in the class i have
public static void Square(Graphics g)
{
g.DrawRectangle(Pens.Blue, 10, 10, this.width, this.height);
}and in the form itself i have
Draw.Triangle(e.Graphics);
so when the button called circle is pressed it triggers the command that calls upon the class to draw a circle with the this.height and this.width how to I tell the class the height and width of the form I hope this clears everything up, if not let me know
-
ok, the buttons are meant to trigger the drawing of the object (shape) I am using a static class to hold the code to draw the shape and then calling it to the form in the paint event to draw them (I think that's how to explain it) so in the class i have
public static void Square(Graphics g)
{
g.DrawRectangle(Pens.Blue, 10, 10, this.width, this.height);
}and in the form itself i have
Draw.Triangle(e.Graphics);
so when the button called circle is pressed it triggers the command that calls upon the class to draw a circle with the this.height and this.width how to I tell the class the height and width of the form I hope this clears everything up, if not let me know
Ok, i understand a little better now. Thou im not sure of your question. But i will try to explain how you should handle what you are trying to do... You should use a class for each of your shapes (triangle, square, etc.) each of these classes should contain the properties for that shape such as location, size, colour etc. (perhaps you could use a 'shape' base class and derive your shapes from that). Each of these classes should contain a method for drawing that takes a Graphics parameter. Then in your main class you have a collection of shapes, then in your paint event for you panel/form you simple loop the collection and call the draw method on each shape by passing the graphics instance from the paintEventArgs. So Then all you need to do on button clicks is to create a shape, set the properties, add it to the collection, then call invalidate() for the panel/form. Next, to accomplish the task of resizing the shapes. I assume it would be logical to resize all the shapes on the screen. So create a handler for the Resize event of the form. Then in here you can go through your collection of shapes and change the location/size as needed. You could also modify the shapes Draw method to take parameters for the width/height of the form and draw each time based on that. Then you just need to call Invalidate() in the resize event. Hope this helps
Life goes very fast. Tomorrow, today is already yesterday.
-
Ok, i understand a little better now. Thou im not sure of your question. But i will try to explain how you should handle what you are trying to do... You should use a class for each of your shapes (triangle, square, etc.) each of these classes should contain the properties for that shape such as location, size, colour etc. (perhaps you could use a 'shape' base class and derive your shapes from that). Each of these classes should contain a method for drawing that takes a Graphics parameter. Then in your main class you have a collection of shapes, then in your paint event for you panel/form you simple loop the collection and call the draw method on each shape by passing the graphics instance from the paintEventArgs. So Then all you need to do on button clicks is to create a shape, set the properties, add it to the collection, then call invalidate() for the panel/form. Next, to accomplish the task of resizing the shapes. I assume it would be logical to resize all the shapes on the screen. So create a handler for the Resize event of the form. Then in here you can go through your collection of shapes and change the location/size as needed. You could also modify the shapes Draw method to take parameters for the width/height of the form and draw each time based on that. Then you just need to call Invalidate() in the resize event. Hope this helps
Life goes very fast. Tomorrow, today is already yesterday.
The only thing i don't understand is that I thought it would be more appropriate to use a refresh event opposed to the invalidate event you suggested, is there a significant difference in the two events? Ok what i have from the ground up is: A class called shapes which contains all the code for drawing the shapes
public static void Square(Graphics g) { g.DrawSquare(Pens.Blue, 10, 10, this.width, this.height); }
etc and then in the form i have a resize event which contains the code to draw the shapes
private void DrawBoard\_Paint(object sender, PaintEventArgs e) { Draw.Square(e.Graphics); }
and for the resize event I have
private void DrawBoard\_Resize(object sender, EventArgs e) { this.Refresh(); }
what i specifically need help with is (as said by someone else) to "call the function that you have written in the class." because it's multiple shapes being drawn and each being drawn on demand not when the app starts and I also need to pass the width and height parameter on the click event I would assume. in a nutshell I don't know the code to cause the click event to call upon the function in the class that draws the shape as well as the code to pass the width and height to the class Thanks in advance.
modified on Wednesday, April 29, 2009 7:13 AM
-
The only thing i don't understand is that I thought it would be more appropriate to use a refresh event opposed to the invalidate event you suggested, is there a significant difference in the two events? Ok what i have from the ground up is: A class called shapes which contains all the code for drawing the shapes
public static void Square(Graphics g) { g.DrawSquare(Pens.Blue, 10, 10, this.width, this.height); }
etc and then in the form i have a resize event which contains the code to draw the shapes
private void DrawBoard\_Paint(object sender, PaintEventArgs e) { Draw.Square(e.Graphics); }
and for the resize event I have
private void DrawBoard\_Resize(object sender, EventArgs e) { this.Refresh(); }
what i specifically need help with is (as said by someone else) to "call the function that you have written in the class." because it's multiple shapes being drawn and each being drawn on demand not when the app starts and I also need to pass the width and height parameter on the click event I would assume. in a nutshell I don't know the code to cause the click event to call upon the function in the class that draws the shape as well as the code to pass the width and height to the class Thanks in advance.
modified on Wednesday, April 29, 2009 7:13 AM
ok so if i got you correctly... your square function needs to have additional params for the height and width values. your button should also call this.Invalidate() which should result in the square being drawn
Johnny Jackson wrote:
is there a significant difference in the two events?
Not entirely sure but i think Invalidate() redraws the control itself, and Refresh() redraws the control and all child controls within it.
Life goes very fast. Tomorrow, today is already yesterday.