Minesweeper: GDI + LINE scratch

Source: Internet
Author: User

This article will demonstrate some basic usage of GDI + painting. First, let's do something evil and directly throw an exception in the onpaint event:

Protected override void onpaint (painteventargs E)
{
Base. onpaint (E );
Throw new exception ();
}

To suppress the dialog box that prompts to throw an exception for processing, you can add the following line to the main function:

Application. threadexception + = (S, e) => {};

In this way, we will get a red edge and cross line window, as shown in, when an exception occurs during winforms window painting, the system will display this image. This image is not affected by the internal status of graphics, such as pageunit, pagescale, anti-sawtooth, and transform.

Next we will use the GDI + function to simulate this error output. We can achieve this goal by understanding drawline and pen.
Let's first draw the two line segments above and left:

Using (PEN = new pen (color. Red, 4 ))
{
E. Graphics. drawline (pen, 0, 0, clientsize. Width, 0 );
E. Graphics. drawline (pen, 0, 0, 0, clientsize. Height );
}

First, create a red pen with a width of 4. The width of the pen will naturally be 4 pixels wide when the line is painted.
Note that if you want to debug the drawing code of GDI + and want to see the intermediate output, it is best to configure a dual-display to view the Code while viewing the output, this prevents Visual Studio from blocking the program window during code tracing, resulting in re-painting and unsatisfactory debugging results.

In addition to specifying the color and width of the pen, you can also modify the attributes of the Pen. Therefore, you can draw the remaining four lines without creating a new pen object. The complete code is as follows:

Rectangle rc = clientrectangle;

Using (PEN = new pen (color. Red, 4 ))
{
E. Graphics. drawline (pen, 0, 0, RC. Right, 0 );
E. Graphics. drawline (pen, 0, 0, 0, RC. Bottom );

Pen. width = 2;
E. Graphics. drawline (pen, RC. Right, 0, RC. Right, RC. Bottom );
E. Graphics. drawline (pen, 0, RC. Bottom, RC. Right, RC. Bottom );
E. Graphics. drawline (pen, 0, 0, RC. Right, RC. Bottom );
E. Graphics. drawline (pen, 0, RC. Bottom, RC. Right, 0 );
}

Let's take a look at all the reloads of drawline:

Drawline (pen, point, point)
Drawline (pen, pointf, pointf)
Drawline (pen, int32, int32, int32, int32)
Drawline (pen, single, Single)

We can use two points or four coordinates to create a line segment. But why are there two overloads of int and float? This is related to the unit, scale, and transform in GDI +. The floating point coordinate has its own meaning. The next article will introduce the coordinate system and unit of GDI +.

Graphics also provides a drawlines function that specifies multiple line segments to be drawn through the coordinate array. Therefore, the code can be improved:

Rectangle rc = clientrectangle;
Point lT = RC. location;
Point RT = new point (RC. Right, 0 );
Point RB = new point (RC. size );
Point lB = new point (0, RC. Bottom );

Using (PEN = new pen (color. Red, 4 ))
{
E. Graphics. drawlines (pen, new point [] {LB, LT, RT });

Pen. width = 2;
E. Graphics. drawlines (pen, new point [] {RB, LB, RT, Rb, Lt });
}

As you can see, the diagonal lines in the middle will have obvious sawtooth phenomena. The following article will introduce the anti-sawtooth function in GDI +. This article only describes the color and width attributes of the pen, and then introduces other common attributes.

Errata and update will be added to this document in a timely manner, and the update will be recorded in the minesweeper series index.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.