- 积分
- 14210
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2014-6-21
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2014-8-6 09:22:25
|
显示全部楼层
温度、湿度控件源码[原创]
上一个贴只是一个如何使用温湿度控件的一个程序例子,里面还有水印,那么可能有的朋友想知道那些控件是怎么制作的,建议有基础的可以看看控件的源码,制作控件的过程很简单很多教科书也都有说明,制作时只需要一个 pictureBox 控件即可。以下仅公布源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
namespace AWS_CuitLu
{
public partial class UseTa : UserControl
{
private float ta = 0;//控件填充值
private float mmin = 0;//最小值控件最低点
private float mmax = 100;//最大值控件最高点
private int mfont = 8;//字体大小
Color mc = Color.Red;//颜色
private string mname = "Ta(℃)";//控件名字,您可以把它写成温度、湿度、气压、雨量、蒸发任意一个
public string Mname //控件名字属性
{
get { return (mname); }
set { mname = value; }
}
public int MFont //控件字体属性
{
get { return (mfont); }
set { mfont = value; }
}
public UseTa()
{
InitializeComponent();
}
public float Min
{
get { return (mmin); }
set { mmin = value; }
}
public float Max
{
get { return (mmax); }
set { mmax = value; }
}
public float TA
{
get { return (ta); }
set { ta = value; }
}
public Color MC
{
get { return (mc); }
set {mc = value; }
}
public void drawTa()
{
pictureBox1.Image = mydrawTa(ta);
}
public void drawTa(float ita)
{
pictureBox1.Image = mydrawTa(ita);
}
private void UseTa_Load(object sender, EventArgs e)
{
pictureBox1.Height = this.Height;
pictureBox1.Width = this.Width;
drawTa();
}
private Bitmap mydrawTa(float s)
{
//控件的绘制就在下面代码完成,
//记住控件是可以任意大小的,但是空间高250,宽84效果最好。
//您也可以按照高250,宽84的比例缩放效果也还可以
Bitmap bitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
Graphics gu = Graphics.FromImage(bitmap);
gu.Clear(Color.White);
gu.SmoothingMode = SmoothingMode.AntiAlias;
float ax = (float)(pictureBox1.Width) / (float)84;
float ay = (float)(pictureBox1.Height) / (float)250;
float mdelay = (mmax - mmin) / 10;//间隔
Pen p = new Pen(Color.Gainsboro, 6);
Pen ufpen = new Pen(mc, 7);
ufpen.EndCap = LineCap.Round;
gu.DrawEllipse(ufpen, 31 * ax, 214 * ay, 3, 3);
PointF p1 = new PointF(28.0f * ax, 221.0f * ay);
PointF p9 = new PointF(28.0f * ax, 205.0f * ay);
PointF p2 = new PointF(18.0f * ax, 197.0f * ay);
PointF p3 = new PointF(18.0f * ax, 221.0f * ay);
PointF p4 = new PointF(28.0f * ax, 228.0f * ay);
PointF p5 = new PointF(38.0f * ax, 228.0f * ay);
PointF p6 = new PointF(48.0f * ax, 221.0f * ay);
PointF p7 = new PointF(48.0f * ax, 197.0f * ay);
PointF p10 = new PointF(38.0f * ax, 205.0f * ay);
PointF p8 = new PointF(38.0f * ax, 221.0f * ay);
PointF[] points = new PointF[] { p1, p9, p2, p3, p4, p5, p6, p7, p10, p8 };
Font mfont2 = new Font("Times New Roman", 8);
gu.FillPolygon(Brushes.Brown, points, FillMode.Winding);
//ufpen.StartCap = LineCap.RoundAnchor;
Pen sdpen = new Pen(Color.Black, 6);
try
{
if (s >=mmin && s <= mmax)
{
gu.DrawLine(ufpen, 32.0f * ax, 214.0f * ay, 32.0f * ax, (214.0f - (s-mmin)/mdelay * 20.0f) * ay);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\r\n");
}
Pen mpen = new Pen(Color.Black, 1);
Pen mypen = new Pen(Color.Black, 1);
Pen graypen = new Pen(Color.Gainsboro, 6);
Font myfont = new Font("华文新魏", mfont);
gu.DrawLine(p, (int)(25.0f * ax), 1, (int)(25.0f * ax), (int)(200.0f * ay));
gu.DrawLine(graypen, (int)(40.0f * ax), 1, (int)(40.0f * ax), (int)(200.0f * ay));
for (int y = 10; y < 212; y += 2)
{
Pen tPen = (y % 10 == 0) ? mypen : mpen;
if (tPen == mypen)
{
Point pt1 = new Point((int)(40.0f * ax), (int)(y * ay));
Point pt2 = new Point((int)(48.0f * ax), (int)(y * ay));
gu.DrawLine(mypen, pt1, pt2);
Rectangle Rect = new Rectangle((int)(48.0f * ax), (int)((y - 3) * ay), (int)(30.0f * ax), (int)(10.0f * ay));
switch (y)
{
case 10:
{
gu.DrawString(mmax.ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 30:
{
gu.DrawString((mmax-mdelay).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 50:
{
gu.DrawString((mmax - mdelay*2).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 70:
{
gu.DrawString((mmax - mdelay*3).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 90:
{
gu.DrawString((mmax - mdelay*4).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 110:
{
gu.DrawString((mmax - mdelay*5).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 130:
{
gu.DrawString((mmax - mdelay*6).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 150:
{
gu.DrawString((mmax - mdelay*7).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 170:
{
gu.DrawString((mmax - mdelay*8).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 190:
{
gu.DrawString((mmax - mdelay*9).ToString("0"), myfont, Brushes.Black, Rect);
break;
}
case 210:
{
gu.DrawString(mmin.ToString("0"), myfont, Brushes.Black, Rect);
break;
}
}
}
else
{
Point pta = new Point((int)(40.0f * ax), (int)(y * ay));
Point ptb = new Point((int)(43.0f * ax), (int)(y * ay));
gu.DrawLine(tPen, pta, ptb);
}
}
Brush brush = new SolidBrush(Color.FromArgb(64, Color.Red));
gu.DrawString(mname, mfont2, Brushes.Black, 0, 0);//这句话是将控件名绘制在控件上的,您不想要就要注释了
gu.DrawString("CUIT-CDIO", mfont2, brush, 0f, 15f);//这句话是给控件添加水印的,您可以修改或注释。
gu.Dispose();
return (bitmap);
}
}
}
|
评分
-
查看全部评分
|