Як отримати значок веб-сайту

Автор работы: Пользователь скрыл имя, 26 Апреля 2013 в 17:43, контрольная работа

Описание работы

У цій роботі описується, як створити і реалізувати деякі з особливостей додатку Web Browser, запам'ятовувати Вибрані веб-вузли і використовувати Links Bar, зберігати Історію відвіданих сторінок, пошук в Google або Live Search, зберігати настроювальні параметри, проглядання джерела і властивостей веб-вузла, друк вмісту сторінки, і інші можливості. C# має WebBrowser control, який робить всю роботу з передачі веб-сторінок.

Содержание работы

Вступ 3
Як отримати значок веб-сайту 4
Панель посилань (Links Bar) 4
Історія і Вибране 6
Перегляд джерела 8
Пошук 9
Збереження настроювальних Параметрів 9
Властивості, Друк, Попередній Перегляд, Установки Сторінки, Зберегти Як 11
Вирізати, Копіювати, Вставити, Виділити Все 12
Список літератури 32

Файлы: 1 файл

Web-браузер.doc

— 355.50 Кб (Скачать файл)

                                historyTreeView.Nodes[url.Host.ToString()].Nodes.Add(node);

                            }

                            else

                                historyTreeView.Nodes[url.Host.ToString()].Nodes[url.ToString()].ToolTipText

                                        = url.ToString() + "\nLast Visited: " + data + "\nTimes visited" + i.ToString();

 

                    }

                    /* view by date*/

                    if (comboBox1.Text.Equals("View by Date"))

                    {

                        if (historyTreeView.Nodes[4].Nodes.ContainsKey(url.ToString()))

                            historyTreeView.Nodes[url.ToString()].ToolTipText

                                    = url.ToString() + "\nLast Visited: " + data + "\nTimes visited: " + i.ToString();

                        else

                        {

                            TreeNode node =

                                new TreeNode(url.ToString(), 3, 3);

                            node.ToolTipText = url.ToString() + "\nLast Visited: " + data + "\nTimes visited :" + i.ToString();

                            node.Name = url.ToString();

                            node.ContextMenuStrip = histContextMenu;

                            historyTreeView.Nodes[4].Nodes.Add(node);

                        }

                    }

                }

 

            }

            myXml.Save(historyXml);

        }

//delete history

        private void deleteHistory()

        {

            XmlDocument myXml = new XmlDocument();

            myXml.Load(historyXml);

            XmlElement root = myXml.DocumentElement;

            foreach (XmlElement x in root.ChildNodes)

            {

                if (x.GetAttribute("url").Equals(adress))

                {

                    root.RemoveChild(x);

                    break;

                }

            }

            historyTreeView.SelectedNode.Remove();

            myXml.Save(historyXml);

        }

 

        #endregion

 

        #region TABURI

        /*TAB-uri*/

 

       //addNewTab method

        private void addNewTab()

        {

            TabPage tpage = new TabPage();

            tpage.BorderStyle = BorderStyle.Fixed3D;

            browserTabControl.TabPages.Insert(browserTabControl.TabCount - 1, tpage);

           WebBrowser browser = new WebBrowser();

            browser.Navigate(homePage);  

            tpage.Controls.Add(browser);

            browser.Dock = DockStyle.Fill;

            browserTabControl.SelectTab(tpage);

            browser.ProgressChanged += new WebBrowserProgressChangedEventHandler(Form1_ProgressChanged);

            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Form1_DocumentCompleted);

            browser.Navigating += new WebBrowserNavigatingEventHandler(Form1_Navigating);

            browser.CanGoBackChanged += new EventHandler(browser_CanGoBackChanged);

            browser.CanGoForwardChanged += new EventHandler(browser_CanGoForwardChanged);

          

        }

 

      

        //DocumentCompleted

        private void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {

            WebBrowser currentBrowser = getCurrentBrowser();

            this.toolStripStatusLabel1.Text = "Done";

            String text = "Blank Page";

 

            if (!currentBrowser.Url.ToString().Equals("about:blank"))

            {

                text = currentBrowser.Url.Host.ToString();

            }

 

            this.adrBarTextBox.Text = currentBrowser.Url.ToString();

            browserTabControl.SelectedTab.Text = text;

 

            img.Image = favicon(currentBrowser.Url.ToString(), "net.png");

 

            if (!urls.Contains(currentBrowser.Url.Host.ToString()))

                urls.Add(currentBrowser.Url.Host.ToString());

 

            if (!currentBrowser.Url.ToString().Equals("about:blank") && currentBrowser.StatusText.Equals("Done"))

                addHistory(currentBrowser.Url,DateTime.Now.ToString(currentCulture));

        }

        //ProgressChanged   

        private void Form1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)

        {

            if (e.CurrentProgress < e.MaximumProgress)

                toolStripProgressBar1.Value=(int)e.CurrentProgress;

            else toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;

 

        }

        //Navigating

        private void Form1_Navigating(object sender, WebBrowserNavigatingEventArgs e)

        {

            this.toolStripStatusLabel1.Text = getCurrentBrowser().StatusText;

 

        }

        //closeTab method

        private void closeTab()

        {

            if (browserTabControl.TabCount != 2)

            {

                browserTabControl.TabPages.RemoveAt(browserTabControl.SelectedIndex);

            }

 

        }

        //selected index changed

        private void browserTabControl_SelectedIndexChanged(object sender, EventArgs e)

        {

            if (browserTabControl.SelectedIndex == browserTabControl.TabPages.Count - 1) addNewTab();

            else

            {

                if (getCurrentBrowser().Url != null)

                    adrBarTextBox.Text = getCurrentBrowser().Url.ToString();

                else adrBarTextBox.Text = "about:blank";

 

                if (getCurrentBrowser().CanGoBack) toolStripButton1.Enabled = true;

                else toolStripButton1.Enabled = false;

 

                if (getCurrentBrowser().CanGoForward) toolStripButton2.Enabled = true;

                else toolStripButton2.Enabled = false;

            }

        }

 

        /* tab context menu */

 

        private void closeTabToolStripMenuItem1_Click(object sender, EventArgs e)

        {

            closeTab();

        }

        private void duplicateTabToolStripMenuItem1_Click(object sender, EventArgs e)

        {

            if (getCurrentBrowser().Url != null)

            {

                Uri dup_url = getCurrentBrowser().Url;

                addNewTab();

                getCurrentBrowser().Url = dup_url;

 

            }

            else addNewTab();

        }

        #endregion

 

        #region FAVICON

      

        // favicon

        public static Image favicon(String u, string file)

        {

                Uri url = new Uri(u);

                String iconurl = "http://" + url.Host + "/favicon.ico";

 

                WebRequest request = WebRequest.Create(iconurl);

                try

                {

                    WebResponse response = request.GetResponse();

 

                    Stream s = response.GetResponseStream();

                    return Image.FromStream(s);

                }

                catch (Exception ex)

                {

                    return Image.FromFile(file);

                }

           

          

        }

        //favicon index

        private int faviconIndex(string url)

        {

            Uri key = new Uri(url);

            if (!imgList.Images.ContainsKey(key.Host.ToString()))

                imgList.Images.Add(key.Host.ToString(), favicon(url, "link.png"));

            return imgList.Images.IndexOfKey(key.Host.ToString());

        }

        //getFavicon from key

        private Image getFavicon(string key)

        {

            Uri url = new Uri(key);

            if (!imgList.Images.ContainsKey(url.Host.ToString()))

                imgList.Images.Add(url.Host.ToString(), favicon(key

                    , "link.png"));

            return imgList.Images[url.Host.ToString()];

        }

        #endregion

 

        #region     TOOL CONTEXT MENU

        /* TOOL CONTEXT MENU*/

 

        //link bar

        private void linksBarToolStripMenuItem_Click(object sender, EventArgs e)

        {

            linkBar.Visible = !linkBar.Visible;

            this.linksBarToolStripMenuItem.Checked = linkBar.Visible;

            settings.DocumentElement.ChildNodes[2].Attributes[0].Value = linkBar.Visible.ToString();

        }

        //menu bar

        private void menuBarToolStripMenuItem_Click(object sender, EventArgs e)

        {

            menuBar.Visible = !menuBar.Visible;

            this.menuBarToolStripMenuItem.Checked = menuBar.Visible;

            settings.DocumentElement.ChildNodes[0].Attributes[0].Value = menuBar.Visible.ToString();

        }

        //address bar

        private void commandBarToolStripMenuItem_Click(object sender, EventArgs e)

        {

            adrBar.Visible = !adrBar.Visible;

            this.commandBarToolStripMenuItem.Checked = adrBar.Visible;

            settings.DocumentElement.ChildNodes[1].Attributes[0].Value = adrBar.Visible.ToString();

        }

        #endregion

 

        #region ADDRESS BAR

        /*ADDRESS BAR*/

 

        private WebBrowser getCurrentBrowser()

        {

            return (WebBrowser)browserTabControl.SelectedTab.Controls[0];

        }

        //ENTER

        private void adrBarTextBox_KeyDown(object sender, KeyEventArgs e)

        {

            if (e.KeyCode == Keys.Enter)

            {

                getCurrentBrowser().Navigate(adrBarTextBox.Text);

 

            }

        }

        //select all from adr bar

        private void adrBarTextBox_Click(object sender, EventArgs e)

        {

            adrBarTextBox.SelectAll();

        }

        //show urls

 

        private void showUrl()

        {

            if (File.Exists(historyXml))

            {

                XmlDocument myXml = new XmlDocument();

                myXml.Load(historyXml);

                int i = 0;

                int num=int.Parse(settings.DocumentElement.ChildNodes[6].InnerText.ToString());

                foreach (XmlElement el in myXml.DocumentElement.ChildNodes)

                {

                    if (num <= i++ ) break;

                    else  adrBarTextBox.Items.Add(el.GetAttribute("url").ToString());

                          

                }

            }

        }

 

        private void adrBarTextBox_DropDown(object sender, EventArgs e)

        {

            adrBarTextBox.Items.Clear();

            showUrl();

        }

        //navigate on selected url

        private void adrBarTextBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            getCurrentBrowser().Navigate(adrBarTextBox.SelectedItem.ToString());

        }

     //canGoForwardChanged

        void browser_CanGoForwardChanged(object sender, EventArgs e)

        {

            toolStripButton2.Enabled = !toolStripButton2.Enabled;

        }

        //canGoBackChanged

        void browser_CanGoBackChanged(object sender, EventArgs e)

        {

            toolStripButton1.Enabled = !toolStripButton1.Enabled;

        }

        //back 

        private void toolStripButton1_Click(object sender, EventArgs e)

        {

            getCurrentBrowser().GoBack();

        }

        //forward

        private void toolStripButton2_Click(object sender, EventArgs e)

        {

            getCurrentBrowser().GoForward();

        }

        //go

        private void toolStripButton3_Click(object sender, EventArgs e)

        {

            getCurrentBrowser().Navigate(adrBarTextBox.Text);

 

        }

        //refresh

        private void toolStripButton4_Click(object sender, EventArgs e)

        {

            getCurrentBrowser().Refresh();

        }

        //stop

        private void toolStripButton5_Click(object sender, EventArgs e)

        {

            getCurrentBrowser().Stop();

        }

        //favorits

        private void toolStripButton7_Click(object sender, EventArgs e)

        {

            favoritesPanel.Visible = !favoritesPanel.Visible;

            settings.DocumentElement.ChildNodes[3].Attributes[0].Value = favoritesPanel.Visible.ToString();

        }

        //add to favorits

        private void toolStripButton8_Click(object sender, EventArgs e)

        {

            if (getCurrentBrowser().Url != null)

            {

                AddFavorites dlg = new AddFavorites(getCurrentBrowser().Url.ToString());

                DialogResult res = dlg.ShowDialog();

 

                if (res == DialogResult.OK)

                {

                    if (dlg.favFile == "Favorites")

                        addFavorit(getCurrentBrowser().Url.ToString(), dlg.favName);

                    else addLink(getCurrentBrowser().Url.ToString(), dlg.favName);

                }

                dlg.Close();

            }

 

        }

        //search

        private void searchTextBox_KeyDown(object sender, KeyEventArgs e)

        {

            if (e.KeyCode == Keys.Enter)

                if (googleSearch.Checked == true)

                    getCurrentBrowser().Navigate("http://google.com/search?q=" + searchTextBox.Text);

                else

                    getCurrentBrowser().Navigate("http://search.live.com/results.aspx?q="+searchTextBox.Text);                      

        }

 

        private void googleSearch_Click(object sender, EventArgs e)

        {

            liveSearch.Checked =!googleSearch.Checked;

        }

 

        private void liveSearch_Click(object sender, EventArgs e)

        {

            googleSearch.Checked = !liveSearch.Checked;

        }

 

        #endregion

 

        #region LINKS BAR

 

        /*LINKS BAR*/

 

        string adress, name;

 

        //favorits button

        private void toolStripButton6_Click(object sender, EventArgs e)

        {

            favoritesPanel.Visible = !favoritesPanel.Visible;

            settings.DocumentElement.ChildNodes[3].Attributes[0].Value = favoritesPanel.Visible.ToString();

        }

        //add to favorits bar button

        private void toolStripButton9_Click(object sender, EventArgs e)

        {

            if (getCurrentBrowser().Url != null)

                addLink(getCurrentBrowser().Url.ToString(), getCurrentBrowser().Url.ToString());

        }

 

        //showLinks on link bar

        private void showLinks()

        {

            if (File.Exists(linksXml))

            {

                XmlDocument myXml = new XmlDocument();

                myXml.Load(linksXml);

                XmlElement root = myXml.DocumentElement;

                foreach (XmlElement el in root.ChildNodes)

                {

                    ToolStripButton b =

                        new ToolStripButton(el.InnerText, getFavicon(el.GetAttribute("url")), items_Click, el.GetAttribute("url"));

 

                    b.ToolTipText = el.GetAttribute("url");

                    b.MouseUp += new MouseEventHandler(b_MouseUp);

                    linkBar.Items.Add(b);

                }

            }

        }

        //click link button

        private void items_Click(object sender, EventArgs e)

        {

            ToolStripButton b = (ToolStripButton)sender;

            getCurrentBrowser().Navigate(b.ToolTipText);

        }

        //show context menu on button

        private void b_MouseUp(object sender, MouseEventArgs e)

        {

            ToolStripButton b = (ToolStripButton)sender;

            adress = b.ToolTipText;

            name = b.Text;

 

            if (e.Button == MouseButtons.Right)

                linkContextMenu.Show(MousePosition);

        }

//visible change

        private void linkBar_VisibleChanged(object sender, EventArgs e)

        {

            if (linkBar.Visible == true) showLinks();

            else while (linkBar.Items.Count > 3) linkBar.Items[linkBar.Items.Count - 1].Dispose();

        }

 

        #endregion

 

        #region LINK, FAVORITES, HISTORY CONTEXT MENU

        /*GENERAL*/

 

        //open

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)

        {

            getCurrentBrowser().Navigate(adress);

        }

        //open in new tab

        private void openInNewTabToolStripMenuItem_Click(object sender, EventArgs e)

Информация о работе Як отримати значок веб-сайту