您的位置:首页 > 编程语言 > C#

C#组件开发——简单计数器

2006-04-25 16:56 531 查看
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.OleDb;

namespace Web.UI.WebControls
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCounter runat=server></{0}:WebCounter>")]
public class WebCounter : System.Web.UI.WebControls.WebControl
{
private int nCounter = 1; //计数器当前值 初始化 0
private int nWidth = 8; //计数器位数 初始化 6
private int nStep = 1; //计数器每次增加的步长 初始化 1
// private int nType = 1; //计数器类型(图片类型序号) 初始化 0

public enum enumCounterType
{
亮绿阿拉伯数字黑底=0,
古铜阿拉伯数字黑底,
白色粗体阿拉伯数字黑底,
暗绿带箭头阿拉伯数字黑底,
黑色毛边阿拉伯数字白底,
暗绿点状阿拉伯数字黑底,
黑色汉字白底,
红色繁体汉字白底,
灯笼繁体汉字,
彩色阿拉伯数字彩底,
亮绿点状阿拉伯数字墨绿底,
白色阿拉伯数字球状,
白色简体汉字黑底,
白色红边阿拉伯数字黑底,
下划线白色阿拉伯数字古印底,
亮绿加边阿拉伯数字黑底,
大白阿拉伯数字黑底,
暗白阿拉伯数字黑底,
黑色阿拉伯数字彩底,
间色阿拉伯数字彩底,
间色阿拉伯数字白底,
毛边白色阿拉伯数字黑底1,
毛边白色阿拉伯数字黑底2,
毛边白色阿拉伯数字黑底3,
黑色阿拉伯数字淡黄底,
细白色阿拉伯数字黑底,
间隔黑色阿拉伯数字灰底,
红色阿拉伯数字黑底,
微斜白色阿拉伯数字黑底,
正白色阿拉伯数字彩底,
正楷白色阿拉伯数字黑底,
小手写白色阿拉伯数字黑底,
粗红长阿拉伯数字间灰底,
阴影粗红阿拉伯数字白底,
正白间隔阿拉伯数字黑底1,
正白间隔阿拉伯数字黑底2,
间色阿拉伯数字间色底,
小间色阿拉伯数字黑底,
};

private enumCounterType nType = enumCounterType.亮绿阿拉伯数字黑底;

private string strCountDirectory = "/images/counter/"; //计数器图片所在位置
private string strFrontWord = "您是第"; //计数器前导字符 初始化 “您是第”
private string strBackWord = "位访客!"; //计数器后导字符 初始化 “位访客!”

public string GetCounterPro() //从文件读取属性值
{
string filePath = Page.Server.MapPath(".") + "//counter.txt";
string r1 = "1";
if(!File.Exists(filePath)) //如果记数器文件不存在,就建立它
{
StreamWriter rw = File.CreateText(filePath);
rw.WriteLine("1");
rw.Flush();
rw.Close();
r1="1";
}
else //如果记数器文件存在,就打开并且读取
{
StreamReader sr = File.OpenText(filePath);

r1 = sr.ReadLine(); //读取
sr.Close();

if(r1==null)
{
r1 = "1";
}
}
return r1;
}

public void PutCounterPro(string strValue ) //设置属性值并写进文件
{
string filePath = Page.Server.MapPath(".") + "//counter.txt";
StreamWriter rw = File.CreateText(filePath); //写入
rw.WriteLine( strValue );
rw.Flush();
rw.Close();
}

[Bindable(true),
Category("计数器属性"),
DefaultValue(1)]
public int 记数器初始值 //得到记数器当前值
{
get
{
//nCounter = Convert.ToInt32(GetCounterPro("nCounter"));
//nCounter = 100;
return nCounter;
}
set
{
nCounter = value;
//PutCounterPro( "nCounter" , nCounter.ToString() );
}
}

[Bindable(true),
Category("计数器属性"),
DefaultValue(8)]
public int 记数器位数
{
get
{
return nWidth;
}
set
{
nWidth = value;
}
}
public void 记数器重置() //记数器重置
{
nCounter = 0;
}

[Bindable(true),
Category("计数器属性"),
DefaultValue(1)]
public int 记数器步长 //设置记数器的步长
{
get
{
return nStep;
}
set
{
nStep = value;
}
}

[Bindable(true),
Category("计数器属性"),
DefaultValue(enumCounterType.亮绿阿拉伯数字黑底)]
public enumCounterType 记数器类型 //设置记数器的类型
{
get
{
return nType;
}
set
{
nType = value;
}
}

[Bindable(true),
Category("计数器属性"),
DefaultValue("您是第")]
public string 记数器前导词 //设置记数器的前导词
{
get
{
return strFrontWord;
}
set
{
strFrontWord = value;
}
}

[Bindable(true),
Category("计数器属性"),
DefaultValue("位访客!")]
public string 记数器后导词 //设置记数器的后导词
{
get
{
return strBackWord;
}
set
{
strBackWord = value;
}
}

/// <summary>
/// true if currently in design mode.
/// </summary>
//判断是否是设计模式
protected bool IsDesignMode
{
get
{
return (Site != null) ? Site.DesignMode : false;
}
}
//Color转变为#FFFFFF格式
internal static string ColorToHexString(Color c)
{
string r = Convert.ToString(c.R, 16);
if (r.Length < 2)
r = "0" + r;
string g = Convert.ToString(c.G, 16);
if (g.Length < 2)
g = "0" + g;
string b = Convert.ToString(c.B, 16);
if (b.Length < 2)
b = "0" + b;

string str = "#" + r + g + b;
return str;
}

//得到Font的集合,内联style
protected string GetFontCss()
{
string strValue = "";
if(Font.Name==null || Font.Name == "")
{
strValue += "FONT-family:宋体;";
}
else
{
strValue += "FONT-family:" + Font.Name + ";";
}

if(Font.Size== 0)
{
strValue += "FONT-SIZE:9pt;";
}
else
{
strValue += "FONT-SIZE:" + Font.Size + ";";
}

strValue += "COLOR:" + ColorToHexString(this.ForeColor) + ";";

strValue += "TEXT-DECORATION:";
if (Font.Underline)
{
strValue += "underline ";
}
else
{
strValue += "none ";
}
if (Font.Overline)
{
strValue += "Overline ";
}

if (Font.Strikeout)
{
strValue += "line-through ";
}
strValue += ";";

if (Font.Bold)
{
strValue += "Font-Weight:bold;";
}

if (Font.Italic)
{
strValue += "Font-Style:italic;";
}

return strValue;
}

//得到边框及背景的样式
protected string GetBoxCss()
{
string strValue = "";
//背景颜色
if( !this.BackColor.IsEmpty )
{
strValue += "background-color:" + ColorToHexString( this.BackColor ) + ";";
}

strValue += "border:";
//边框宽度
if( !this.BorderWidth.IsEmpty )
{
strValue += " " + this.BorderWidth.Value + "px";
}
else
{
strValue += " 0px";
}
//边框样式
strValue += " " + this.BorderStyle.ToString();
//边框颜色
if( !this.BorderColor.IsEmpty )
{
strValue += " " + ColorToHexString( this.BorderColor ) + ";";
}
else
{
strValue += " #white;";
}
//高度
if( !this.Height.IsEmpty )
{
strValue += "Height:" + this.Height.ToString() + ";";
}
//宽度
if( !this.Width.IsEmpty )
{
strValue += "Width:" + this.Width.ToString() + ";";
}

return strValue;
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{

if(!IsDesignMode) //运行模式
{
// startCounter();
int nAddCounter = Convert.ToInt32(GetCounterPro());
nAddCounter += nStep;
PutCounterPro( nAddCounter.ToString() );
nCounter += nAddCounter;

}

string strCounter;
strCounter = Convert.ToString(nCounter);

int nCounterLen;
nCounterLen = strCounter.Length;

if(nCounterLen < nWidth)
{
for(int iPreZeroNumber = 1;iPreZeroNumber <= nWidth - nCounterLen;iPreZeroNumber++)
{
strCounter = "0" + strCounter;
}
}

output.Write("<span style=/"" + GetFontCss() + GetBoxCss() + "/">" + strFrontWord);
// output.Write("<table style=/"" + GetFontCss() + GetBoxCss() + "/"><tr><td width=/"100%/" height=/"100%/" align=center valign=middle>" + strFrontWord);

int k = (int)nType;
string strImageDir = strCountDirectory + k.ToString() + "/"; //小图片的路径

for(int iCounter = 0;iCounter < nWidth;iCounter++)
{
string strEveryCounter = strCounter.Substring(iCounter,1);

output.Write("<img src='" + strImageDir + strEveryCounter + ".gif' align='absbottom'>");
}

output.Write(strBackWord + "</span>");
// output.Write(strBackWord + "</td></tr></table>");

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: