C# draws simple graphics based on winform form

What is a form?

It can be understood as a customized console application.

If you only need to use VS to make games, then in VS, the window presented by the console application we usually use is far from enough. Therefore a custom form is required.

Therefore, when creating a new project, we can no longer use the console application, but should choose the form:

After that, the new form is completed.

Here you can run our newly created form directly:

The window that pops up below is our newly created form. Since we have not performed any operations on the form, the current form is blank. 

Secondly, after completion, you will find the following files in the project directory:

Form1.cs specifically places some codes related to interactive response time, logic processing, and business;

Designer manages the appearance of the form

Extension 1 - Set the generation location of the form:

If you need to customize the generation position, you can use the manual method, followed by the generation coordinate position of the form.

Expansion 2 - Controls in the form

What are controls?

Controls are no stranger to those with Android studio development experience. The so-called control is a graphical interactive component similar to a button that can respond to events and perform corresponding processing.

Position of controls in vs form:

Open the toolbar inside the view:

A list of controls will expand on the right, containing various controls:

We will not use any controls when developing games, but we will use a graphics device interface GDI. To put it simply, it is a drawing technology that can draw pictures on winform.

  Next, I will introduce you to how to draw pictures easily with GDI.

Before drawing graphics, we need to pay attention to the fact that the coordinate axis of this form is based on the upper left corner of the screen as the coordinate origin. This is very important for us to draw graphics and set coordinate points, as shown in the following figure:

Drawing of simple straight lines

Open the form1.cs file, there will be the following code:

 However, if you draw graphics directly in the constructor of form1, the window will not be drawn. This is because when you draw graphics in the constructor, the form has not been created yet, so there will be no graphics drawn.

Therefore, we need to draw the graphics after the form is created. The steps are as follows:

In the design window, right-click - Properties:

Find paint, click in the blank space, and create a new paint method:

 It can be found that the form1 code file will automatically create a new paint method for us. We can draw graphics directly in this method:

This step can be understood as creating a new drawing canvas:

 Drawing graphics uses some methods of draw, and drawing straight lines uses drawLine:

 Using the draw method requires passing information about the brush, starting point, and origin, so we defined a brush before.

The final code written and run results are as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ct1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color .Blue);
            g.DrawLine(p,new Point (1,1),new Point (100,100));
        }
    }
}

In order to verify the previous coordinates, I set the starting point coordinates of the straight line to (1, 1). As you can see, the position of the straight line (1, 1) is very close to the upper left corner of the screen.

You can also further test the coordinates of (0.00001, 0.0000001). The result should be infinitely close to the upper left corner of the form screen.

However, it is worth noting that the parameter value type of new's point class name is int type, but you can use cast type to complete the test.

Draw simple string

In fact, the drawing method is the same as above, except that the original drawline is changed to drawstring:

 

 The parameters passed are the string to be drawn, font (font form, size), brush (brush color), and drawing coordinates.

The finished effect is as follows:

 

Guess you like

Origin blog.csdn.net/qq_51196701/article/details/123086884