C# 背景与前景

一 背景与前景

背景:填充整个区域的、一般不变化的;

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并不支持。

1 多段拟合法

一条曲线可以看成由无数条极小线段连接二成,当线段足够短时,视觉上近似为一条曲线。

二 曲线的设置

演示:在界面上调整曲线的参数设置。
NumericUpDown控件,用于数字微雕。

1 实现

① 添加3个NumericUpDown控件,分别控制粒度、周期、振幅参数;
② 添加事件响应处理:一个回调方法,同时注册给三个控件;
③ 更新曲线的显示;

三 要点与细节

1 一个回调方法可以同时注册给多个控件;

2 为了减轻重绘时的闪烁问题,可以使用“双缓冲”在自定义控件中,启用DoubleBuffer双缓冲特性;

源代码
曲线的设置.rar: https://url09.ctfile.com/f/22158009-727364279-442f9a?p=5939 (访问密码: 5999)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值