65.9K
CodeProject 正在变化。 阅读更多。
Home

模拟仪表

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.12/5 (17投票s)

2008年4月4日

CPOL

2分钟阅读

viewsIcon

67081

downloadIcon

4225

模拟仪表控件, 可以设置自定义渲染器

引言

在互联网上,我找到了很多关于模拟仪表的项目,但很少有项目能够使用自定义渲染器更改其外观。 因此,我决定用 C# 创建我的第一个控件,并在我的下一个项目中使用此控件。

背景

要编译此演示,您需要 .NET 2.0,可以从 此处 下载。 该项目使用 SharpDevelop 开发,这是一个免费的 .NET IDE。

Using the Code

在 zip 文件中有一个完整的解决方案,但使用控件的代码并将其插入到另一个项目中也足够简单。 在下一节中,将介绍如何使用该类。

Control 类

该类派生自 System.Windows.Forms.UserControl。 通常,此类有一个默认的渲染器类,用于绘制所有部件,但可以设置一个自定义渲染器来绘制单个部件或整个控件。 控件的主要类是

namespace LBSoft.IndustrialCtrls.Meters
{
    ///
    /// Class for the analog meter control
    ///
    
    public partial class LBAnalogMeter : UserControl
    {        

    ...

    }
}

您可以在设计时设置以下属性

外观属性

  • MeterStyle - 控件的样式。 目前,唯一的样式是 AnalogMeterStyle.Circular
  • BodyColor - 控件主体的颜色。
  • NeedleColor - 指针的颜色
  • ScaleColor - 控件刻度线标记的颜色
  • ScaleDivisions - 刻度的主要刻度线数量
  • ScaleSubDivisions - 刻度盘上一个主刻度到另一个主刻度之间的子刻度的数量
  • ViewGlass - 用于可视化玻璃效果的标志

行为属性

  • Value - 控件的当前值
  • MinValue - 控件的最小值
  • MaxValue - 控件的最大值

在下一节中,将介绍渲染器类以及如何自定义控件的绘制。

渲染器类

渲染器类是一个具有几个虚拟方法的类,这些方法用于设计控件的各个部分。

namespace LBSoft.IndustrialCtrls.Meters
{
    ///
    /// Base class for the renderers of the analog meter
    ///
    public class LBAnalogMeterRenderer
    {
        ///
        /// Control to render
        ///
        private LBAnalogMeter meter = null;
        
        ///
        /// Control properties
        //
        public LBAnalogMeter AnalogMeter
        {
            set { this.meter = value; } 
            get { return this.meter; } 
        }
        
        ///
        /// Draw the background of the control
        ///
        public virtual bool DrawBackground( Graphics gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Draw the body of the control
        ///
        public virtual bool DrawBody( Graphics Gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Draw the scale of the control
        ///
        public virtual bool DrawDivisions( Graphics Gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Draw the thresholds 
        ///
        public virtual bool DrawThresholds( Graphics gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Drawt the unit measure of the control
        ///
        public virtual bool DrawUM( Graphics gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Draw the current value in numerical form
        ///
        public virtual bool DrawValue( Graphics gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Draw the needle 
        ///
        public virtual bool DrawNeedle( Graphics Gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Draw the needle cover at the center
        ///
        public virtual bool DrawNeedleCover( Graphics Gr, RectangleF rc )
        {
            return false;
        }
        
        ///
        /// Draw the glass effect
        ///
        public virtual bool DrawGlass( Graphics Gr, RectangleF rc )        
        {
            return false;
        }
    }
}

基类的所有方法都返回 False。 如果控件类有一个自定义渲染器,它将调用自定义渲染器,如果调用的方法返回 False,则调用默认渲染器的相同方法。

例如,如果您想消除控件主体的绘制,请使用以下步骤

  • 创建一个派生自 LBAnalogMeterRenderer 的类
  • 重写方法 DrawBody
  • 在此方法中返回 True
  • 在主窗体中创建自定义渲染器的实例
  • 使用 Renderer 属性将渲染器设置给控件

代码

namespace TestApp
{
    ///
    /// Custom renderer
    ///
    public class LBNoBodyAnalogMeterRenderer : LBAnalogMeterRenderer
    {
        public override bool DrawBody( Graphics Gr, RectangleF rc )
        {
            return true;
        }
    }
    
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        // Declare the renderer members of the form
        LBNoBodyAnalogMeterRenderer customRenderer = null;
        ...
        
        public MainForm()
        {
            InitializeComponent();
            
            // Create the renderer
            this.customRenderer = new LBNoBodyAnalogMeterRenderer;
            
            // Set the renderer
            this.lbAnalogMeter.Renderer = this.customRenderer;
            ...
        }
        ...
    }
}

结论

这是该控件的初步版本,许多功能尚未实现,但我希望尽快实现

对于这篇文章,我使用了这篇文章的代码和想法

关注点

非常感谢任何建议/评论/反馈

历史

  • 0.1 (2008 年 4 月 4 日)
    • 初步版本
© . All rights reserved.