让知识更加联贯 让技术走进生活
我的博客 我的程序 我的网络
------郑紫至
E-mail:zhengzizhi@yahoo.com.cn
地磅称量系统
57. 在WinApp项目下新建LCD控件并设置BackColor为Black 区域大小Size为(448, 64)
然后可以设计代码了,完整的代码如下:
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;
namespace WinApp
{
public
partial class
LCD : UserControl
{
public LCD()
{
SetStyle(ControlStyles.UserPaint |
ControlStyles.DoubleBuffer
|
ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw
|
ControlStyles.SupportsTransparentBackColor, true);
this.UpdateStyles();
Init();
InitializeComponent();
}
System.Drawing.Graphics g =
null;
private
void Init()
{
m_Bitmap =
new System.Drawing.Bitmap(this.Width,
this.Height);
g = Graphics.FromImage(m_Bitmap);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
}
//LCD 显示的内容
string _LCDShowStringValue =
String.Empty;
public
string LCDShowStringValue
{
get {
return this._LCDShowStringValue; }
set
{
if (this._LCDShowStringValue !=
value)
{
this._LCDShowStringValue =
value;
this.Invalidate();
}
}
}
int _LCDStringWidth;
public
int LCDStringWidth
{
get {
return this._LCDStringWidth; }
}
int _LCDStringHeight;
public
int LCDStringHeight
{
get {
return this._LCDStringHeight; }
}
//LCD内显示字体颜色
System.Drawing.Color _LCDShowFontColor = System.Drawing.Color.Lime;
public System.Drawing.Color LCDShowFontColor
{
get {
return this._LCDShowFontColor; }
set
{
this._LCDShowFontColor =
value;
this.Invalidate();
}
}
System.Drawing.Bitmap m_Bitmap =
null;
protected
override void OnPaint(PaintEventArgs e)
{
m_Bitmap = DrawLCD();
System.Drawing.Graphics grfx = e.Graphics;
grfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
grfx.DrawImageUnscaled(m_Bitmap, 0, 0);
}
public System.Drawing.Bitmap DrawLCD()
{
return
this.DrawLCD(this.ClientRectangle);
}
//LCD 显示字体是否有阴影,默认无阴影
bool _IsDrawShadow =
false;
public
bool LCDIsDrawShow
{
get {
return this._IsDrawShadow; }
set {
this._IsDrawShadow = value; }
}
public System.Drawing.Bitmap DrawLCD(System.Drawing.Rectangle destRect)
{
m_Bitmap =
new Bitmap(destRect.Width, destRect.Height);
System.Drawing.Graphics grfx = System.Drawing.Graphics.FromImage(m_Bitmap);
grfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
grfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
grfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
SevenSegmentDigitGraphicsDisplay ssdgd =
new SevenSegmentDigitGraphicsDisplay(grfx);
ssdgd.IsDrawShadow =
this._IsDrawShadow;
System.Drawing.Drawing2D.GraphicsState gs = grfx.Save();
grfx.TranslateTransform(destRect.X, destRect.Y);
string StringLCD = (this.LCDShowStringValue ==
null ? "" :
this.LCDShowStringValue);
System.Drawing.SizeF sizef = ssdgd.MeasureString(StringLCD, Font);
float fscale =
Math.Min(destRect.Width / sizef.Width, destRect.Height / sizef.Height);
Font font =
new Font(Font.FontFamily, fscale * Font.SizeInPoints);
sizef = ssdgd.MeasureString(StringLCD, font);
this._LCDStringWidth = (int)sizef.Width;
this._LCDStringHeight = (int)sizef.Height;
ssdgd.MeasureString(StringLCD, font,
new System.Drawing.SolidBrush(this._LCDShowFontColor),
(destRect.Width - sizef.Width) / 2, (destRect.Height - sizef.Height) / 2);
grfx.Restore(gs);
return m_Bitmap;
}
}
public
class SevenSegmentDigitGraphicsDisplay
{
System.Drawing.Graphics grfx;
System.Drawing.Brush _brush = System.Drawing.Brushes.Black;
bool _IsDrawShadow =
true;
System.Drawing.Color _ShadowColor =
System.Drawing.Color.FromArgb(60, System.Drawing.Color.White);
System.Drawing.Brush _ShadowBrush =
null;
static
byte[,] bySegment ={
{ 1, 1, 1, 0, 1, 1, 1 },
// 0
{ 0, 0, 1, 0, 0, 1, 0 },
// 1
{ 1, 0, 1, 1, 1, 0, 1 },
// 2
{ 1, 0, 1, 1, 0, 1, 1 },
// 3
{ 0, 1, 1, 1, 0, 1, 0 },
// 4
{ 1, 1, 0, 1, 0, 1, 1 },
// 5
{ 1, 1, 0, 1, 1, 1, 1 },
// 6
{ 1, 0, 1, 0, 0, 1, 0 },
// 7
{ 1, 1, 1, 1, 1, 1, 1 },
// 8
{ 1, 1, 1, 1, 0, 1, 1 } // 9
};
readonly System.Drawing.Point[][] SevenSegmentDigitGraphics =
new System.Drawing.Point[7][];
public
bool IsDrawShadow
{
get {
return this._IsDrawShadow; }
set {
this._IsDrawShadow = value; }
}
public SevenSegmentDigitGraphicsDisplay(System.Drawing.Graphics grfx)
{
this.grfx = grfx;
SevenSegmentDigitGraphics[0] =
new Point[] {
new
Point( 3, 2), new
Point(39, 2),
new
Point(31, 10), new
Point(11, 10)
};
SevenSegmentDigitGraphics[1] =
new Point[] {
new
Point( 2, 3), new
Point(10, 11),
new
Point(10, 31), new
Point( 2, 35)
};
SevenSegmentDigitGraphics[2] =
new Point[] {
new
Point(40, 3), new
Point(40, 35),
new
Point(32, 31), new
Point(32, 11)
};
SevenSegmentDigitGraphics[3] =
new Point[] {
new
Point( 3, 36), new
Point(11, 32),
new
Point(31, 32), new
Point(39, 36),
new
Point(31, 40), new
Point(11, 40)
};
SevenSegmentDigitGraphics[4] =
new Point[] {
new
Point( 2, 37), new
Point(10, 41),
new
Point(10, 61), new
Point( 2, 69)
};
SevenSegmentDigitGraphics[5] =
new Point[] {
new
Point(40, 37), new
Point(40, 69),
new
Point(32, 61), new
Point(32, 41)
};
SevenSegmentDigitGraphics[6] =
new Point[] {
new
Point(11, 62), new
Point(31, 62),
new
Point(39, 70), new
Point( 3, 70)
};
}
public System.Drawing.SizeF MeasureString(string LCDStringValue,
System.Drawing.Font font)
{
System.Drawing.SizeF sizef =
new System.Drawing.SizeF(0,
grfx.DpiX * font.SizeInPoints / 72);
for (int i = 0; i < LCDStringValue.Length; i++)
{
if (char.IsDigit(LCDStringValue[i]))
{
sizef.Width += 42 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
else
if (LCDStringValue[i] ==
'-')
{
sizef.Width += 42 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
else
if (LCDStringValue[i] ==
':')
{
sizef.Width += 20 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
else
if (LCDStringValue[i] ==
' ')
{
sizef.Width += 36 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
else
if (LCDStringValue[i] ==
'.')
{
sizef.Width += 20 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
}
return sizef;
}
public
void MeasureString(string LCDStringValue, System.Drawing.Font font,
System.Drawing.Brush brush,
float x, float y)
{
this._brush = brush;
this._ShadowBrush =
new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(40,
((System.Drawing.SolidBrush)this._brush).Color));
for (int i = 0; i < LCDStringValue.Length; i++)
{
if (char.IsDigit(LCDStringValue[i]))
{
x = Number(LCDStringValue[i] -
'0', font, brush, x, y);
}
else
if (LCDStringValue[i] ==
'-')
{
x = MinusSign(font, brush, x, y);
}
else
if (LCDStringValue[i] ==
':')
{
x = Colon(font, brush, x, y);
}
else
if (LCDStringValue[i] ==
' ')
{
x = DrawSpace(font, brush, x, y);
}
else
if (LCDStringValue[i] ==
'.')
{
x = DrawDot(font, brush, x, y);
}
}
}
private
float Number(int num, System.Drawing.Font font, System.Drawing.Brush brush,
float x,
float y)
{
for (int i = 0; i < SevenSegmentDigitGraphics.Length; i++)
{
if (this._IsDrawShadow)
{
Fill(SevenSegmentDigitGraphics[i], font,
this._ShadowBrush, x, y);
}
if (bySegment[num, i] == 1)
{
Fill(SevenSegmentDigitGraphics[i], font, brush, x, y);
}
}
return x + 42 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
private
float MinusSign(Font font,
Brush brush, float x,
float y)
{
Fill(SevenSegmentDigitGraphics[3], font, brush, x, y);
return x + 42 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
private
float DrawSpace(Font font,
Brush brush, float x,
float y)
{
return x + 36 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
private
float Colon(Font font,
Brush brush, float x,
float y)
{
Point[][] point =
new Point[2][];
point[0] =
new Point[] {
new
Point( 4, 12), new
Point( 16, 12),
new
Point(16, 24), new
Point( 4, 24)
};
point[1] =
new Point[] {
new
Point( 4, 50), new
Point( 16, 50),
new
Point(16, 62), new
Point( 4, 62)
};
for (int i = 0; i < point.Length; i++)
{
Fill(point[i],
font, brush, x, y);
}
return x + 20 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
private
float DrawDot(Font font,
Brush brush, float x,
float y)
{
Point[][] point =
new Point[1][];
point[0] =
new Point[] {
new
Point( 4, 50), new
Point( 16, 50),
new
Point(16, 62), new
Point( 4, 62)
};
Fill(point[0], font, brush, x, y);
return x + 20 * grfx.DpiX * font.SizeInPoints / 72 / 72;
}
private void Fill(System.Drawing.Point[] SevenSegmentDigitGraphics, System.Drawing.Font font,System.Drawing.Brush
brush, float x, float y)
{
System.Drawing.PointF[] pointf =
new System.Drawing.PointF[SevenSegmentDigitGraphics.Length];
for (int i = 0; i < SevenSegmentDigitGraphics.Length; i++)
{
pointf[i].X = x +
SevenSegmentDigitGraphics[i].X * grfx.DpiX * font.SizeInPoints / 72 / 72;
pointf[i].Y = y +
SevenSegmentDigitGraphics[i].Y * grfx.DpiY * font.SizeInPoints / 72 / 72;
}
grfx.FillPolygon(brush, pointf);
}
}
}
阅读上面的代码可能你看起来很费劲 七段图形的每个点的坐标为什么要定义为哪个大小的数字?
请参考我提供的在设计编码之前的模拟草图 草图如下所示: