類似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"200120"消息提示:""你有1條消息""歡迎訪問小小程序員博客!");

//也可以選用以下的三個方法,帶url參數的可以實現點擊消息跳轉到相應的url
//notice.ShowNotice("2", 200, 120, "消息提示:", "你有1條消息", "有歡迎訪問小小程序員博客!", Target.Self, "http://blog.csdn.net/oneiter");
//notice.ShowNotice("3", "你有1條消息", "歡迎訪問小小程序員博客!");
//notice.ShowNotice("4", "你有1條消息", "歡迎訪問小小程序員博客!", Target.Blank, "http://blog.csdn.net/oneiter");

這種實現方式在其它資料上講述較少,所以我的實現方式也只是一個探索,未必成熟,歡迎提出寶貴意見。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章