使用了 SWFObject.js http://blog.deconcept.com/swfobject/
SWFObject.js 的功能很丰富,改写为控件可以更方便的调用。
样例下载
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Text;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Drawing;
using
System.Drawing.Design;
using
System.Web.UI.Design;
using
System.IO;
using
System.Collections;
using
System.ComponentModel.Design;
[assembly: TagPrefix(
"
Flash
"
,
"
Flash
"
)]
[assembly: WebResource(
"
Flash.Flash.js
"
,
"
application/x-javascript
"
)]
namespace
Flash
{
#region
参数项
///
<summary>
///
参数项。
///
</summary>
[DefaultProperty(
"
Name
"
)]
[ToolboxData(
"
<{0}:FlashItem />
"
)]
public
class
FlashItem : IStateManager
{
public
FlashItem()
:
this
(
""
,
""
)
{
}
///
<summary>
///
构造函数。
///
</summary>
///
<param name="name">
名称
</param>
///
<param name="value">
值
</param>
public
FlashItem(
string
name,
string
value)
{
this
.Name
=
name;
this
.Value
=
value;
}
///
<summary>
///
名称。
///
</summary>
[DefaultValue(
""
)]
[Description(
"
名称
"
)]
public
string
Name
{
get
{
return
ViewState[
"
Name
"
]
is
string
?
(
string
)ViewState[
"
Name
"
] :
string
.Empty; }
set
{ ViewState[
"
Name
"
]
=
value; }
}
///
<summary>
///
值。
///
</summary>
[DefaultValue(
""
)]
[Description(
"
值
"
)]
public
string
Value
{
get
{
return
ViewState[
"
Value
"
]
is
string
?
(
string
)ViewState[
"
Value
"
] :
string
.Empty; }
set
{ ViewState[
"
Value
"
]
=
value; }
}
public
override
string
ToString()
{
return
string
.IsNullOrEmpty(Name)
?
"
Item
"
: Name;
}
#region
视图
private
bool
marked;
private
StateBag viewState;
internal
void
LoadViewState(
object
state)
{
if
(state
!=
null
)
((IStateManager)
this
.ViewState).LoadViewState(state);
}
internal
object
SaveViewState()
{
if
(
this
.viewState
==
null
)
return
null
;
else
return
((IStateManager)
this
.viewState).SaveViewState();
}
internal
void
TrackViewState()
{
this
.marked
=
true
;
if
(
this
.viewState
!=
null
)
((IStateManager)
this
.viewState).TrackViewState();
}
internal
StateBag ViewState
{
get
{
if
(
this
.viewState
==
null
)
{
this
.viewState
=
new
StateBag();
if
(((IStateManager)
this
).IsTrackingViewState)
((IStateManager)
this
.viewState).TrackViewState();
}
return
this
.viewState;
}
}
internal
bool
IsTrackingViewState
{
get
{
return
this
.marked; }
}
internal
void
SetDirty()
{
this
.ViewState.SetDirty(
true
);
}
void
IStateManager.LoadViewState(
object
savedState)
{
this
.LoadViewState(savedState);
}
object
IStateManager.SaveViewState()
{
return
this
.SaveViewState();
}
void
IStateManager.TrackViewState()
{
this
.TrackViewState();
}
bool
IStateManager.IsTrackingViewState
{
get
{
return
this
.IsTrackingViewState; }
}
#endregion
}
#endregion
[DefaultProperty(
"
Src
"
)]
[Designer(
typeof
(Flash.Designer))]
[ToolboxData(
"
<{0}:Flash runat=\
"
server\
"
Width=\
"
125px\
"
Height=\
"
50px\
"
></{0}:Flash>
"
)]
[ParseChildren(
true
)]
public
class
Flash : WebControl
{
#region
参数集合
///
<summary>
///
参数集合。
///
</summary>
public
class
FlashItemCollection : StateManagedCollection
{
public
FlashItem
this
[
int
index]
{
get
{
return
(
this
[index]
as
FlashItem); }
set
{
this
[index]
=
value; }
}
public
int
Add(FlashItem value)
{
return
((IList)
this
).Add(value);
}
public
bool
Contains(FlashItem value)
{
return
((IList)
this
).Contains(value);
}
public
void
CopyTo(FlashItem[] array,
int
index)
{
base
.CopyTo(array, index);
}
public
int
IndexOf(FlashItem value)
{
return
((IList)
this
).IndexOf(value);
}
public
void
Remove(FlashItem value)
{
((IList)
this
).Remove(value);
}
public
void
RemoveAt(
int
index)
{
((IList)
this
).RemoveAt(index);
}
protected
override
void
SetDirtyObject(
object
o)
{
(o
as
FlashItem).SetDirty();
}
}
#endregion
#region
视图刷新
protected
override
void
LoadViewState(
object
savedState)
{
if
(savedState
!=
null
)
{
object
[] objArray
=
(
object
[])savedState;
base
.LoadViewState(objArray[
0
]);
((IStateManager)
this
.Variables).LoadViewState(objArray[
1
]);
((IStateManager)
this
.Params).LoadViewState(objArray[
2
]);
}
}
protected
override
object
SaveViewState()
{
return
new
object
[] {
base
.SaveViewState(), ((IStateManager)
this
.Variables).SaveViewState(), ((IStateManager)
this
.Params).SaveViewState() };
}
protected
override
void
TrackViewState()
{
base
.TrackViewState();
((IStateManager)
this
.Variables).TrackViewState();
((IStateManager)
this
.Params).TrackViewState();
}
#endregion
public
Flash()
:
base
(HtmlTextWriterTag.Div)
{
}
public
class
Designer : ControlDesigner
{
public
override
string
GetDesignTimeHtml()
{
Flash component
=
(Flash)
base
.Component;
if
(component.Width
==
Unit.Empty)
component.Style.Add(HtmlTextWriterStyle.Width,
"
100%
"
);
else
component.Style.Remove(HtmlTextWriterStyle.Width);
HtmlTextWriter writer
=
new
HtmlTextWriter(
new
StringWriter());
component.RenderControl(writer);
return
writer.InnerWriter.ToString();
}
}
///
<summary>
///
窗口模式。
///
</summary>
public
enum
WmodeType
{
///
<summary>
///
窗口
///
</summary>
Window,
///
<summary>
///
透明
///
</summary>
Transparent
}
///
<summary>
///
品质。
///
</summary>
public
enum
QualityType
{
///
<summary>
///
低
///
</summary>
Low,
///
<summary>
///
自动低
///
</summary>
Autolow,
///
<summary>
///
自动高
///
</summary>
Autohigh,
///
<summary>
///
高
///
</summary>
High
}
private
FlashItemCollection variables
=
null
;
///
<summary>
///
传递参数。
///
</summary>
[Description(
"
传递参数。
"
)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[MergableProperty(
false
)]
[Editor(
typeof
(CollectionEditor),
typeof
(UITypeEditor))]
[DefaultValue((
string
)
null
)]
public
FlashItemCollection Variables
{
get
{
if
(variables
==
null
)
{
variables
=
new
FlashItemCollection();
if
(
base
.IsTrackingViewState)
((IStateManager)(variables)).TrackViewState();
}
return
variables;
}
}
private
FlashItemCollection _params
=
null
;
///
<summary>
///
属性参数。
///
</summary>
[Description(
"
属性参数。
"
)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[MergableProperty(
false
)]
[Editor(
typeof
(CollectionEditor),
typeof
(UITypeEditor))]
[DefaultValue((
string
)
null
)]
public
FlashItemCollection Params
{
get
{
if
(_params
==
null
)
{
_params
=
new
FlashItemCollection();
if
(
base
.IsTrackingViewState)
((IStateManager)(_params)).TrackViewState();
}
return
_params;
}
}
///
<summary>
///
Flash 文件地址。
///
</summary>
[DefaultValue(
""
)]
[Category(
"
Flash
"
)]
[Description(
"
Flash 文件地址。
"
)]
[Editor(
"
System.Web.UI.Design.UrlEditor, System.Design
"
,
typeof
(UITypeEditor))]
[UrlProperty]
public
string
Src
{
get
{
return
ViewState[
"
Src
"
]
is
string
?
(
string
)ViewState[
"
Src
"
] :
string
.Empty; }
set
{ ViewState[
"
Src
"
]
=
value; }
}
///
<summary>
///
Flash 版本号。
///
</summary>
[DefaultValue(
8
)]
[Category(
"
Flash
"
)]
[Description(
"
Flash 版本号。
"
)]
public
int
Version
{
get
{
return
ViewState[
"
Version
"
]
is
int
?
(
int
)ViewState[
"
Version
"
] :
8
; }
set
{ ViewState[
"
Version
"
]
=
value; }
}
///
<summary>
///
显示菜单。
///
</summary>
[DefaultValue(
true
)]
[Category(
"
Flash
"
)]
[Description(
"
显示菜单。
"
)]
public
bool
Menu
{
get
{
return
ViewState[
"
Menu
"
]
is
bool
?
(
bool
)ViewState[
"
Menu
"
] :
true
; }
set
{ ViewState[
"
Menu
"
]
=
value; }
}
///
<summary>
///
窗口模式。
///
</summary>
[DefaultValue(WmodeType.Window)]
[Category(
"
Flash
"
)]
[Description(
"
窗口模式。
"
)]
public
WmodeType Wmode
{
get
{
return
ViewState[
"
Wmode
"
]
is
WmodeType
?
(WmodeType)ViewState[
"
Wmode
"
] : WmodeType.Window; }
set
{ ViewState[
"
Wmode
"
]
=
value; }
}
///
<summary>
///
品质。
///
</summary>
[DefaultValue(QualityType.High)]
[Category(
"
Flash
"
)]
[Description(
"
品质。
"
)]
public
QualityType Quality
{
get
{
return
ViewState[
"
Quality
"
]
is
QualityType
?
(QualityType)ViewState[
"
Quality
"
] : QualityType.High; }
set
{ ViewState[
"
Quality
"
]
=
value; }
}
protected
override
void
OnPreRender(EventArgs e)
{
base
.OnPreRender(e);
Page.ClientScript.RegisterClientScriptResource(
typeof
(Flash),
"
Flash.Flash.js
"
);
}
///
<summary>
///
Script 字符编码。
///
</summary>
///
<param name="s">
要编码的字符
</param>
///
<returns>
返回已编码的字符。
</returns>
private
string
ScriptEncode(
string
s)
{
if
(
string
.IsNullOrEmpty(s))
return
s;
else
return
s.Replace(
"
\\
"
,
"
\\\\
"
).Replace(
"
\r
"
,
"
\\r
"
).Replace(
"
\n
"
,
"
\\n
"
).Replace(
"
\b
"
,
"
\\b
"
).Replace(
"
\f
"
,
"
\\f
"
).Replace(
"
\t
"
,
"
\\t
"
).Replace(
"
/
"
,
"
\\/
"
).Replace(
"
'
"
,
"
\\'
"
).Replace(
"
\
""
,
"
\\\
""
);
}
public
override
void
RenderEndTag(HtmlTextWriter writer)
{
base
.RenderEndTag(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Type,
"
text/javascript
"
);
writer.RenderBeginTag(HtmlTextWriterTag.Script);
writer.Write(
"
var {0}_SWFObject = new SWFObject('{1}', '{0}_SWF', '{2}', '{3}', '{4}', '{5}');\n
"
, ClientID, ScriptEncode(Page.ResolveUrl(Src)), Width, Height, Version, ColorTranslator.ToHtml(BackColor));
writer.Write(
"
{0}_SWFObject.addParam('wmode', '{1}');\n
"
, ClientID, Wmode);
writer.Write(
"
{0}_SWFObject.addParam('menu', '{1}');\n
"
, ClientID, Menu);
writer.Write(
"
{0}_SWFObject.addParam('quality', '{1}');\n
"
, ClientID, Quality);
//
遍历属性参数
foreach
(FlashItem i
in
Params)
{
writer.Write(
"
{0}_SWFObject.addParam('{1}', '{2}');\n
"
, ClientID, ScriptEncode(i.Name), ScriptEncode(i.Value));
}
//
遍历传递参数
foreach
(FlashItem i
in
Variables)
{
writer.Write(
"
{0}_SWFObject.addVariable('{1}', '{2}');\n
"
, ClientID, ScriptEncode(i.Name), ScriptEncode(i.Value));
}
writer.Write(
"
{0}_SWFObject.write('{0}');
"
, ClientID);
writer.RenderEndTag();
}
}
}