INTRODUCTION In a previous tutorial we talked about graphics in C# and the .Net environment. In this tutorial we will present the basic steps for drawing graphics.
HOW TO SPECIFY LOCATION AND SIZE OF CONTROLS One of the most common uses for the System.Drawing namespace is specifying the location and size of controls in a windows form application. To specify a control’s location you must create a point structure that corresponds to the relative point you want to define. The coordinates of this structure are relative to the upper left corner of the form. The point structure accepts integer values, while the pointF structure takes floating values. However, the pointF cannot be used to specify the location of controls. To move a button 10 pixels from the top and left sides of the form use the following code: button1.Location = new point(10, 10); The same procedure is used to change the colors of controls. You change the ForeColor or BackColor property of the appropriate controls. DRAW LINES AND SHAPES To draw on a form control you should conduct the necessary steps: · Create a Graphics object by calling the appropriate method. · Create a pen object. · Call a member of the graphics class to draw on the control using the pen object. · Once you create the graphics object you can call many methods to complete the drawing: · Clear: Clears the drawing surface and fills it with a specified color. · DrawLine: draws a line connecting two points. · DrawLines: Draws a series of lines that connect an array of Point structures. · DrawPolygon: Draws a shape as defined by an array of Point structures. · DrawRectangle: Draws a rectangle specified by a coordinate pair, a width and a length parameter. In order to use these methods you have to provide an instance of the Pen class. You can specify the pen’s color and width in pixels before drawing. The pen class provides several dash styles you can choose from. Finally, you can control the end caps in order to draw arrows or other special effects. Copy the following code in a windows form application and see its result. Remember to add a button first: private void button1_Click(object sender, EventArgs e) { Graphics myG = this.CreateGraphics(); Pen myP = new Pen(Color.Red); myP.EndCap = LineCap.RoundAnchor; myP.DashStyle = DashStyle.Dash; myP.Width = 8; Point p1 = new Point(10,10); Point p2 = new Point(200,10); myG.DrawLine(myP, p1, p2); } Instead of a line we could have created a polygon or an arc. However, these objects would be empty on the inside. In order to fill shapes the Graphics class has Fill methods that draw a shape and fill in the contents. The only difference is that these methods require an instance of a Brush class instead of a Pen class. Actualy, you do not instantiate the Brush class but one of its derived classes such as: · SolidBrush: Defines a brush of solid color. · TextureBrush: Defines a brush made from an image. · LinearGradientBush: brush with linear gradient providing an appealing look. In order to fill a polygon, for example you have to call the suitable method GraphicsObject.FillPolygon(). You can also combine the Draw and Fill methods to create solid objects with outlines.
Trackback(0)
 |