推荐两个web菜单控件(右击和顶部主菜单)
前两天在整理电脑资料时,发现这两个已被打入"冷宫"的控件。
运行效果如下:
其中的右击菜单控件的js源码是从milonic网站(一个商业JS库)上搜集来的, 本人觉得它的"生成菜单项"部分
非常不好用,所以自己封装了它的部分代码,用一个 XML文件记录相应的菜单项,并用cs写了一个递归来加载XML
中的菜单项,以实现控件绑定。
代码如下:
1protected override void Render(HtmlTextWriter output)
2 {
3
4 output.Write("<script type=\"text/javascript\" src=\""+ScriptPath+"/contextmenu.
5 js\"></script>\r\n");
6
7 output.Write("<script type=\"text/javascript\">\r\n");
8
9 output.Write("fixMozillaZIndex=true;\r\n");
10 output.Write("_menuCloseDelay=500;\r\n");
11 output.Write("_menuOpenDelay=150;\r\n");
12 output.Write("_subOffsetTop=2;\r\n");
13 output.Write("_subOffsetLeft=-2;\r\n");
14
15 output.Write("with(contextStyle=new mm_style())\r\n");
16 output.Write("{\r\n");
17 output.Write("bordercolor=\"#999999\";\r\n");
18 output.Write("borderstyle=\"solid\";\r\n");
19 output.Write("borderwidth=1;\r\n");
20 output.Write("fontfamily=\"arial, verdana, tahoma\";\r\n");
21 output.Write("fontsize=\"75%\";\r\n");
22 output.Write("fontstyle=\"normal\";\r\n");
23 output.Write("headerbgcolor=\"#4F8EB6\";\r\n");
24 output.Write("headerborder=1;\r\n");
25 output.Write("headercolor=\"#ffffff\";\r\n");
26 output.Write("offbgcolor=\"#ffffff\";\r\n");
27 output.Write("offcolor=\"#000000\";\r\n");
28 output.Write("onbgcolor=\"#ECF4F9\";\r\n");
29 output.Write("onborder=\"1px solid #316AC5\";\r\n");
30 output.Write("oncolor=\"#000000\";\r\n");
31 output.Write("outfilter=\"randomdissolve(duration=0.4)\";\r\n");
32 output.Write("overfilter=\"Fade(duration=0.2);Shadow(color=#777777', Direction=135,
33 Strength=3)\";\r\n");
34 output.Write("padding=3;\r\n");
35 output.Write("pagebgcolor=\"#eeeeee\";\r\n");
36 output.Write("pageborder=\"1px solid #ffffff\";\r\n");
37 output.Write("pageimage=\""+this.ImageUrl+"/pageimg.gif\";\r\n");
38 output.Write("separatorcolor=\"#999999\";\r\n");
39 output.Write("subimage=\""+this.ImageUrl+"/subarrow.gif\";\r\n");
40 output.Write("}\r\n");
41
42 output.Write("with(milonic=new menuname(\"contextMenu\")){\r\n");
43 output.Write("margin=3;\r\n");
44 output.Write("style=contextStyle;\r\n");
45 output.Write("top=\"offset=2\";\r\n");
46
47 DataSet dsSrc = new DataSet();
48 dsSrc.ReadXml(System.Web.HttpContext.Current.Server.MapPath(this.XmlFileFullPathName));
49
50 string result="";
51 foreach(System.Data.DataRow dr in dsSrc.Tables[0].Select("parentid=0"))
52 {
53
54 输出主菜单
86 }
87
88 output.Write("}\r\n");
89 output.Write(result);
90 output.Write("drawMenus();\r\n");
91
92 output.Write("</script>\r\n");
93 }
94
95 private string LoadSubMenuItem(DataSet dsSrc ,string parentid,string itemwidth, string margin)
96 {
97 StringBuilder sb=new StringBuilder();
98
99 DataRow[] drs = dsSrc.Tables[0].Select("parentid="+parentid);
100 if(drs.Length>0)
101 {
102
103 sb.Append("with(milonic=new menuname(\""+parentid+"\")){\r\n");
104 sb.Append("itemwidth="+(itemwidth==""?"127":itemwidth)+";\r\n");
105 sb.Append("margin="+(margin==""?"3":margin)+";\r\n");
106 sb.Append("overflow=\"scroll\";\r\n");
107 sb.Append("style=contextStyle;\r\n");
108
109 string result="";
110 foreach(DataRow dr in drs)
111 {
112 输出主菜单
145 }
146 sb.Append("}\r\n");
147 sb.Append(result);
148 }
149 return sb.ToString();
150 }
151
152
2 {
3
4 output.Write("<script type=\"text/javascript\" src=\""+ScriptPath+"/contextmenu.
5 js\"></script>\r\n");
6
7 output.Write("<script type=\"text/javascript\">\r\n");
8
9 output.Write("fixMozillaZIndex=true;\r\n");
10 output.Write("_menuCloseDelay=500;\r\n");
11 output.Write("_menuOpenDelay=150;\r\n");
12 output.Write("_subOffsetTop=2;\r\n");
13 output.Write("_subOffsetLeft=-2;\r\n");
14
15 output.Write("with(contextStyle=new mm_style())\r\n");
16 output.Write("{\r\n");
17 output.Write("bordercolor=\"#999999\";\r\n");
18 output.Write("borderstyle=\"solid\";\r\n");
19 output.Write("borderwidth=1;\r\n");
20 output.Write("fontfamily=\"arial, verdana, tahoma\";\r\n");
21 output.Write("fontsize=\"75%\";\r\n");
22 output.Write("fontstyle=\"normal\";\r\n");
23 output.Write("headerbgcolor=\"#4F8EB6\";\r\n");
24 output.Write("headerborder=1;\r\n");
25 output.Write("headercolor=\"#ffffff\";\r\n");
26 output.Write("offbgcolor=\"#ffffff\";\r\n");
27 output.Write("offcolor=\"#000000\";\r\n");
28 output.Write("onbgcolor=\"#ECF4F9\";\r\n");
29 output.Write("onborder=\"1px solid #316AC5\";\r\n");
30 output.Write("oncolor=\"#000000\";\r\n");
31 output.Write("outfilter=\"randomdissolve(duration=0.4)\";\r\n");
32 output.Write("overfilter=\"Fade(duration=0.2);Shadow(color=#777777', Direction=135,
33 Strength=3)\";\r\n");
34 output.Write("padding=3;\r\n");
35 output.Write("pagebgcolor=\"#eeeeee\";\r\n");
36 output.Write("pageborder=\"1px solid #ffffff\";\r\n");
37 output.Write("pageimage=\""+this.ImageUrl+"/pageimg.gif\";\r\n");
38 output.Write("separatorcolor=\"#999999\";\r\n");
39 output.Write("subimage=\""+this.ImageUrl+"/subarrow.gif\";\r\n");
40 output.Write("}\r\n");
41
42 output.Write("with(milonic=new menuname(\"contextMenu\")){\r\n");
43 output.Write("margin=3;\r\n");
44 output.Write("style=contextStyle;\r\n");
45 output.Write("top=\"offset=2\";\r\n");
46
47 DataSet dsSrc = new DataSet();
48 dsSrc.ReadXml(System.Web.HttpContext.Current.Server.MapPath(this.XmlFileFullPathName));
49
50 string result="";
51 foreach(System.Data.DataRow dr in dsSrc.Tables[0].Select("parentid=0"))
52 {
53
54 输出主菜单
86 }
87
88 output.Write("}\r\n");
89 output.Write(result);
90 output.Write("drawMenus();\r\n");
91
92 output.Write("</script>\r\n");
93 }
94
95 private string LoadSubMenuItem(DataSet dsSrc ,string parentid,string itemwidth, string margin)
96 {
97 StringBuilder sb=new StringBuilder();
98
99 DataRow[] drs = dsSrc.Tables[0].Select("parentid="+parentid);
100 if(drs.Length>0)
101 {
102
103 sb.Append("with(milonic=new menuname(\""+parentid+"\")){\r\n");
104 sb.Append("itemwidth="+(itemwidth==""?"127":itemwidth)+";\r\n");
105 sb.Append("margin="+(margin==""?"3":margin)+";\r\n");
106 sb.Append("overflow=\"scroll\";\r\n");
107 sb.Append("style=contextStyle;\r\n");
108
109 string result="";
110 foreach(DataRow dr in drs)
111 {
112 输出主菜单
145 }
146 sb.Append("}\r\n");
147 sb.Append(result);
148 }
149 return sb.ToString();
150 }
151
152
而另一个顶部菜单源码(c#)最早是从codeproject上得到的, 原作者我已忘记了,本人在这个控件基础上加上了图片菜单项
(修改 xsl),相信如果大家有Css基础的话,只要改一下相应的menu.css中的代码就会得到诸如 XP风格等样式的菜单了。另外因
为原作者写的代码扩展性很好,大家可以加入更多的个性化的元素。
关键字:.net, 控件,control,菜单,mainmenu右击菜单,contextmenu,代震军,daizhj
下载链接(for vs2008):/Files/daizhj/ContextMenu.rar