65.9K
CodeProject 正在变化。 阅读更多。
Home

CPBar显示最新文章摘要(RSS feed)

2003 年 7 月 7 日

2分钟阅读

viewsIcon

91353

downloadIcon

1914

本文介绍如何创建一个垂直资源管理器栏,它从 CodeProject 的 RSS 订阅源显示最新的文章摘要。

引言

我借助 BandObject 基类以及本文 使用 .NET 和 Windows Forms 扩展资源管理器实现了此资源管理器栏。这个垂直资源管理器栏实际上会获取 CodeProject 最新文章摘要的 RSS 订阅源,地址为 https://codeproject.org.cn/webservices/articlerss.aspx,提取每篇文章的详细信息,然后以滚动的方式显示它们。

CPBar 设计概述

CodeProjectBar 对象类似于一个窗体,您可以直接从工具箱拖放控件到其中。至于 ScrollScreen,它是一个自定义控件,包含许多 RichLabel 对象,并使用计时器协调它们的滚动。RichLabel 对象的作用仅仅是组织和格式化用于显示的数据。除此之外,它还应该隐藏光标。(请参阅尚未解决的问题部分)

获取 CP 的 RSS 订阅源

上图显示了 RSS 订阅源文件包含的部分内容。每个 item 标签代表一个文章摘要,其中包含文章标题、描述、作者等信息。因此,为了提取它们,我做了以下操作。

private void DownloadCPRSSFeed(string url /* url of the rss feed */)
{
    XmlTextReader xmlTextReader = new XmlTextReader(url);

    // We don't want to handle whitespaces
    xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;

    // Read through the xml document
    while(xmlTextReader.Read())
    {
        // Check if this is an element of name "item", continue reading
        if (xmlTextReader.NodeType == XmlNodeType.Element 
               && xmlTextReader.Name == "item")
        {
            // Continue read
            xmlTextReader.Read();

            // Get the content for each element. Although I am 
            // not interested in the "category"
            // data, i still need to follow the order so that 
            // I can reach the date section.
            string title = xmlTextReader.ReadElementString("title");
            string desc = xmlTextReader.ReadElementString("description");
            string link = xmlTextReader.ReadElementString("link");
            string author = xmlTextReader.ReadElementString("author");
            string category = xmlTextReader.ReadElementString("category");
            string date = xmlTextReader.ReadElementString("pubDate");

            // Add an item for these data to our scroll screen
            scrollArticlesScreen.AddItem(date, title, link, author, desc);
        }
    }
}

设置 CPBar

这是一个简单的安装程序,用于将 CPBar 安装到您的计算机上。下面显示了安装和卸载部分的代码。

// Install
private void btnInstallCPBar_Click(object sender, System.EventArgs e)
{            
    // Run "gacutil.exe /i CPBar.dll" and "regasm CPBar.dll"
    System.Diagnostics.Process.Start(Application.StartupPath + 
         '\\' + "gacutil.exe", "/i CPBar.dll");
    System.Diagnostics.Process.Start(Application.StartupPath + 
         '\\' + "regasm.exe", "CPBar.dll");
}

// Uninstall
private void btnUninstallCPBar_Click(object sender, System.EventArgs e)
{
    // Run "regasm /u CPBar.dll" and "gacutil.exe /u CPBar.dll"
    System.Diagnostics.Process.Start(Application.StartupPath + '\\' 
         + "regasm.exe", "/u CPBar.dll");
    System.Diagnostics.Process.Start(Application.StartupPath + '\\' 
         + "gacutil.exe", "/u CPBar.dll");
}

尚未解决的问题

这里有一些我遇到的问题,但到目前为止,我仍然没有找到解决方案。

  • 当鼠标悬停在 RichLabel 中的链接上时,手型光标未显示(但链接仍然有效)
  • 实际上,当 ScrollScreen 未托管在 CodeProjectBar 中时,情况并非如此。例如,我创建了另一个应用程序,并将我的 ScrollScreen 控件拖放到其中。

  • 单击 RichLabel 时,光标仍然可见
  • 我无法在 C# 中找到类似 HideCaret() 或类似的方法。

历史

无。

© . All rights reserved.