fff基于WINCE 6.0 .Net CompactFramework开发一个流体控件,主要目的就是让程序自动生成矩形图,定时几十毫秒更新一次就可看到图片要循环流动,在工业控制方面可动态显示某个设备在运行中。
如果是.NET GDI+ 不需要这么复杂,只需引用 Bitmap.RotateFlip(RotateFlipType.Rotate90FlipX);就可实现右至左,上到下,下到上的流动效果!!
主要代码如下:
//流动速度
private int speed = 10;
//矩形块大小
private int blockSize = 20;
//矩形块间的缝隙大小
private int chinkSize = 5;
//步伐大小
private int stepSize = 1;
//背景颜色
private Color backColor = Color.White;
//前景颜色
private Color foreColor = Color.Blue;
//记录偏移位置
private int offset = 0;
#region 左到右流动
///
/// 左到右流动-完成2019-09-17
///
public void LeftGoToRight() {
Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
Graphics ghp = Graphics.FromImage(map);
ghp.DrawImage(map, 0, 0);
ghp.Clear(this.backColor);
int count = this.Width / (chinkSize + blockSize);
//余数
if ((this.Width % (chinkSize + blockSize)) > 0) {
count++;
}
//压入第一个
if (offset > 0) {
if ((offset - chinkSize) > 0) {
if (offset <= (chinkSize + blockSize)) {
Rectangle first = new Rectangle(0, 0, offset - chinkSize, this.Height);
SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(firstBlueBrush, first);
firstBlueBrush.Dispose();
} else {
offset = stepSize;
}
}
}
for (int index = 0; index < count; index++) {
int mv = index * (chinkSize + blockSize);
Rectangle r = new Rectangle(offset + mv, 0, blockSize, this.Height);
SolidBrush blueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(blueBrush, r);
blueBrush.Dispose();
}
offset += stepSize;
ghp.Dispose();
//如果是.Net CompactFramework 下面方函数无法使用
//.NET 可用下面的方法翻转或垂直镜像
//map.RotateFlip(RotateFlipType.Rotate90FlipX);
this.pictureBox_main.Image = map;
}
#endregion
#region 右到左流动
///
/// 右到左流动
///
public void RightGoToLeft() {
Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
Graphics ghp = Graphics.FromImage(map);
ghp.DrawImage(map, 0, 0);
ghp.Clear(this.backColor);
int count = this.Width / (chinkSize + blockSize);
//余数
if ((this.Width % (chinkSize + blockSize)) > 0) {
count++;
}
//压入第一个
if (offset > 0) {
if ((offset - chinkSize) > 0) {
if (offset <= (chinkSize + blockSize)) {
Rectangle first = new Rectangle((this.Width + chinkSize) - offset, 0, offset - chinkSize, this.Height);
SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(firstBlueBrush, first);
firstBlueBrush.Dispose();
} else {
offset = stepSize;
}
}
}
for (int index = 0; index < count; index++) {
int mv = index * (chinkSize + blockSize);
int x = mv == 0 ? this.Width - blockSize : this.Width - mv - blockSize;
Rectangle r = new Rectangle(x - offset, 0, blockSize, this.Height);
SolidBrush blueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(blueBrush, r);
blueBrush.Dispose();
}
offset += stepSize;
ghp.Dispose();
this.pictureBox_main.Image = map;
}
#endregion
#region 从顶部向下暴布流下
///
/// 从顶部向下暴布流下
///
public void TopGoToBottom() {
Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
Graphics ghp = Graphics.FromImage(map);
ghp.DrawImage(map, 0, 0);
ghp.Clear(this.backColor);
int count = this.Height / (chinkSize + blockSize);
//余数
if ((this.Height % (chinkSize + blockSize)) > 0) {
count++;
}
//压入第一个
if (offset > 0) {
if ((offset - chinkSize) > 0) {
if (offset <= (chinkSize + blockSize)) {
Rectangle first = new Rectangle(0, 0, this.Width, offset - chinkSize);
SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(firstBlueBrush, first);
firstBlueBrush.Dispose();
} else {
offset = stepSize;
}
}
}
for (int index = 0; index < count; index++) {
int mv = index * (chinkSize + blockSize);
Rectangle r = new Rectangle(0, offset + mv, this.Width, blockSize);
SolidBrush blueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(blueBrush, r);
blueBrush.Dispose();
}
offset += stepSize;
ghp.Dispose();
this.pictureBox_main.Image = map;
}
#endregion
#region 从底部向上流动
///
/// 从底部向上流动
///
public void BottomGoToTop() {
Bitmap map = new Bitmap(this.pictureBox_main.Width, this.pictureBox_main.Height);
Graphics ghp = Graphics.FromImage(map);
ghp.DrawImage(map, 0, 0);
ghp.Clear(this.backColor);
int count = this.Height / (chinkSize + blockSize);
//余数
if ((this.Height % (chinkSize + blockSize)) > 0) {
count++;
}
//压入第一个
if (offset > 0) {
if ((offset - chinkSize) > 0) {
if (offset <= (chinkSize + blockSize)) {
Rectangle first = new Rectangle(0, (this.Height + chinkSize) - offset, this.Width, offset - chinkSize);
SolidBrush firstBlueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(firstBlueBrush, first);
firstBlueBrush.Dispose();
} else {
offset = stepSize;
}
}
}
for (int index = 0; index < count; index++) {
int mv = index * (chinkSize + blockSize);
int x = mv == 0 ? this.Height - blockSize : this.Height - mv - blockSize;
Rectangle r = new Rectangle(0, x - offset, this.Width, blockSize);
SolidBrush blueBrush = new SolidBrush(this.foreColor);
ghp.FillRectangle(blueBrush, r);
blueBrush.Dispose();
}
offset += stepSize;
ghp.Dispose();
this.pictureBox_main.Image = map;
}
#endregion
效果图:要定时更新才会走动!!!