BosonZhang's Blog



    不积跬步,无以至千里;不积小流,无以成江海。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

图片新闻栏目的实现小结

Posted on 2008-07-27 10:10  qdzhbsh  阅读(1020)  评论(1)    收藏  举报

      图片新闻栏目在许多网站的主页中都能见到,而且一般都放在比较醒目的位置。一条新闻,如果再附上一幅生动的图片,一定会更吸引你去点击,看看里面到底写了些什么,远比点击一个生硬的标题链接,效果要好的多。而且图片新闻栏目还有循环播放的功能,让你在懒得点鼠标的时候,也可以看到更多的图片新闻。好了,说了这么多,应该步入正题了,下面就讲一下图片新闻栏目的一个实现过程,用来显示图片新闻的控件其实是一个名称为focus.swf的FLASH,要让此FLASH工作,除了设置一些基本属性外,还有两个重要的属性必须设置,一个是名称为FlashVars的param标记的value属性,另一个是embed标记的FlashVars属性。这两个属性设置的值是相同的,而且其中都包含pics(图片路径字符串),links(链接字符串),texts(标题字符串),borderwidth(图片宽度),borderheight(图片高度),textheight(标题高度)等属性,是一个复合字符串。还有一点需要说明的是图片路径字符串、链接字符串和标题字符串中的内容必须以"|"分割。当为图片新闻控件的属性正确赋值后,它将循环播放图片新闻,图片和标题是同步的,而且点击标题会跳到一个指定的页面去。最后讲一下此图片新闻控件的一些小BUG,仅支持JPG格式的图片,而且最多支持五张图片的循环播放。此FLASH在网上应该能下到,如果下不到请回帖说明。后附一张效果图和相关代码。

 

                    

 

---------- 图片新闻前台页面 PhotoNews.aspx----------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PhotoNews.aspx.cs" Inherits="Test_PhotoNews" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>       
</head>
<body>
    <form id="form1" runat="server">
     <table border="0" width="195" id="table14" cellspacing="0" cellpadding="0" background="../Images/tianyi/right/3.jpg">
      <tr>
       <td style="width: 195px">
        <img alt="" src="../Images/tianyi/right/01.jpg" width="196" height="57"/>
    </td>
   </tr>
   <tr>
       <td style="width: 195px">
        <table border="0" width="100%" id="table15" cellspacing="0" cellpadding="0">
         <tr>
          <td style="width: 31px">
           <img alt="" src="../Images/tianyi/right/49.jpg" width="33" height="5"/>
       </td>
       <td align="center"><%=content %></td>
       <td style="width: 8px">
       </td>
      </tr>
     </table>
    </td>
   </tr>
   <tr>
       <td style="width: 195px">
    </td>
   </tr>
  </table>
    </form>
</body>
</html>

---------- 图片新闻后台页面 PhotoNews.aspx.cs----------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Solution.USL.Module;

 

public partial class Test_PhotoNews : System.Web.UI.Page
{
    public string content = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        PhotoNewsProxy pnp = new PhotoNewsProxy();

        content = pnp.ShowTPXW("图片新闻");

    }
}

---------- 图片新闻代理类 PhotoNewsProxy.cs----------

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

using System.Data;
using System.Configuration;
using System.Collections;

using Solution.BLL;

 

namespace Solution.USL.Module
{
    public class PhotoNewsProxy
    {
        private string _webSiteAddress = "";

        public PhotoNewsProxy()
        {
            _webSiteAddress = ConfigurationManager.AppSettings["WebSiteAddress"];
        }

        /// <summary>
        /// 得到图片新闻控件所需的图片路径字符串
        /// </summary>
        /// <param name="typeTitle">栏目标题</param>
        /// <returns></returns>
        public string GetPics(string typeTitle)
        {
            string ret = "";

            string[] paths = GetPhotoNewsPaths(typeTitle);

            string temp = "";

            if (paths != null && paths.Length > 0)
            {
                for (int i = 0; i < paths.Length; i++)
                {
                    if (paths[i] != "")
                    {
                        temp = _webSiteAddress + "/web" + paths[i];

                        ret += temp + "|";
                    }
                    else
                    {
                        ret += temp + "|";
                    }
                }

                ret = ret.Substring(0, ret.Length - 1);
            }

            return ret;
        }

        /// <summary>
        /// 得到图片新闻控件所需的链接字符串
        /// </summary>
        /// <param name="typeTitle">栏目标题</param>
        /// <returns></returns>
        public string GetLinks(string typeTitle)
        {
            string ret = "";

            string[] guids = GetPhotoNewsGuids(typeTitle);

            string temp = "";

            if (guids != null && guids.Length > 0)
            {
                for (int i = 0; i < guids.Length; i++)
                {
                    temp = _webSiteAddress + "/web/Main/InfolibItemViewFrame.aspx?guid=" + guids[i];

                    ret += temp + "|";
                }

                ret = ret.Substring(0, ret.Length - 1);
            }

            return ret;
        }

        /// <summary>
        /// 得到图片新闻控件所需的标题字符串
        /// </summary>
        /// <param name="typeTitle">栏目标题</param>
        /// <returns></returns>
        public string GetTexts(string typeTitle)
        {
            string ret = "";

            string[] titles = GetPhotoNewsTitles(typeTitle);

            if (titles != null && titles.Length > 0)
            {
                 for (int i = 0; i < titles.Length; i++)
                 {
                      ret += titles[i] + "|";
                 }
                 ret = ret.Substring(0, ret.Length - 1);
            }

            return ret;
        }

        /// <summary>
        /// 得到图片新闻的内容
        /// </summary>
        /// <param name="typeTitle">栏目标题</param>
        /// <returns></returns>
        public string ShowTPXW(string typeTitle)
        {
            string pics = GetPics(typeTitle);
            string links = GetLinks(typeTitle);
            string texts = GetTexts(typeTitle);

 

            int focus_width = 150;
            int focus_height = 160;
            int text_height = 20;
            int swf_height = focus_height + text_height;

 

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"" + focus_width.ToString() + "\" height=\"" + swf_height.ToString() + "\">");
            sb.AppendLine("<param name=\"allowScriptAccess\" value=\"sameDomain\"><param name=\"movie\" value=\"" + _webSiteAddress + "/web/Images/tianyi/right/focus.swf\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#fffff\">");
            sb.AppendLine("<param name=\"menu\" value=\"false\"><param name=\"wmode\" value=\"transparent\">");
            sb.AppendLine("<param name=\"FlashVars\" value=\"pics=" + pics + "&links=" + links + "&texts=" + texts + "&borderwidth=" + focus_width.ToString() + "&borderheight=" + focus_height.ToString() + "&textheight=" + text_height.ToString() + "\">");
            sb.AppendLine("<embed src=\"" + _webSiteAddress + "/web/Images/tianyi/right/focus.swf\" wmode=\"opaque\" FlashVars=\"texts=" + texts + "&pics=" + pics + "&links=" + links + "&borderwidth=" + focus_width.ToString() + "&borderheight=" + focus_height.ToString() + "&textheight=" + text_height.ToString() + "\" menu=\"false\" bgcolor=\"#ffffff\" quality=\"high\" width=\"" + focus_width.ToString() + "\" height=\"" + swf_height.ToString() + "\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer/%22/>");
            sb.AppendLine("</object>");

 

            return sb.ToString();
        }


        /// <summary>
        /// 得到图片新闻的标题数组
        /// </summary>
        /// <param name="typeTitle">栏目标题</param>
        /// <returns></returns>
        public string[] GetPhotoNewsTitles(string typeTitle)
        {
            string[] titles = null;

            PhotoNewsManager pnm = new PhotoNewsManager();

            DataSet ds = pnm.GetReleaseInfo(5, typeTitle, "0");

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                titles = new string[ds.Tables[0].Rows.Count];

                for (int i = 0; i < ds.Tables[0].Rows.Count;i++ )
                {
                    DataRow dr = ds.Tables[0].Rows[i];

                    int length = dr["txt_title"].ToString().Length;

                    string title = "";

                    if (length > 12)
                    {
                        title = dr["txt_title"].ToString().Substring(0, 10) + "...";
                    }
                    else
                    {
                        title = dr["txt_title"].ToString();
                    }

                    titles[i] = title;
                }
               
            }

            return titles;

        }

        /// <summary>
        /// 得到图片新闻的GUID数组
        /// </summary>
        /// <param name="typeTitle">栏目标题</param>
        /// <returns></returns>
        public string[] GetPhotoNewsGuids(string typeTitle)
        {
            string[] guids = null;

            PhotoNewsManager pnm = new PhotoNewsManager();

            DataSet ds = pnm.GetReleaseInfo(5, typeTitle, "0");

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                guids = new string[ds.Tables[0].Rows.Count];

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];

                    guids[i] = dr["GUID"].ToString();
                }

            }

            return guids;

        }

        /// <summary>
        /// 得到图片新闻的路径数组
        /// </summary>
        /// <param name="typeTitle">栏目标题</param>
        /// <returns></returns>
        public string[] GetPhotoNewsPaths(string typeTitle)
        {
            PhotoNewsManager pnm = new PhotoNewsManager();

            return pnm.GetPhotoNewsPaths(typeTitle);
        }


    }

 

}