Windows forms drawing tutorial




















Blue, 3 ;. DrawPie p, 1, 1, , , , 60 ;. A structure is a composite of other types that make easier to work with related data. The simplest example is System. Point, which contain X and Y integer properties that define the horizontal and vertical coordinates of a point. The Point structure simplifies working with coordinates by providing constructors and members as follows:.

Point 20, 20 ;. Offset -1, Just create a new Point structure by specifying the coordinates relative to the upper-left corner of the form, and use the Point to set the control's Location property. DrawLines, Graphics. DrawPolygons, and Graphics.

DrawRectangles accept arrays as parameters to help you draw more complex shapes: Here is an example of a polygon, but with its contents filled. Notice the code responsible for filling the shape: DrawPolygonAndFill. Maroon ;. FillPolygon b, points ;. The endcaps are the end of the line, and you can use them to create arrows and other special effects.

While pens by default draw straight lines, their properties can be set to one of these values: DashStyle. Dash, DashStyle. DashDot, DashStyle. DashDotDot, DashStyle. Dot, or DashStyle. Consider this final example: UsePen. Red, 10 ;. DrawLine p, 50, 25, , 25 ;. DrawLine p, 50, 50, , 50 ;. DrawLine p, 50, 75, , 75 ;. DrawLine p, 50, , , ;. This should the new developer to understand that these principles can be expanded upon to specify the size and location of controls that are dragged and dropped onto a Windows Form.

For instance, if we were to start Visual Studio and begin a Forms application, we could set that Form background color to white. The designer file and the source file that comprise the application would both reflect that property: the setting of the property would cause the IDE to generate the code in the Form.

Well, that all for now. And how those would be handled then? In case we need to render something more complex than just one or two pictures, we use Graphics. The principle is we place a single PictureBox on the form and draw what we need on its canvas. We'll simplify the application considerably, it doesn't need to be complicated. It'll be able to display a single hall only, which will be initially empty.

The user will click the seats to make them occupied using the mouse, and then they will press the Save button, which will create a simple text file at the selected location.

The file will contain information about the occupancy of the hall. The reason of saving the data is to learn how to work with dialogues. Add a PictureBox to cover most of the form and name it as cinemaPictureBox. Finally, we'll drag SaveFileDialog onto the form. This control won't be placed directly on the form, but in a bar below it, because it's not an actual form control, but merely a helper component.

We'll rename it to cinemaSaveFileDialog. You can play with the anchors as you want. Your form should look like this:. You're probably not surprised that we'll add a Cinema class. It'll have a single private field, a two-dimensional array of seats. If you haven't worked with a 2D array yet, you can think of it as a table. A one-dimensional classic array is just a single row. With the 2D array we work exactly the same as with the one-dimensional array, except that we have to specify two coordinates X and Y.

In many languages, 2D arrays are created as array of arrays, but C allows us to declare 2D arrays directly as follows:.

The seats are of the bool type, because we only care whether it's vacant or occupied. We'll also add 2 private constants to the class, one defining the size of rendered seats in pixels, and the other one defining the space between the seats in pixels.

Get used to constants, because when you'll want to resize the seats, you'll just change the value of a constant without having to mess with the rendering code.

The cinema hall should be able to render itself. We've already mentioned that we'll draw on a canvas. This canvas is of the Graphics type, and we'll request it as a Draw method parameter. You need to add using System. Drawing for the Graphics type to use, but it certainly didn't surprise you. We draw on the canvas using its methods. For now, we'll be interested only in the FillRectangle method, which draws a rectangle filled with a specific color. There are a lot of methods for different geometric shapes, both filled or unfilled.

You can check them out, we'll try some of them in following lessons. In Graphics , there are two types of color - the fill color Brush and the outline color Pen. Drawing2D Provides advanced two-dimensional and vector graphics functionality. The classes in this namespace can be used to create and use collections of fonts.

Printing Provides printing functionality. Custom Control Painting and Rendering Details how to provide code for painting controls. Skip to main content. This browser is no longer supported.



0コメント

  • 1000 / 1000