C# programming questions-forms, dialog boxes and menus-program for drawing lines and circles

Topic: Complete the graphic program: select the font through the font menu, select the color through the color menu, draw pictures in the form and output text descriptions. The Draw menu includes two menu items: Draw Line and Draw Circle.

The details are shown in the figure: Insert picture description here
There are several key points in this question.
One is that if you want to transfer the font and color into the circle and line you draw, then this value needs to be a global variable, otherwise it cannot be transferred into the
ToolStripMenuItem_Click_1() method .
The second is how to
create a Graphic object through the ToolStripMenuItem_Click_1() method when clicking the "circle" and "line" menus to achieve the effect of drawing.
If you want to understand these questions, this question is not difficult.
I thought about it for a long time, and finally realized the function as shown in the figure.
Insert picture description here
Insert picture description here
When clicking another dialog box, the previous graphic will not be automatically cleared
Finally, I don’t know if anyone has read this blog post. If anyone reads it, please click like below to let me know. I will release the source code.

The source code is here:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace 窗体对话框菜单作业
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        Color mycolor = new Color();
        Font myfont = new Font("宋体", 16, FontStyle.Regular);

        private void 画线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            Graphics g = this.CreateGraphics();
            Pen mypen = new Pen(mycolor);
            SolidBrush mybrush = new SolidBrush(mycolor);
            g.DrawLine(mypen, 0, 0, 500, 500);
            g.DrawString("线", myfont, mybrush, 80, 80);
        }

        private void 画圆ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
    
    
            Graphics g = this.CreateGraphics();
            Pen mypen = new Pen(mycolor);
            SolidBrush mybrush = new SolidBrush(mycolor);
            g.DrawEllipse(mypen, 100, 100, 100, 100);
            g.DrawString("圆", myfont, mybrush, 100, 100);
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
            {
    
    
                mycolor = cd.Color;
            }
            else
            {
    
    
                mycolor = Color.Black;
            }
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
    
            FontDialog fd = new FontDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
    
    
                Font myfont = fd.Font;
            }
        }
    }
}

See this, like it QAQ! ! It's too difficult for newcomers to be original! !

Guess you like

Origin blog.csdn.net/NikoHsu/article/details/105770603