faib的技术专栏
每天进步1%
http://faib.cnblogs.com
posts - 50, comments - 140, trackbacks - 8, articles - 0
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
ValidateBox验证码控件原代码
Posted on 2007-05-11 16:51
faib
阅读(665)
评论(1)
编辑
收藏
网摘
所属分类:
C#
、
Asp.Net
、
控件
今天想了想,还是把这个源代码发布出来。
//
********************************************************
//
验证码验证控件
//
Designed by Faib Studio.
//
Copyright 2007
//
Email faib920@126.com or QQ 55570729
//
********************************************************
using
System;
using
System.Collections.Specialized;
using
System.ComponentModel;
using
System.ComponentModel.Design;
using
System.Drawing;
using
System.Drawing.Drawing2D;
using
System.Drawing.Imaging;
using
System.Drawing.Design;
using
System.IO;
using
System.Text;
using
System.Reflection;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Globalization;
namespace
FaibClass.WebControls
{
[DefaultProperty(
"
Text
"
)]
[ToolboxData(
"
<{0}:ValidateBox Runat=Server Font-Name=\
"
System\
"
Font-Size=\
"
9pt\
"
></{0}:ValidateBox>
"
)]
public
class
ValidateBox : System.Web.UI.WebControls.Image
{
私有变量
#region
私有变量
private
int
m_Length
=
4
;
private
string
m_newImage
=
""
;
private
string
m_ValidateKey
=
""
;
private
float
m_TextAngle
=
0
;
private
bool
m_IsLaod
=
false
;
private
FontInfo m_Font;
private
ValidateStyle m_ReturnStyle
=
ValidateStyle.Client;
private
ValidateCodeType m_ValidateCodeType
=
ValidateCodeType.Commix;
private
string
m_CustomCodes;
#endregion
public
ValidateBox()
{
m_Font
=
new
Style().Font;
m_Font.Name
=
"
System
"
;
m_Font.Size
=
9
;
}
属性
#region
属性
/**/
///
<summary>
///
返回验证码。
///
</summary>
[Browsable(
false
)]
public
string
Text
{
get
{
if
(ViewState[
"
Text
"
]
==
null
)
return
""
;
else
return
ViewState[
"
Text
"
].ToString();
}
}
/**/
///
<summary>
///
验证码验证标识。
///
</summary>
[Category(
"
Behavior
"
)]
[Description(
"
验证码验证标识。
"
)]
public
string
ValidateKey
{
get
{
return
m_ValidateKey;}
set
{m_ValidateKey
=
value;}
}
/**/
///
<summary>
///
验证码产生类型。
///
</summary>
[Category(
"
Behavior
"
)]
[Description(
"
验证码产生类型。
"
)]
[DefaultValue(ValidateCodeType.Commix)]
public
ValidateCodeType ValidateType
{
get
{
return
m_ValidateCodeType;}
set
{m_ValidateCodeType
=
value;}
}
/**/
///
<summary>
///
验证码验证验证方式。
///
</summary>
[Category(
"
Behavior
"
)]
[Description(
"
验证码验证验证方式。
"
)]
[DefaultValue(ValidateStyle.Client)]
public
ValidateStyle ValidateStyle
{
get
{
return
m_ReturnStyle;}
set
{m_ReturnStyle
=
value;}
}
/**/
///
<summary>
///
设置或返回验证码字符的长度。
///
</summary>
[Category(
"
Appearance
"
)]
[Description(
"
设置或返回验证码字符的长度。
"
)]
[DefaultValue(
4
)]
public
int
Length
{
get
{
return
m_Length;}
set
{m_Length
=
value;}
}
/**/
///
<summary>
///
设置或返回字体。
///
</summary>
[Browsable(
true
)]
[Category(
"
Appearance
"
)]
[Description(
"
设置或返回字体。
"
)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public
new
FontInfo Font
{
get
{
return
m_Font;}
set
{m_Font
=
value;}
}
/**/
///
<summary>
///
设置或返回字体倾斜的角度。
///
</summary>
[Category(
"
Appearance
"
)]
[Description(
"
字体倾斜的角度。
"
)]
[DefaultValue(
typeof
(
float
),
"
0
"
)]
public
float
TextAngle
{
get
{
return
m_TextAngle;}
set
{m_TextAngle
=
value;}
}
/**/
///
<summary>
///
设置或返回字体倾斜的角度。
///
</summary>
[Category(
"
Appearance
"
)]
[Description(
"
自定义随机抽取的验证码列表。
"
)]
public
string
CustomCodes
{
get
{
return
m_CustomCodes;}
set
{m_CustomCodes
=
value;}
}
public
override
string
ImageUrl
{
get
{
return
base
.ImageUrl;}
set
{
base
.ImageUrl
=
value;
if
(
!
m_IsLaod)
{
ViewState[
"
OldImage
"
]
=
value;
}
}
}
#endregion
公共方法
#region
公共方法
protected
override
void
OnUnload(EventArgs e)
{
if
(m_ValidateKey
!=
null
&&
m_ValidateKey
!=
""
)
{
switch
(m_ReturnStyle)
{
case
ValidateStyle.Session:
Context.Session[m_ValidateKey]
=
ViewState[
"
Text
"
].ToString();
break
;
case
ValidateStyle.Cookie:
Context.Response.Cookies[m_ValidateKey].Value
=
ViewState[
"
Text
"
].ToString();
break
;
case
ValidateStyle.Cache:
Context.Cache[m_ValidateKey]
=
ViewState[
"
Text
"
].ToString();
break
;
}
}
base
.OnUnload (e);
}
protected
override
void
OnLoad(EventArgs e)
{
string
strCode
=
GetValidateCode();
ViewState[
"
Text
"
]
=
strCode;
BuildImage();
if
(m_ReturnStyle
==
ValidateStyle.Client)
{
this
.Attributes.Add(
"
Text
"
,
this
.Text);
this
.Attributes.Add(
"
value
"
,
this
.Text.ToLower());
}
this
.Style.Add(
"
cursor
"
,
"
hand
"
);
this
.Attributes.Add(
"
onclick
"
, Page.GetPostBackEventReference(
this
,
""
));
base
.OnLoad(e);
}
#endregion
私有方法
#region
私有方法
private
void
BuildImage()
{
bool
blnHasPic;
if
(ViewState[
"
OldImage
"
]
!=
null
)
{
blnHasPic
=
System.IO.File.Exists(Page.Server.MapPath(ViewState[
"
OldImage
"
].ToString()));
}
else
{
blnHasPic
=
false
;
}
m_newImage
=
this
.ClientID
+
"
.fbs.ashx
"
;
if
(
!
blnHasPic
&&
this
.Width.IsEmpty)
{
this
.Width
=
Unit.Pixel(
70
);
}
if
(
!
blnHasPic
&&
this
.Height.IsEmpty)
{
this
.Height
=
Unit.Pixel(
20
);
}
string
strCode
=
ViewState[
"
Text
"
].ToString();
string
strFName
=
"
System
"
;
float
floFSize
=
9
;
FontStyle fntstyFont
=
FontStyle.Regular;
if
(m_Font
!=
null
)
{
if
(m_Font.Name
!=
string
.Empty)
{
strFName
=
m_Font.Name;
}
//
如果使用是的是FontSize.Large等相对值应该怎么做
if
(m_Font.Size.Type
!=
FontSize.NotSet)
{
floFSize
=
(
float
)m_Font.Size.Unit.Value;
}
else
{
floFSize
=
9
;
}
if
(m_Font.Bold)
{
fntstyFont
|=
FontStyle.Bold;
}
if
(m_Font.Italic)