Flash控件

时间:2024-03-04 21:29:06

Flash控件开发

    在做基于asp.net的软件时,似乎Flash不是很常用。但是在电子商务网站中就比较常用了,很多广告或者产品的图片浏览是用Flash的。
    很幸运的是,插入一段Flash的代码并不是很难,但是在习惯于用控件来开发的我们是否会重新接受那种如同asp一样让人眼花的代码。所以,开发一个Flash控件似乎还是比较必要的。当然,我只是简单的开发了一个能用的Flash,更好的功能还要大家去扩展了。好了,闲话不
说了,直接进入正题。和其他的asp.net控件一样,Flash控件将继承自WebControl。然后我们只需重写RenderContents方法就可以了。

 [Designer("Lin.Controls.FlashDesign,Lin.Controls")]
    [ToolboxData(
"<{0}:Flash runat=server></{0}:Flash>")]
    [
System.Drawing.ToolboxBitmap(typeof(Flash), "Flash.ico")]
    public class Flash 
: WebControl
    {
        private string imageUrl;

        [UrlProperty
, Bindable(true), Category("Behavior")]
        [DescriptionAttribute(
"链接地址")]
        [DefaultValue(null)
, Editor("System.Web.UI.Design.XmlUrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        public string ImageUrl
        {
            get
            {
                
return (imageUrl);
            }
            set
            {
                imageUrl 
= value;
            }
        }

        private void RegisterScript()
        {
            
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("Lin.Controls.myLable"))
            {
                this
.Page.ClientScript.RegisterClientScriptInclude
                (
                    
"Lin.Controls.myLable",
                    this
.Page.ClientScript.GetWebResourceUrl
                    (
                        this
.GetType(), "Lin.Controls.myLable.js"
                    )
                );
            }
            string str 
= string.Format("Rewrite(\'{0}\');", this.ClientID);
            
if (!this.Page.ClientScript.IsStartupScriptRegistered("flash" + this.ClientID))
                this
.Page.ClientScript.RegisterStartupScript(this.GetType(), "flash" + this.ClientID, str, true);
        }
        protected override void OnPreRender(EventArgs e)
        {
            base
.OnPreRender(e);
            this
.RegisterScript();
        }

        private string GetUrl(string virtualUrl)
        {
            string applicationPath 
= (HttpRuntime.AppDomainAppVirtualPath.Length > 1? HttpRuntime.AppDomainAppVirtualPath : String.Empty;
            
if (!string.IsNullOrEmpty(virtualUrl))
            {
                
if (virtualUrl.StartsWith("~/"))
                    
return applicationPath + virtualUrl.Substring(1);
                
else if (virtualUrl.StartsWith("http://"))
                    
return virtualUrl;
            }
            
return virtualUrl;
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                
return HtmlTextWriterTag.Div;
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            
if (this.Height.IsEmpty)
                this
.Height = new Unit(100, UnitType.Percentage);
            
if (this.Width.IsEmpty)
                this
.Width = new Unit(100, UnitType.Percentage);

            base
.RenderContents(writer);
            writer
.AddAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
            writer
.AddAttribute("codebase", "http://linjianyu.cnblogs.com");
            writer
.AddAttribute(HtmlTextWriterAttribute.Width, this.Width.ToString());
            writer
.AddAttribute(HtmlTextWriterAttribute.Height, this.Height.ToString());
            writer
.RenderBeginTag(HtmlTextWriterTag.Object);

            writer
.AddAttribute(HtmlTextWriterAttribute.Name, "movie");
            writer
.AddAttribute(HtmlTextWriterAttribute.Value, this.GetUrl(this.ImageUrl));
            writer
.RenderBeginTag(HtmlTextWriterTag.Param);
            writer
.RenderEndTag();

            writer
.AddAttribute(HtmlTextWriterAttribute.Name, "quality");
            writer
.AddAttribute(HtmlTextWriterAttribute.Value, "high");
            writer
.RenderBeginTag(HtmlTextWriterTag.Param);
            writer
.RenderEndTag();

            writer
.AddAttribute(HtmlTextWriterAttribute.Src, this.GetUrl(this.ImageUrl));
            writer
.AddAttribute("quality", "high");
            writer
.AddAttribute(HtmlTextWriterAttribute.Type, "application/x-shockwave-flash");
            writer
.AddAttribute(HtmlTextWriterAttribute.Width, this.Width.ToString());
            writer
.AddAttribute(HtmlTextWriterAttribute.Height, this.Height.ToString());
            writer
.RenderBeginTag(HtmlTextWriterTag.Embed);
            writer
.RenderEndTag();

            writer
.RenderEndTag();
        }

    }


 

 

然后我们为这个控件做一个设计时支持FlashDesign。FlashDesign继承自System.Web.UI.Design.ControlDesigner
支持asp.net设计时控件都应继承自这个类。然后我们override方法GetDesignTimeHtml就可以了。