昨天晚上看到
http://www.cnblogs.com/overred/archive/2006/03/24/357833.html的效果,感觉挺好看的.
我结合枚举做了一下.可以显示系统的全部颜色.
看看代码
我结合枚举做了一下.可以显示系统的全部颜色.
看看代码
1
<%
@ Page Language
=
"
C#
"
UICulture
=
"
zh-CHS
"
Culture
=
"
zh-CN
"
%>
2
3
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
4
5
<
script runat
=
"
server
"
>
6
protected
void
Page_Load(
object
sender, EventArgs e)
7
{
8
if (!IsPostBack)
9
{
10
BindText();
11
}
12
}
13
void
BindText()
14
{
15
//绑定颜色
16
string[] colorArray = Enum.GetNames(typeof(System.Drawing.KnownColor));
17
18
foreach(string color in colorArray)
19
{
20
ListItem item = new ListItem(color);
21
item.Attributes.Add("style", "color:" + color);
22
23
txt_color.Items.Add(item);
24
}
25
//绑定字体
26
System.Drawing.Text.InstalledFontCollection font;
27
font = new System.Drawing.Text.InstalledFontCollection();
28
foreach (System.Drawing.FontFamily family in font.Families)
29
{
30
txt_Font.Items.Add(family.Name);
31
}
32
//字体大小
33
string[] sizeArray = Enum.GetNames(typeof(System.Web.UI.WebControls.FontSize));
34
35
listsize.DataSource = sizeArray;
36
listsize.SelectedIndex = -1;
37
listsize.DataBind();
38
}
39
protected
void
Button1_Click(
object
sender, EventArgs e)
40
{
41
show.Text = txt.Text;
42
show.ForeColor = System.Drawing.Color.FromName(txt_color.SelectedItem.Text);
43
show.Font.Name = txt_Font.SelectedItem.Text;
44
if (listsize.SelectedIndex>0)
45
{
46
show.Font.Size = FontUnit.Parse(listsize.SelectedItem.Text);
47
}
48
else
49
{
50
show.Font.Size = FontUnit.Point(Int32.Parse(size.Text));
51
}
52
}
53
54
55
</
script
>
56
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
57
<
head runat
=
"
server
"
>
58
<
title
>
无标题页
</
title
>
59
</
head
>
60
<
body
>
61
<
form id
=
"
form1
"
runat
=
"
server
"
>
62
<
div
>
63
选择字体颜色:
<
asp:DropDownList ID
=
"
txt_color
"
runat
=
"
server
"
>
64
</
asp:DropDownList
><
br
/>
65
<
br
/>
66
选择系统字体:
<
asp:DropDownList ID
=
"
txt_Font
"
runat
=
"
server
"
>
67
</
asp:DropDownList
><
br
/>
68
<
br
/>
69
选择字体大小:
<
asp:TextBox ID
=
"
size
"
runat
=
"
server
"
></
asp:TextBox
>
70
&
nbsp;
&
nbsp;
<
asp:RadioButtonList ID
=
"
listsize
"
runat
=
"
server
"
RepeatColumns
=
"
3
"
RepeatDirection
=
"
Horizontal
"
>
71
</
asp:RadioButtonList
>
72
&
nbsp;
&
nbsp;
73
<
br
/>
74
<
br
/>
75
请输入文字:
76
<
asp:TextBox ID
=
"
txt
"
runat
=
"
server
"
></
asp:TextBox
><
br
/>
77
<
br
/>
78
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
OnClick
=
"
Button1_Click
"
Text
=
"
确定
"
/><
br
/>
79
<
br
/>
80
<
asp:Label ID
=
"
show
"
runat
=
"
server
"
></
asp:Label
></
div
>
81
</
form
>
82
</
body
>
83
</
html
>
84
<%
@ Page Language
=
"
C#
"
UICulture
=
"
zh-CHS
"
Culture
=
"
zh-CN
"
%>
2

3
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
4

5
<
script runat
=
"
server
"
>
6
protected
void
Page_Load(
object
sender, EventArgs e)7

{8
if (!IsPostBack)9

{10
BindText();11
}12
}
13
void
BindText()14

{15
//绑定颜色16
string[] colorArray = Enum.GetNames(typeof(System.Drawing.KnownColor));17
18
foreach(string color in colorArray)19

{20
ListItem item = new ListItem(color);21
item.Attributes.Add("style", "color:" + color);22
23
txt_color.Items.Add(item);24
}25
//绑定字体26
System.Drawing.Text.InstalledFontCollection font;27
font = new System.Drawing.Text.InstalledFontCollection();28
foreach (System.Drawing.FontFamily family in font.Families)29

{30
txt_Font.Items.Add(family.Name);31
}32
//字体大小33
string[] sizeArray = Enum.GetNames(typeof(System.Web.UI.WebControls.FontSize));34
35
listsize.DataSource = sizeArray;36
listsize.SelectedIndex = -1;37
listsize.DataBind();38
}
39
protected
void
Button1_Click(
object
sender, EventArgs e)40

{41
show.Text = txt.Text;42
show.ForeColor = System.Drawing.Color.FromName(txt_color.SelectedItem.Text);43
show.Font.Name = txt_Font.SelectedItem.Text;44
if (listsize.SelectedIndex>0)45

{46
show.Font.Size = FontUnit.Parse(listsize.SelectedItem.Text);47
}48
else49

{50
show.Font.Size = FontUnit.Point(Int32.Parse(size.Text));51
}52
}
53
54
55
</
script
>
56
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
57
<
head runat
=
"
server
"
>
58
<
title
>
无标题页
</
title
>
59
</
head
>
60
<
body
>
61
<
form id
=
"
form1
"
runat
=
"
server
"
>
62
<
div
>
63
选择字体颜色:
<
asp:DropDownList ID
=
"
txt_color
"
runat
=
"
server
"
>
64
</
asp:DropDownList
><
br
/>
65
<
br
/>
66
选择系统字体:
<
asp:DropDownList ID
=
"
txt_Font
"
runat
=
"
server
"
>
67
</
asp:DropDownList
><
br
/>
68
<
br
/>
69
选择字体大小:
<
asp:TextBox ID
=
"
size
"
runat
=
"
server
"
></
asp:TextBox
>
70
&
nbsp;
&
nbsp;
<
asp:RadioButtonList ID
=
"
listsize
"
runat
=
"
server
"
RepeatColumns
=
"
3
"
RepeatDirection
=
"
Horizontal
"
>
71
</
asp:RadioButtonList
>
72
&
nbsp;
&
nbsp;73
<
br
/>
74
<
br
/>
75
请输入文字:76
<
asp:TextBox ID
=
"
txt
"
runat
=
"
server
"
></
asp:TextBox
><
br
/>
77
<
br
/>
78
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
OnClick
=
"
Button1_Click
"
Text
=
"
确定
"
/><
br
/>
79
<
br
/>
80
<
asp:Label ID
=
"
show
"
runat
=
"
server
"
></
asp:Label
></
div
>
81
</
form
>
82
</
body
>
83
</
html
>
84
本文介绍了一个使用 ASP.NET 的示例,展示了如何在网页上枚举并显示系统的所有颜色选项。通过 C# 代码结合 ASP.NET 控件,实现了字体颜色、字体类型及大小的选择功能。
2390

被折叠的 条评论
为什么被折叠?



