首页 > Windows开发 > 详细

c# 创建自绘用户控件

时间:2019-09-29 20:37:38      阅读:104      评论:0      收藏:0      [点我收藏+]

一、继承UserControl类

public class Chart : UserControl

 

二、定义常量、私有成员变量、属性

  加入属性的修饰,可以在图形界面配置

  private const int LeftPos = 60;

  private int[] m_values = new int[3];

  [CategoryAttribute("Chart")]   [DescriptionAttribute("已解决的事务数量")]
  [DefaultValueAttribute(0)]   public int Resolved
  {   get {   return m_values[0];     } set {   //对输入值进行验证 if (value < 0) {   value = 0; } m_values[0] = value; //重绘控件 Invalidate();     }   }

 三、重写父类UserControl的几个事件处理

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    e.Graphics.Clear(Parent.BackColor);
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    //如果控件大小能显示下所有数据
    if (Width > LeftPos && m_barHeight > 1)
    {
        //绘制左边的标签
        DrawLabels(e.Graphics);
        //绘制图表
        DrawChart(e.Graphics);
    }
    // 总是绘制文本信息
    DrawMessage(e.Graphics);
}

protected override void OnSizeChanged(EventArgs e)
{
    // 调用CalculateBounds函数重新计算布局
    CalculateBounds();
    
    //调用基类处理函数
    base.OnSizeChanged(e);
}

// 计算布局及布局内元素的各种参数数值
private void CalculateBounds()
{ 
}

protected override void OnFontChanged(EventArgs e)
{
    base.OnFontChanged(e);
    CalculateBounds();
}

protected override void OnTextChanged(EventArgs e)
{
    base.OnTextChanged(e);
    CalculateBounds();
    // 立刻重绘
    Invalidate();
}

 

c# 创建自绘用户控件

原文:https://www.cnblogs.com/nw0220/p/11609152.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!