分类目录归档:小玩意

c#(.net)基于WebBrowser开发的简单的浏览器

用webbrowser控件做的浏览器,界面仿ie9,虽然很丑,懒得贴图,包含收藏夹功能(网上找的,研究中····),其他都是些webbrowser的方法,Internet选项用的是命令直接打开的方式,添加到收藏夹用的是创建快捷方式的方法实现的。

附上代码:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IWshRuntimeLibrary;
using Microsoft.Win32;
using System.IO;
namespace MyBrowser
{
    public partial class MainForm : Form
    {

        string url = "www.baidu.com";

        string favorfolder = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
        public MainForm()
        {
            InitializeComponent();
            this.webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);
            ListMenuItem(收藏夹ToolStripMenuItem, new DirectoryInfo(favorfolder));
            // Refresh();
        }

        public void ListMenuItem(ToolStripMenuItem toolstripmenuitem, FileSystemInfo info)             //生成收藏夹菜单的函数,递归使用
        {
            if (!info.Exists) return;
            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录
            if (dir == null) return;

            FileSystemInfo[] files = dir.GetFileSystemInfos();
            ToolStripMenuItem[] ShouCangsMenuItem = new ToolStripMenuItem[files.Length];
            for (int i = 0; i < files.Length; i++)
            {

                FileInfo file = files[i] as FileInfo;
                ShouCangsMenuItem[i] = new System.Windows.Forms.ToolStripMenuItem();

                //是文件
                if (file != null)
                {
                    if (file.Extension == ".url")
                    {
                        //ShouCangsMenuItem[i].Image = global::MyBrowser.Properties.Resources.ie1;//菜单项图片
                        string str = file.Name;             //获取收藏夹的文件名(都是URL文件)
                        str = str.Remove(str.Length - 4);     //去掉.url后缀名

                        ShouCangsMenuItem[i].Text = str;          //然后赋值给菜单文本

                        StreamReader sr = file.OpenText();//获取文件输入流

                        List src = new List();

                        string source = null;
                        while ((source = sr.ReadLine()) != null)
                        {
                            src.Add(source);//文件所有行添加到List中
                        }

                        ShouCangsMenuItem[i].Tag = src;

                        foreach (string stri in src)
                        {
                            if (stri != null)
                            {
                                if (stri.StartsWith("URL="))
                                {
                                    ShouCangsMenuItem[i].ToolTipText = stri.Remove(0, 4);//ToolTipText
                                }
                            }
                        }

                        toolstripmenuitem.DropDownItems.Add(ShouCangsMenuItem[i]);   //生成的子菜单添加到上一级菜单
                        ShouCangsMenuItem[i].Click += new EventHandler(ShouCangsMenuItem_Click);          //为生成的子菜单添加单击消息(可写一个消息处理函数,这里就不例出来了)
                    }
                }
                //对于子目录,进行递归调用
                else
                {
                    DirectoryInfo Direct = files[i] as DirectoryInfo;
                    ShouCangsMenuItem[i].Text = files[i].Name;
                    //ShouCangsMenuItem[i].Image = global::MyBrowser.Properties.Resources.folder2;//菜单项图片
                    ShouCangsMenuItem[i].ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
                    ShouCangsMenuItem[i].ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.SizeToFit;
                    toolstripmenuitem.DropDownItems.Add(ShouCangsMenuItem[i]);       //生成的子菜单添加到上一级菜单
                    ListMenuItem(ShouCangsMenuItem[i], new DirectoryInfo(Direct.FullName));   //递归使用,生成子菜单
                }
            }
        }

        private void ShouCangsMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            string[] tar = ((List)item.Tag).ToArray();
            for (int i = 0; i < tar.Length; i++)
            {
                if (tar[i] != null)
                {
                    if (tar[i].StartsWith("URL="))
                    {
                        webBrowser1.Navigate(tar[i].Remove(0, 4));
                    }
                }
            }
        }

        /////////////////////////////底部信息/////////////////////////////////
        private void webBrowser1_StatusTextChanged(Object obj, EventArgs e)
        {
            this.label1.Text = webBrowser1.StatusText;
        }
        private void buttongo_Click(object sender, EventArgs e)
        {
            url = this.text_url.Text;
            this.webBrowser1.Navigate(url);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Random rd = new Random();
            this.BackColor = Color.FromArgb(rd.Next(255), rd.Next(255), rd.Next(255));//随机背景颜色

            this.webBrowser1.Width = base.Width - 10;
            this.webBrowser1.Height = base.Height - 80;
            webBrowser1.GoHome();

            Invalidate();
        }

        private void button_home_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoHome();
        }

        private void button_fresh_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Refresh();
        }

        private void button_back_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoBack();
        }

        private void button_forward_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoForward();
        }
        /////////////////////////////回车事件/////////////////////////////////
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                url = this.text_url.Text;
                this.webBrowser1.Navigate(url);
            }
        }

        private void text_url_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                url = this.text_url.Text;
                this.webBrowser1.Navigate(url);
            }
        }
        ////////////////////////////////////////////////////////////////////

        private void Form1_Resize(object sender, EventArgs e)
        {
            //this.webBrowser1.Width = base.Width-10;
            //this.webBrowser1.Height = base.Height - 95;
            this.panel1.Width = base.Width - 10;
            this.panel1.Height = base.Height - 90;
            this.toolStrip1.Location = new Point(base.Width - 60, this.toolStrip1.Location.Y);
            this.button_go.Location = new Point(base.Width - 193, this.button_go.Location.Y);
            this.text_url.Size = new Size(base.Width - 300, this.text_url.Size.Height);

        }

        private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowSaveAsDialog();
        }

        private void 查找ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            webBrowser1.GoSearch();
        }

        private void 页面属性ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPropertiesDialog();
        }

        private void 打印ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintDialog();
        }

        private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPrintPreviewDialog();
        }

        private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            webBrowser1.ShowPageSetupDialog();
        }

        private void 关于MyToolStripMenuItem_Click(object sender, EventArgs e)
        {

            MessageBox.Show("My Browser made by DormyMo  \n www.7mdm.com \n \nQQ:440056060\n\n" + "ie版本" + webBrowser1.Version.ToString());
        }

        private void internet选项ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("inetcpl.cpl");

        }
        /////////////////////////////设置按钮效果/////////////////////////////////
        private void toolStripDropDownButton1_MouseEnter(object sender, EventArgs e)
        {
            this.toolStripDropDownButton1.Image = MyBrowser.Properties.Resources.shezhi2;
        }

        private void toolStripDropDownButton1_MouseLeave(object sender, EventArgs e)
        {
            this.toolStripDropDownButton1.Image = MyBrowser.Properties.Resources.shezhi;
        }
        //////////////////////////////////////////////////////////////////////////

        /////////////////////////////缩放/////////////////////////////////
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.Body.Style = "zoom:0.5";
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.Body.Style = "zoom:0.75";
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.Body.Style = "zoom:1.0";
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.Body.Style = "zoom:1.25";
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.Body.Style = "zoom:1.5";
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.Body.Style = "zoom:2.0";
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Document.Body.Style = "zoom:4.0";
        }
        //////////////////////////////////////////////////////////////

        /////////////////////////////显示网页标题/////////////////////////////////
        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            this.Text = this.webBrowser1.DocumentTitle + "                               made by DormyMo";
        }
        /////////////////////////////新窗口在原始窗口打开/////////////////////////////////
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)//当发生新窗口打开事件时
        {
            WebBrowser webBrowser_temp = (WebBrowser)sender;
            string newUrl = webBrowser_temp.Document.ActiveElement.GetAttribute("href");//截取url
            webBrowser1.Url = new Uri(newUrl);//把url传给webbrowser1
            e.Cancel = true; //取消ie操作
        }

        /////////////////////////////收藏夹按钮效果/////////////////////////////////
        private void toolStripDropDownButton2_MouseEnter(object sender, EventArgs e)
        {
            this.toolStripDropDownButton2.Image = MyBrowser.Properties.Resources.star2;
        }

        private void toolStripDropDownButton2_MouseLeave(object sender, EventArgs e)
        {
            this.toolStripDropDownButton2.Image = MyBrowser.Properties.Resources.star;
        }
        //////////////////////////////////////////////////////////////////////////

        private void 添加到收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IWshShell_Class shell = new IWshShell_Class();
            IWshURLShortcut shortcut = null;
            string filename = this.webBrowser1.DocumentTitle;
            shortcut = shell.CreateShortcut(Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites) + "\\" + filename + ".url") as IWshURLShortcut;
            shortcut.TargetPath = webBrowser1.Url.AbsoluteUri;

            shortcut.Save();
        }

        private void 百度一下ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            this.webBrowser1.Navigate("www.baidu.com");
        }

        private void 打开收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(favorfolder);
        }

        private void 设为主页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string key = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main";
            string valueName = "Start Page";
            string value = webBrowser1.Url.AbsoluteUri;
            DialogResult Confirm = MessageBox.Show("你确定要执行修改主页的操作吗!?", "通过注册表来修改Internet Explore的主页", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (Confirm == DialogResult.OK)
            {
                Registry.SetValue(key, valueName, value);
                MessageBox.Show("你的浏览器主页已被更改为:" + webBrowser1.Url.AbsoluteUri, "恭喜您", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
    }
}

【源文件】

c#数字时间控件

.net上课作业,还是满简单的。

附上控件的代码:

 

/////控件的timer计时器事件/////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace timer
{
    public partial class showtime : UserControl
    {
        
        public showtime()
        {
            InitializeComponent();
            this.timer1.Start();  //启动计时器

            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            label1.Text = System.DateTime.Now.ToString()+":"+System.DateTime.Now.Millisecond.ToString();;

        }
       public int timerate    //设置属性
        {
            get { return this.timer1.Interval; }
            set { this.timer1.Interval = value; }

        }
        
        
       
        
    }
}