c#如何实现在窗体中绘制直线

2025-03-10 00:42:56
推荐回答(1个)
回答1:

就是重载窗体的OnPain方法,见下图

出现智能提示后,按【Tab】即可

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

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

        //重载后方法!
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawLine(Pens.Black, 10, 10, 100, 100);
        }
    }
}

重载OnPain方法后,每次窗体显示内容被更新时,就调用次方法来刷新窗体。