背景:填充整个区域的、一般不变化的;
OnPaintBackground();
安装官方的建议,背景的绘制应该和前景分开。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using System.Drawing.Color;
using System.Drawing;
using System.Drawing.Drawing2D;namespace 自定义控件
{internal class selfControl:Control{public selfControl(){this.BackColor = Color.White;this.Size = new Size(100, 100);}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);Graphics g = e.Graphics;int w = this.Width, h = this.Height;Rectangle rect = new Rectangle(0, 0, w, h);//平滑绘制 反锯齿g.SmoothingMode = SmoothingMode.HighQuality;using (Brush brush = new SolidBrush(Color.LightSkyBlue)){rect.Inflate(-40, -40);//往里缩小一点Point[] points ={new Point(rect.Left,rect.Top),new Point(rect.Right,rect.Top),new Point(rect.Left+rect.Width/2,rect.Bottom)};g.FillPolygon(brush, points);}}protected override void OnPaintBackground(PaintEventArgs pevent){base.OnPaintBackground(pevent);Graphics g = pevent.Graphics;int w = this.Width, h = this.Height;Rectangle rect = new Rectangle(0, 0, w, h);//渐变色Brush lgbrush = new LinearGradientBrush(new Point(rect.Left, rect.Top),new Point(rect.Right, rect.Bottom),Color.FromArgb(255, 0, 0),Color.FromArgb(255, 255, 255));using(lgbrush){g.FillRectangle(lgbrush, rect);}}}
}
WinForm API支持的三种线条:
① 直线 DrawLine;
② 圆弧、椭圆弧DrawArc;
③ 贝赛尔曲线 DrawBezier;
像正弦这种特殊的曲线,API并不支持。
一条曲线可以看成由无数条极小线段连接二成,当线段足够短时,视觉上近似为一条曲线。
演示:在界面上调整曲线的参数设置。
NumericUpDown控件,用于数字微雕。
① 添加3个NumericUpDown控件,分别控制粒度、周期、振幅参数;
② 添加事件响应处理:一个回调方法,同时注册给三个控件;
③ 更新曲线的显示;
源代码
曲线的设置.rar: https://url09.ctfile.com/f/22158009-727364279-442f9a?p=5939 (访问密码: 5999)