概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
RSSビュアーを作れ。
サンプルコード
using System;
using System.Drawing;
using System.Windows.Forms;
public class WebApp: Form {
Panel m_plControls;
TextBox m_tbUrl;
Button m_btSrc,
m_btLinks;
WebBrowser m_wbDoc;
TextBox m_tbView;
public WebApp() {
Text = "br2";
m_plControls = new Panel();
m_plControls.Height = 32;
m_tbUrl = new TextBox();
m_tbUrl.Width = 240;
m_tbUrl.Left = 20;
m_tbUrl.Top = (m_plControls.Height / 2) - (m_tbUrl.Height / 2);
m_tbUrl.KeyDown += new KeyEventHandler(UrlKeyDown);
m_tbUrl.Text = "http://qiita.com/ohisama@github/feed.atom";
m_plControls.Controls.Add(m_tbUrl);
m_btSrc = new Button();
m_btSrc.Text = "htmlソース";
m_btSrc.Left = m_tbUrl.Bounds.Right + 20;
m_btSrc.Top = (m_plControls.Height / 2) - (m_btSrc.Height / 2);
m_btSrc.Click += new EventHandler(SrcClick);
m_plControls.Controls.Add(m_btSrc);
m_btLinks = new Button();
m_btLinks.Text = "リンク一覧";
m_btLinks.Left = m_btSrc.Bounds.Right + 20;
m_btLinks.Top = (m_plControls.Height / 2) - (m_btLinks.Height / 2);
m_btLinks.Click += new EventHandler(LinksClick);
m_plControls.Controls.Add(m_btLinks);
Controls.Add(m_plControls);
m_wbDoc = new WebBrowser();
m_wbDoc.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocCompleted);
Controls.Add(m_wbDoc);
m_tbView = new TextBox();
m_tbView.Multiline = true;
m_tbView.Height = 64;
Controls.Add(m_tbView);
ClientSize = new Size(800, 600);
}
protected override void OnResize(EventArgs e) {
m_plControls.Width = ClientSize.Width;
int iDocWidth = ClientSize.Width;
int iDocHeight = ClientSize.Height - (m_plControls.Height + m_tbView.Height);
m_wbDoc.Size = new Size(iDocWidth, iDocHeight);
m_wbDoc.Top = m_plControls.Top + m_plControls.Height;
m_tbView.Width = ClientSize.Width;
m_tbView.Top = m_wbDoc.Top + m_wbDoc.Height;
}
public void UrlKeyDown(Object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter)
{
m_wbDoc.Navigate(m_tbUrl.Text);
}
}
public void DocCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e) {
m_tbUrl.Text = m_wbDoc.Document.Url.ToString();
}
public void SrcClick(Object sender, EventArgs e) {
m_tbView.Text = m_wbDoc.DocumentText;
}
public void LinksClick(Object sender, EventArgs e) {
HtmlElementCollection hecList = m_wbDoc.Document.Links;
String stView = "";
int i = 0;
foreach (HtmlElement elm in hecList)
{
if (elm.GetAttribute("href") != null)
{
stView += i.ToString();
stView += " - " + elm.GetAttribute("href").ToString();
stView += "\r\n";
i++;
}
}
m_tbView.Text = stView;
}
[STAThread]
public static void Main() {
WebApp app = new WebApp();
Application.Run(app);
}
}
写真
以上。