1、新建一个类
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
class MyControl : Control //继承Control类
{
public MyControl ()
{
this.BackColor = Color.White;
this.Size = new Size(100, 100);
//this.BackgroundImage = "@.//image//.png";
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen pen= new Pen(Color.Red);
g.DrawRectangle(pen, 0, 0, 30, 30);
g.DrawLine(pen, 0, 0, 40, 40);
pen.Dispose();//pen为系统资源,需要释放掉
}
}
}
2、工具——选项
写完类。要重新生成一下解决方案,然后重新打开项目,如果工具箱中依旧没有mycontrol控件,则讲下图选项置为true,重新生成项目后再打开即可
结果: