抽奖系统简单demo
之前有博友问过这个怎么做,恰巧一个同事也问。顺带做个demo
思路:
重写onpaint方法将数字(因为是画的,所以数字可以使用一些漂亮的图片)画到背景透明的控件上面。
之前有博友问过这个怎么做,恰巧一个同事也问。顺带做个demo
思路:
重写onpaint方法将数字(因为是画的,所以数字可以使用一些漂亮的图片)画到背景透明的控件上面。
具体看代码。
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace TicketsBonus
{
public class TicketsBonusLabel : Control, IDisposable
{
public TicketsBonusLabel() {
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.UpdateStyles();
srcMap = new Dictionary<int, Bitmap>();
InitializeBackground();
}
private Dictionary<int, Bitmap> srcMap;
[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Dictionary<int, Bitmap> SrcMap
{
get { return srcMap; }
set { srcMap = value; }
}
void InitializeBackground() {
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "number");
for (int i = 0; i < 10; i++)
{
string file = Path.Combine(path, i.ToString() + ".png");
if (File.Exists(file)) {
Bitmap map = Bitmap.FromFile(file) as Bitmap;
if (map != null)
{
this.srcMap.Add(i, map);
}
}
}
}
private long _value = 13588886666;
[DefaultValue(13588886666)]
public long Value {
get {
return _value;
}
set {
if (value < 0) {
throw new Exception();
}
_value = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (this.BackColor != Color.Transparent) {
this.BackColor = Color.Transparent;
}
List<int> value = new List<int>();
foreach (char c in this.Value.ToString().PadLeft(11,'0'))
{
int v = -1;
int.TryParse(c.ToString(), out v);
value.Add(v);
}
int x, y = 0;
int i = 0;
foreach (int item in value)
{
x = 64 * i;
if (srcMap.ContainsKey(item))
{
Bitmap map = srcMap[item];
e.Graphics.DrawImage(map, new Rectangle(x, 0, 64, 64));
}
else {
e.Graphics.DrawString(item.ToString(), this.Font, Brushes.Black, new Point(x, 0));
}
i++;
}
}
public new void Dispose() {
foreach (Bitmap m in srcMap.Values)
{
m.Dispose();
}
srcMap.Clear();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace TicketsBonus
{
public class TicketsBonusLabel : Control, IDisposable
{
public TicketsBonusLabel() {
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.UpdateStyles();
srcMap = new Dictionary<int, Bitmap>();
InitializeBackground();
}
private Dictionary<int, Bitmap> srcMap;
[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Dictionary<int, Bitmap> SrcMap
{
get { return srcMap; }
set { srcMap = value; }
}
void InitializeBackground() {
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "number");
for (int i = 0; i < 10; i++)
{
string file = Path.Combine(path, i.ToString() + ".png");
if (File.Exists(file)) {
Bitmap map = Bitmap.FromFile(file) as Bitmap;
if (map != null)
{
this.srcMap.Add(i, map);
}
}
}
}
private long _value = 13588886666;
[DefaultValue(13588886666)]
public long Value {
get {
return _value;
}
set {
if (value < 0) {
throw new Exception();
}
_value = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (this.BackColor != Color.Transparent) {
this.BackColor = Color.Transparent;
}
List<int> value = new List<int>();
foreach (char c in this.Value.ToString().PadLeft(11,'0'))
{
int v = -1;
int.TryParse(c.ToString(), out v);
value.Add(v);
}
int x, y = 0;
int i = 0;
foreach (int item in value)
{
x = 64 * i;
if (srcMap.ContainsKey(item))
{
Bitmap map = srcMap[item];
e.Graphics.DrawImage(map, new Rectangle(x, 0, 64, 64));
}
else {
e.Graphics.DrawString(item.ToString(), this.Font, Brushes.Black, new Point(x, 0));
}
i++;
}
}
public new void Dispose() {
foreach (Bitmap m in srcMap.Values)
{
m.Dispose();
}
srcMap.Clear();
}
}
}
demo代码:
https://files.cnblogs.com/5yplan/TicketsBonus.rar
-----------
欢迎转载,但请注明出处!!!