类似msn的提示效果代码系列四:利用反射和嵌入JavaScript资源封装消息提示的C#类

在本系列文章的第三篇中,我们已经基本实现了消息提示的要求,也有了一定的灵活性,但是在这个消息提示应用的开发过程中,我的追求是最好能封装出一个C#的类来,这样,其他开发人员如果要实现类似的要求,就可以直接引用我编写的dll文件,然后实例化相应的类,调用相应的方法即可,而不必去复制粘贴那些复杂的JavaScript脚本,也不必去关心它是如何实现的,就像没有利用JavaScript一样。有了这个想法,自然就去寻找解决的办法。后来,我利用嵌入资源和反射技术相结合,解决了这个问题。解决的方法如下:

一、建立一个类库项目,在其中添加一个JavaScript文件(Notice.js),在文件中输入如下代码:

< SCRIPT language = JavaScript >   
/**/ /*  
**    ==================================================================================================  
**    类名:CLASS_MSN_MESSAGE  
**    功能:提供类似MSN消息框     
**    ==================================================================================================  
*
*/
  
  
/**/ /*  
 *    消息构造  
 
*/
  
function  CLASS_MSN_MESSAGE(id,width,height,caption,title,message,target,action) {  
    
this.id     = id;  
    
this.title  = title;  
    
this.caption= caption;  
    
this.message= message;  
    
this.target = target;  
    
this.action = action;  
    
this.width    = width?width:200;  
    
this.height = height?height:120;  
    
this.timeout= 150;  
    
this.speed    = 20
    
this.step    = 1
    
this.right    = screen.width -1;  
    
this.bottom = screen.height; 
    
this.left    = this.right - this.width; 
    
this.top    = this.bottom - this.height; 
    
this.timer    = 0
    
this.pause    = false;
    
this.close    = false;
    
this.autoHide    = true;
}
  
  
/**/ /*  
 *    隐藏消息方法  
 
*/
  
CLASS_MSN_MESSAGE.prototype.hide 
=   function () {  
    
if(this.onunload()){  

        
var offset  = this.height>this.bottom-this.top?this.height:this.bottom-this.top; 
        
var me  = this;  
 
        
if(this.timer>0){   
            window.clearInterval(me.timer);  
        }
  
 
        
var fun = function(){  
            
if(me.pause==false||me.close){
                
var x  = me.left; 
                
var y  = 0
                
var width = me.width; 
                
var height = 0
                
if(me.offset>0)
                    height 
= me.offset; 
                }
 
     
                y  
= me.bottom - height; 
     
                
if(y>=me.bottom)
                    window.clearInterval(me.timer);  
                    me.Pop.hide();  
                }
 else 
                    me.offset 
= me.offset - me.step;  
                }
 
                me.Pop.show(x,y,width,height);    
            }
             
        }
  
 
        
this.timer = window.setInterval(fun,this.speed)      
    }
  
}
  
  
/**/ /*  
 *    消息卸载事件,可以重写  
 
*/
  
CLASS_MSN_MESSAGE.prototype.onunload 
=   function ()  {  
    
return true;  
}
  
/**/ /*  
 *    消息命令事件,要实现自己的连接,请重写它  
 *  
 
*/
  
CLASS_MSN_MESSAGE.prototype.oncommand 
=   function () {  
    
this.close = true;
    
this.hide();
    
    
if(this.target=="blank")
    
{
      window.open(
this.action); 
    }

    
if(this.target=="self")
    
{
      parent.location 
= this.action;
    }
  
}
  
  
/**/ /*  
 *    消息显示方法  
 
*/
  
CLASS_MSN_MESSAGE.prototype.show 
=   function () {  

    
var oPopup = window.createPopup(); //IE5.5+  
    
    
this.Pop = oPopup;  
  
    
var w = this.width;  
    
var h = this.height;  
  
    
var str = "<DIV style='BORDER-RIGHT: #455690 1px solid; BORDER-TOP: #a6b4cf 1px solid; Z-INDEX: 99999; LEFT: 0px; BORDER-LEFT: #a6b4cf 1px solid; WIDTH: " + w + "px; BORDER-BOTTOM: #455690 1px solid; POSITION: absolute; TOP: 0px; HEIGHT: " + h + "px; BACKGROUND-COLOR: #c9d3f3'>"  
        str 
+= "<TABLE style='BORDER-TOP: #ffffff 1px solid; BORDER-LEFT: #ffffff 1px solid' cellSpacing=0 cellPadding=0 width='100%' bgColor=#cfdef4 border=0>"  
        str 
+= "<TR>"  
        str 
+= "<TD style='FONT-SIZE: 12px;COLOR: #0f2c8c' width=30 height=24></TD>"  
        str 
+= "<TD style='PADDING-LEFT: 4px; FONT-WEIGHT: normal; FONT-SIZE: 12px; COLOR: #1f336b; PADDING-TOP: 4px' vAlign=center width='100%'>" + this.caption + "</TD>"  
        str 
+= "<TD style='PADDING-RIGHT: 2px; PADDING-TOP: 2px' vAlign=center align=right width=19>"  
        str 
+= "<SPAN title=关闭 style='FONT-WEIGHT: bold; FONT-SIZE: 12px; CURSOR: hand; COLOR: red; MARGIN-RIGHT: 4px' id='btSysClose' >×</SPAN></TD>"  
        str 
+= "</TR>"  
        str 
+= "<TR>"  
        str 
+= "<TD style='PADDING-RIGHT: 1px;PADDING-BOTTOM: 1px' colSpan=3 height=" + (h-28+ ">"  
        str 
+= "<DIV style='BORDER-RIGHT: #b9c9ef 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: #728eb8 1px solid; PADDING-LEFT: 8px; FONT-SIZE: 12px; PADDING-BOTTOM: 8px; BORDER-LEFT: #728eb8 1px solid; WIDTH: 100%; COLOR: #1f336b; PADDING-TOP: 8px; BORDER-BOTTOM: #b9c9ef 1px solid; HEIGHT: 100%'>" + this.title + "<BR><BR>"  
        str 
+= "<DIV style='WORD-BREAK: break-all' align=left><A href='javascript:void(0)' hidefocus=true id='btCommand'><FONT color=#ff0000>" + this.message + "</FONT></A></DIV>"  
        str 
+= "</DIV>"  
        str 
+= "</TD>"  
        str 
+= "</TR>"  
        str 
+= "</TABLE>"  
        str 
+= "</DIV>"  
  
    oPopup.document.body.innerHTML 
= str; 
    
  
    
this.offset  = 0
    
var me  = this;  

    oPopup.document.body.onmouseover 
= function(){me.pause=true;}
    oPopup.document.body.onmouseout 
= function(){me.pause=false;}

    
var fun = function(){  
        
var x  = me.left; 
        
var y  = 0
        
var width    = me.width; 
        
var height    = me.height; 
 
            
if(me.offset>me.height)
                height 
= me.height; 
            }
 else 
                height 
= me.offset; 
            }
 
 
        y  
= me.bottom - me.offset; 
        
if(y<=me.top)
            me.timeout
--
            
if(me.timeout==0)
                window.clearInterval(me.timer);  
                
if(me.autoHide){
                    me.hide(); 
                }

            }
 
        }
 else 
            me.offset 
= me.offset + me.step; 
        }
 
        me.Pop.show(x,y,width,height);    
 
    }
  
  
    
this.timer = window.setInterval(fun,this.speed)      
  
     
  
    
var btClose = oPopup.document.getElementById("btSysClose");  
  
    btClose.onclick 
= function(){  
        me.close 
= true;
        me.hide();  
    }
  
  
    
var btCommand = oPopup.document.getElementById("btCommand");  
    btCommand.onclick 
= function(){  
        me.oncommand(); 
    }
    
}
  
/**/ /* 
** 设置速度方法 
*
*/
 
CLASS_MSN_MESSAGE.prototype.speed 
=   function (s)
    
var t = 20
    
try 
        t 
= praseInt(s); 
    }
 catch(e){} 
    
this.speed = t; 
}
 
/**/ /* 
** 设置步长方法 
*
*/
 
CLASS_MSN_MESSAGE.prototype.step 
=   function (s)
    
var t = 1
    
try 
        t 
= praseInt(s); 
    }
 catch(e){} 
    
this.step = t; 
}
 
  
CLASS_MSN_MESSAGE.prototype.rect 
=   function (left,right,top,bottom)
    
try 
        
this.left        = left    !=null?left:this.right-this.width; 
        
this.right        = right    !=null?right:this.left +this.width; 
        
this.bottom        = bottom!=null?(bottom>screen.height?screen.height:bottom):screen.height; 
        
this.top        = top    !=null?top:this.bottom - this.height; 
    }
 catch(e){} 
}
 
</ SCRIPT >  

将这个文件的属性设置成为“嵌入的资源”。

二、在这个项目中添加一个类,类的代码如下:

using  System;
using  System.Collections.Generic;
using  System.Text;

using  System.IO;
using  System.Reflection;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;

namespace  NoticeClass
{
    
/// <summary>
    
/// 实现类似MSN消息提示
    
/// </summary>

    public class NoticeScript
    
{
        Page page;
        UserControl control;
        
int winWidth =200 ;
        
int winHeight = 120 ;
        
string winCaption = "短消息提示:";
        
int speed = 10;
        
int step = 5;

        
bool isPage;

        
/// <summary>
        
/// 消息提示窗体宽度
        
/// </summary>

        public int WinWidth
        
{
            
get return winWidth; }
            
set { winWidth = value; }
        }


        
/// <summary>
        
/// 消息提示窗体高度
        
/// </summary>

        public int WinHeight
        
{
            
get return winHeight; }
            
set { winHeight = value; }
        }


        
/// <summary>
        
/// 消息提示窗体标题
        
/// </summary>

        public string WinCaption
        
{
            
get return winCaption; }
            
set { winCaption = value; }
        }


        
/// <summary>
        
/// 消息提示窗体显示的速度
        
/// </summary>

        public int Speed
        
{
            
get return speed; }
            
set { speed = value; }
        }


        
/// <summary>
        
/// 消息提示窗体显示时的步长
        
/// </summary>

        public int Step
        
{
            
get return step; }
            
set { step = value; }
        }


        
/// <summary>
        
/// 用于显示消息的页面实例
        
/// </summary>

        public Page NoticePage
        
{
            
get return page; }
            
set { page = value; }
        }


        
/// <summary>
        
/// 用于 显示消息的用户控件实例
        
/// </summary>

        public UserControl NoticeUserControl
        
{
            
get return control; }
            
set{control = value;}
        }


        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="noticePage">显示消息的页面</param>

        public NoticeScript(Page noticePage)
        
{
            
this.isPage = true;
            
this.page = noticePage;
            RegScript(noticePage);
        }


        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="userControl">显示消息的用户控件</param>

        public NoticeScript(UserControl userControl)
        
{
            
this.isPage = false;
            
this.control = userControl;
            RegScript(userControl.Page);
        }


        
/// <summary>
        
/// 显示提示消息
        
/// </summary>
        
/// <param name="noticeID">消息提示标识</param>
        
/// <param name="width">消息提示窗体宽</param>
        
/// <param name="height">消息提示窗体高</param>
        
/// <param name="caption">消息提示窗体标题</param>
        
/// <param name="title">消息标题</param>
        
/// <param name="message">消息内容</param>
        
/// <param name="target">链接的打开方式</param>
        
/// <param name="url">消息内容链接到的地址</param>

        public void ShowNotice(string noticeID, int width, int height, string caption, string title, string message,Target target,string url)
        
{
            
if (target == Target.Blank)
            
{
                Show(noticeID, width, height, caption, title, message, 
"blank", url);
            }

            
else
            
{
                Show(noticeID, width, height, caption, title, message, 
"self", url);
            }

        }


        
/// <summary>
        
/// 显示提示消息
        
/// </summary>
        
/// <param name="noticeID">消息提示标识</param>
        
/// <param name="width">消息提示窗体宽</param>
        
/// <param name="height">消息提示窗体高</param>
        
/// <param name="caption">消息提示窗体标题</param>
        
/// <param name="title">消息标题</param>
        
/// <param name="message">消息内容</param>

        public void ShowNotice(string noticeID,int width,int height,string caption,string title,string message)
        
{
            Show(noticeID, width, height, caption, title, message);
        }


        
/// <summary>
        
/// 显示提示消息
        
/// </summary>
        
/// <param name="noticeID">消息提示标识</param>
        
/// <param name="title">消息标题</param>
        
/// <param name="message">消息内容</param>

        public void ShowNotice(string noticeID, string title, string message)
        
{
            Show(noticeID, 
this.winWidth, this.winHeight, this.winCaption, title, message);
        }


        
/// <summary>
        
/// 显示提示消息
        
/// </summary>
        
/// <param name="noticeID">消息提示标识</param>
        
/// <param name="title">消息标题</param>
        
/// <param name="message">消息内容</param>
        
/// <param name="target">链接的打开方式</param>
        
/// <param name="url">消息内容链接到的地址</param>

        public void ShowNotice(string noticeID, string title, string message,Target target,string url)
        
{
            
if (target == Target.Blank)
            
{
                Show(noticeID, 
this.winWidth, this.winHeight, this.winCaption, title, message, "blank", url);
            }

            
else
            
{
                Show(noticeID, 
this.winWidth, this.winHeight, this.winCaption, title, message, "self", url);
            }

        }


        
private void Show(string noticeID, int width, int height, string caption, string title, string message)
        
{
            
string alert = "<script>var MSG1 = new CLASS_MSN_MESSAGE("" + noticeID + ""," + width + "," + height + ","" +
                caption 
+ "","" + title + "","" + message + "");";
            alert 
+= "MSG1.rect(screen.width - 205, null, null, screen.height - 60);";
            alert 
+= "MSG1.speed = " + this.speed + ";";
            alert 
+= "MSG1.step = " + this.step + ";";
            alert 
+= "MSG1.show(); ";
            alert 
+= "</script>";

            
if (isPage)
            
{
                page.Controls.Add(
new LiteralControl(alert));
            }

            
else
            
{
                control.Controls.Add(
new LiteralControl(alert));
            }

        }


        
private void Show(string noticeID, int width, int height, string caption, string title, string message,string target,string url)
        
{
            
string alert = "<script>var MSG1 = new CLASS_MSN_MESSAGE("" + noticeID + ""," + width + "," + height + ","" +
                caption 
+ "","" + title + "","" + message + "","" + target + "","" + url + "");";
            
            alert 
+= "MSG1.rect(screen.width - 205, null, null, screen.height - 60);";
            alert 
+= "MSG1.speed = " + this.speed + ";";
            alert 
+= "MSG1.step = " + this.step + ";";
            alert 
+= "MSG1.show(); ";
            alert 
+= "</script>";

            
if (isPage)
            
{
                page.Controls.Add(
new LiteralControl(alert));
            }

            
else
            
{
                control.Controls.Add(
new LiteralControl(alert));
            }

        }


        
private void RegScript(Page showPage)
        
{
            
string clientScript = "";

            Assembly asm 
= Assembly.GetExecutingAssembly();
            
string resource = "NoticeClass.Notice.js";
            Stream stm 
= asm.GetManifestResourceStream(resource);

            StreamReader reader 
= new StreamReader(stm);
            clientScript 
= reader.ReadToEnd();
            reader.Close();
            stm.Close();

            
if (!showPage.ClientScript.IsClientScriptBlockRegistered(showPage.GetType(), "clientScript"))
            
{
                showPage.ClientScript.RegisterClientScriptBlock(showPage.GetType(), 
"clientScript", clientScript);
            }

        }

    }


    
public enum Target
    
{
        Blank,
        Self
    }

}

编译这个类库项目,产生一个dll文件。这样,就完成了该类库的设计。

三、调用方式。在需要提示的asp.net项目中,添加上述dll文件的引用。在要提示的方法或函数中,增加如下代码即可完成提示:

NoticeClass.NoticeScript notice  =   new  NoticeScript( this );
notice.ShowNotice(
" 1 " 200 120 " 消息提示: " " 你有1条消息 " " 欢迎访问小小程序员博客! " );

// 也可以选用以下的三个方法,带url参数的可以实现点击消息跳转到相应的url
// notice.ShowNotice("2", 200, 120, "消息提示:", "你有1条消息", "有欢迎访问小小程序员博客!", Target.Self, " http://blog.youkuaiyun.com/oneiter ");
// notice.ShowNotice("3", "你有1条消息", "欢迎访问小小程序员博客!");
// notice.ShowNotice("4", "你有1条消息", "欢迎访问小小程序员博客!", Target.Blank, " http://blog.youkuaiyun.com/oneiter ");

这种实现方式在其它资料上讲述较少,所以我的实现方式也只是一个探索,未必成熟,欢迎提出宝贵意见。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值