那些年刚学 C#——搜壶浏览器(二)

这里只是主要写一些功能的实现

image

无边框窗体

首先设置窗体FormStyle属性 为none,设完就是什么都没有了,包括最大化最小化关闭都没了,这样你就可以自定义这些按钮了。但是无边框后会发现各种问题,比如无法拖动,无法缩放窗体,下面的代码将用于解决这一系列问题。

1、拖动和缩放无边框窗体:

View Code
const int WM_NCHITTEST = 0x0084;
        const int HT_LEFT = 10;
        const int HT_RIGHT = 11;
        const int HT_TOP = 12;
        const int HT_TOPLEFT = 13;
        const int HT_TOPRIGHT = 14;
        const int HT_BOTTOM = 15;
        const int HT_BOTTOMLEFT = 16;
        const int HT_BOTTOMRIGHT = 17;
        const int HT_CAPTION = 2;
        protected override void WndProc(ref Message Msg)
        {

            if (Msg.Msg == WM_NCHITTEST)
            {
                //获取鼠标位置
                int nPosX = (Msg.LParam.ToInt32() & 65535);
                int nPosY = (Msg.LParam.ToInt32() >> 16);
                //右下角
                if (nPosX >= this.Right - 6 && nPosY >= this.Bottom - 6)
                {
                    Msg.Result = new IntPtr(HT_BOTTOMRIGHT);
                    return;
                }
                //左上角
                else if (nPosX <= this.Left + 6 && nPosY <= this.Top + 6)
                {
                    Msg.Result = new IntPtr(HT_TOPLEFT);
                    return;
                }
                //左下角
                else if (nPosX <= this.Left + 6 && nPosY >= this.Bottom - 6)
                {
                    Msg.Result = new IntPtr(HT_BOTTOMLEFT);
                    return;
                }
                //右上角
                else if (nPosX >= this.Right - 6 && nPosY <= this.Top + 6)
                {
                    Msg.Result = new IntPtr(HT_TOPRIGHT);
                    return;
                }
                else if (nPosX >= this.Right - 2)
                {
                    Msg.Result = new IntPtr(HT_RIGHT);
                    return;
                }
                else if (nPosY >= this.Bottom - 2)
                {
                    Msg.Result = new IntPtr(HT_BOTTOM);
                    return;
                }
                else if (nPosX <= this.Left + 2)
                {
                    Msg.Result = new IntPtr(HT_LEFT);
                    return;
                }
                else if (nPosY <= this.Top + 2)
                {
                    Msg.Result = new IntPtr(HT_TOP);
                    return;
                }
                else
                {
                    Msg.Result = new IntPtr(HT_CAPTION);
                    return;
                }
            }
            base.WndProc(ref Msg);


        }

2、圆弧边角及边框阴影效果:

View Code
View Code
        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 10);
            this.Region = new Region(FormPath);

        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }



        /// <summary>
        /// 当窗口改变时调动SetWindowsRegion函数改变窗口形象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_CYHBass_Resize(object sender, EventArgs e)
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            this.Refresh();
            SetWindowRegion();
        }

3、窗体打开关闭动画:

View Code

自定义关闭按钮

1、鼠标悬停图标变化:(先设置按钮的ImaleList对象并放入图标)

        private void btn_Close_MouseHover(object sender, EventArgs e)
        {
            this.btn_Close.ImageIndex = 2;
        }

        private void btn_Close_MouseLeave(object sender, EventArgs e)
        {
            this.btn_Close.ImageIndex = 0;
        }

2、关闭事件:

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.btn_Close.ImageIndex = 1;
            AniWindow a = new AniWindow(this.Handle, 7, 200, this);//从四周向中间隐藏
            this.Close();
        }

鼠标悬停窗体指定位置展开

clip_image001

1、只打开一个窗体:

  if (Application.OpenForms["frm_mMenu"] == null)
            {
//打开窗体
}

2、指定位置显示:

View Code
  if (Application.OpenForms["frm_mMenu"] == null)
            {
                Frm_mMenu frm_mMenu = new Frm_mMenu();
                frm_mMenu.Location = new Point(btn_Menu.Location.X
                    + Frm_Main.pMainWin.Location.X - frm_mMenu.Width, btn_Menu.Location.Y
                    + Frm_Main.pMainWin.Location.Y + btn_Menu.Height);
                frm_mMenu.Show();
            }

3、换肤窗体从右上至左下展开:

Frm_mMenu 的Load事件(AniWindow 代码上面有)

 private void Frm_mMenu_Load(object sender, EventArgs e)
        {
            AniWindow a = new AniWindow(this.Handle, 10, 200, this);//从左上角扩展效果         
        }

皮肤的实现

clip_image001

其实我的皮肤就是一张背景图片

1、皮肤切换:

View Code
        /// <summary>
        /// 更换背景图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Select_Pic_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件(*.jpg,*.gif,*.bmp,*.png)|*.jpg;*.gif;*.bmp;*.png";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                /*
                 *复制图片到根目录的skin文件夹命名为user_bg.jpg
                 *将图片路径存储到配置文件 
                 *待续....
                 */
                //if (coverFile(openFileDialog1.FileName,System.Windows.Forms.Application.StartupPath+@"\skin\bg\0.jpg"))
                //{
                selectBp[0] = new Bitmap(openFileDialog1.FileName);
                this.BackgroundImage = selectBp[0];
                if (Application.OpenForms["frm_Main"] != null)
                {
                    foreach (Form f in Application.OpenForms)
                    {
                        f.BackColor = this.BackColor;
                        f.BackgroundImage = selectBp[0];
                    }
                    skin.setBgConfig(0);
                }
                openFileDialog1.Dispose();
                //}
                //else
                //{
                //    MessageBox.Show("设置失败!");
                //}
            }
        }

2、配置保存:

这里我只是把图片覆盖到目标图片实现,于是经常报只读错误,因为读取用的是Image.From 但是它会锁定图片。

View Code

3、根据背景图片颜色设置不同文本颜色:

这个比较有用啊,我后面做的很多项目中也用到过。

View Code
     public Frm_mMenu()
        {
            InitializeComponent();
            //this.BackgroundImage = Frm_Main.pMainWin.BackgroundImage;
            if (Frm_Main.pMainWin.BackgroundImage != null)
            {
                Bitmap bitmap = new Bitmap(Frm_Main.pMainWin.BackgroundImage);
                this.BackColor = bitmap.GetPixel(20, 15);
            }
        }

透明框提示

clip_image005

1、自定义控件?

不是,那时候还不懂自定义控件,直接用窗体代替了。关于窗体定位和单一显示和上面原理一样的。其中还糊里糊涂用到了GDI透明处理。后面才知道这就是传说中的不规则非矩形窗体的实现。

“控件”代码如下:

View Code
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using 搜壶浏览器.CYH_CL;

namespace 搜壶浏览器.CYH_UI
{
    public partial class Frm_Message : Form
    {
        public static bool IsClose;
        public string strMg = "";

        public Frm_Message()
        {
            InitializeComponent();
        }

        private void Frm_Message_Load(object sender, EventArgs e)
        {
            this.lbl_Message.Text = strMg;
            IsClose = false;
            timer_Close.Start();
            Bitmap img = (Bitmap)this.BackgroundImage;
            GraphicsPath grapth = GetNoneTransparentRegion(img, 200);
            this.Region = new Region(grapth);

            AniWindow a = new AniWindow(this.Handle, 10, 200, this);//透明渐变显示
        }
        /// <summary>
        /// 返回指定图片中的非透明区域;
        /// </summary>
        /// <param name="img">位图</param>
        /// <param name="alpha">alpha 小于等于该值的为透明</param>
        /// <returns></returns>
        public static GraphicsPath GetNoneTransparentRegion(Bitmap img, byte alpha)
        {
            int height = img.Height;
            int width = img.Width;

            int xStart, xEnd;
            GraphicsPath grpPath = new GraphicsPath();
            for (int y = 0; y < height; y++)
            {
                //逐行扫描;
                for (int x = 0; x < width; x++)
                {
                    //略过连续透明的部分;
                    while (x < width && img.GetPixel(x, y).A <= alpha)
                    {
                        x++;
                    }
                    //不透明部分;
                    xStart = x;
                    while (x < width && img.GetPixel(x, y).A > alpha)
                    {
                        x++;
                    }
                    xEnd = x;
                    if (img.GetPixel(x - 1, y).A > alpha)
                    {
                        grpPath.AddRectangle(new Rectangle(xStart, y, xEnd - xStart, 1));
                    }
                }
            }
            return grpPath;

        }

        private void Frm_Message_MouseLeave(object sender, EventArgs e)
        {
            //如果鼠标不在窗体上
            if (!this.Bounds.Contains(Cursor.Position))
            {
                this.Dispose();
                this.Close();
            }
        }

        private void timer_Close_Tick(object sender, EventArgs e)
        {
            if (IsClose)
            {
                timer_Close.Stop();
                this.Dispose();
                this.Close();
            }
        }


    }
}

2、调用“控件”方法:

View Code

文档搜索功能实现

clip_image002

1、搜文档:

好吧,这个很简单,就是 加入了 搜索引擎 搜索规则,比如:搜word文档,百度框输入"关键字+fileType:doc" ,可以选择不同的搜索引擎。

2、指定网站:

同上类似 "搜索关键字+site:网站域名"

View Code
/// <summary>
        /// 高级搜索按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Search_Exp_Click(object sender, EventArgs e)
        {
            StringBuilder strB = new StringBuilder();
            strB.Append(setting.getSearch_Engine());

            if (rdBtn_DOC.Checked)
            {
                strB = strB.Replace("CYH", "CYH filetype:DOC");
            }
            if (rdBtn_PPT.Checked)
            {
                strB = strB.Replace("CYH", "CYH filetype:PPT");
            }
            if (rdnBtn_PDF.Checked)
            {
                strB = strB.Replace("CYH", "CYH filetype:PDF");
            }
            if (txtBox_Site.Text.Contains("."))
            {
                strB.Append(" site:" + txtBox_Site.Text.Trim());
            }
            //WebBrowserArr[tabMain.SelectedIndex].GoSearch();
            //            C# Url传递中文 转义编码
            //百度的编码
            //System.Web.HttpUtility.UrlEncode( "中文 ", System.Text.UnicodeEncoding.GetEncoding( "GB2312 ")).ToUpper()

            //Google的编码
            //System.Web.HttpUtility.UrlEncode( "中文 ")


            //得到值
            //System.Web.HttpUtility.UrlDecode( "%BA%AB%B9%FA%D3%EF%D1%A7%CF%B0%D7%CA%C1%CF ", System.Text.UnicodeEncoding.GetEncoding( "GB2312 "))
    
            NavToUrl(strB.Replace("CYH", cmbBox_SearchTxt.Text.Trim()).ToString());
            //保存搜索历史
            setting.saveSearchHis(this.cmbBox_SearchTxt.Text.Trim());

        }

浏览器设置

clip_image003

1、启动页:

2、主页:

3、搜索引擎:

View Code
View Code
  public void setHomepages(string value)
        {
            string strPath = System.Windows.Forms.Application.StartupPath + @"\Config\HomPgsConfig.txt";
            FileInfo finfo = new FileInfo(strPath);
            if (finfo.Exists)
            {
                ///删除该文件
                finfo.Delete();
            }
            WriteFile(strPath, "HomePage", value, "*");
        }
        public string getHomepages()
        {
            string strPath = System.Windows.Forms.Application.StartupPath + @"\Config\HomPgsConfig.txt";
            string txt = ReadFile(strPath);
            if (txt.Length < 1)
            {
                return "about:blank";
            }
            else
            {
                return txt.Split('*')[1].Trim().ToString();
            }
        }

        public void setStartPage(string value)
        {
            string strPath = System.Windows.Forms.Application.StartupPath + @"\Config\StPgConfig.txt";
            FileInfo finfo = new FileInfo(strPath);
            if (finfo.Exists)
            {
                ///删除该文件
                finfo.Delete();
            }
            WriteFile(strPath, "StartPage", value, "*");
        }
        public string getStartPage()
        {
            string strPath = System.Windows.Forms.Application.StartupPath + @"\Config\StPgConfig.txt";
            string txt = ReadFile(strPath);
            if (txt.Length < 1)
            {
                return "about:blank";
            }
            else
            {
                return txt.Split('*')[1].Trim().ToString();
            }
        }

当文件大于某值时删除

View Code

那时候没学XML,网上例子也看不懂所以最终还是用文本+占位符方式保存配置的。

 clip_image008clip_image009

Internet选项

1、调用系统Internet选项:

      private void btn_IE_Option_Click(object sender, EventArgs e)
        {
            //Frm_Main.pMainWin.WebBrowserArr[Frm_Main.pMainWin.TabSelectedIndex].ShowSaveAsDialog();
            //调用IE的Internet选项
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = new System.Diagnostics.ProcessStartInfo("rundll32.exe", "shell32.dll,Control_RunDLL inetcpl.cpl");
            p.Start();           
        }

2、获取IE主页:

View Code

浏览网页

1、Uri格式处理:

View Code

2、历史记录:

第一次用上xml,而且是抄别人的,什么东西都不敢改。

View Code

3、多标签:

这个。

//WebBrowser数组
        public List<ExtendedWebBrowser> WebBrowserArr;

4、加载Logo:

这种写法显然不靠谱,不过那时候就是这么通过访问两次实现的。 

View Code
/// <summary>
        /// 获取网站的标题图片
        /// </summary>
        /// <param name="strUrl">网址</param>
        /// <returns></returns>
        private Image GetFavicon(string strUrl)
        {
            var uri = new Uri(strUrl);
            HttpWebRequest vHttpWebRequest = (HttpWebRequest)WebRequest.Create(
                @"http://" + uri.Host + @"/favicon.ico");
            HttpWebResponse vHttpWebResponse = (HttpWebResponse)vHttpWebRequest.GetResponse();
            Image vFavicon = Image.FromStream(vHttpWebResponse.GetResponseStream());
            vHttpWebResponse.Close();
            if (vFavicon != null)
            {
                return vFavicon;
            }
            else
            {
                return imgLstfavicon.Images[0];

            }

        }

下载管理

clip_image006

1、下载实现:

那时候虽然刚接触Winform但是网上找个下载类还是很容易的。这个类还很强大的,不过我只用了最简单的功能。

View Code
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Security;
using System.Threading;
using System.Collections.Specialized;

namespace 搜壶浏览器.CYH_UI
{
    /// <summary>
    /// 记录下载的字节位置
    /// </summary>
    public class DownLoadState
    {
        private string _FileName;
        private string _AttachmentName;
        private int _Position;
        private string _RequestURL;
        private string _ResponseURL;
        private int _Length;
        private byte[] _Data;
        public string FileName
        {
            get
            {
                return _FileName;
            }
        }
        public int Position
        {
            get
            {
                return _Position;
            }
        }
        public int Length
        {
            get
            {
                return _Length;
            }
        }
        public string AttachmentName
        {
            get
            {
                return _AttachmentName;
            }
        }
        public string RequestURL
        {
            get
            {
                return _RequestURL;
            }
        }
        public string ResponseURL
        {
            get
            {
                return _ResponseURL;
            }
        }
        public byte[] Data
        {
            get
            {
                return _Data;
            }
        }
        internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length, byte[] Data)
        {
            this._FileName = FileName;
            this._RequestURL = RequestURL;
            this._ResponseURL = ResponseURL;
            this._AttachmentName = AttachmentName;
            this._Position = Position;
            this._Data = Data;
            this._Length = Length;
        }
        internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length, ThreadCallbackHandler tch)
        {
            this._RequestURL = RequestURL;
            this._ResponseURL = ResponseURL;
            this._FileName = FileName;
            this._AttachmentName = AttachmentName;
            this._Position = Position;
            this._Length = Length;
            this._ThreadCallback = tch;
        }
        internal DownLoadState(string RequestURL, string ResponseURL, string FileName, string AttachmentName, int Position, int Length)
        {
            this._RequestURL = RequestURL;
            this._ResponseURL = ResponseURL;
            this._FileName = FileName;
            this._AttachmentName = AttachmentName;
            this._Position = Position;
            this._Length = Length;
        }
        private ThreadCallbackHandler _ThreadCallback;
        public HttpWebClient httpWebClient
        {
            get
            {
                return this._hwc;
            }
            set
            {
                this._hwc = value;
            }
        }
        internal Thread thread
        {
            get
            {
                return _thread;
            }
            set
            {
                _thread = value;
            }
        }
        private HttpWebClient _hwc;
        private Thread _thread;
        //
        internal void StartDownloadFileChunk()
        {
            if (this._ThreadCallback != null)
            {
                this._ThreadCallback(this._RequestURL, this._FileName, this._Position, this._Length);
                this._hwc.OnThreadProcess(this._thread);
            }
        }
    }
    //委托代理线程的所执行的方法签名一致
    public delegate void ThreadCallbackHandler(string S, string s, int I, int i);
    //异常处理动作
    public enum ExceptionActions
    {
        Throw,
        CancelAll,
        Ignore,
        Retry
    }
    /// <summary>
    /// 包含 Exception 事件数据的类
    /// </summary>
    public class ExceptionEventArgs : System.EventArgs
    {
        private System.Exception _Exception;
        private ExceptionActions _ExceptionAction;
        private DownLoadState _DownloadState;
        public DownLoadState DownloadState
        {
            get
            {
                return _DownloadState;
            }
        }
        public Exception Exception
        {
            get
            {
                return _Exception;
            }
        }
        public ExceptionActions ExceptionAction
        {
            get
            {
                return _ExceptionAction;
            }
            set
            {
                _ExceptionAction = value;
            }
        }
        internal ExceptionEventArgs(System.Exception e, DownLoadState DownloadState)
        {
            this._Exception = e;
            this._DownloadState = DownloadState;
        }
    }
    /// <summary>
    /// 包含 DownLoad 事件数据的类
    /// </summary>
    public class DownLoadEventArgs : System.EventArgs
    {
        private DownLoadState _DownloadState;
        public DownLoadState DownloadState
        {
            get
            {
                return _DownloadState;
            }
        }
        public DownLoadEventArgs(DownLoadState DownloadState)
        {
            this._DownloadState = DownloadState;
        }
    }
    public class ThreadProcessEventArgs : System.EventArgs
    {
        private Thread _thread;
        public Thread thread
        {
            get
            {
                return this._thread;
            }
        }
        public ThreadProcessEventArgs(Thread thread)
        {
            this._thread = thread;
        }
    }
    /// <summary>
    /// 支持断点续传多线程下载的类
    /// </summary>
    public class HttpWebClient
    {
        private static object _SyncLockObject = new object();
        public delegate void DataReceiveEventHandler(HttpWebClient Sender, DownLoadEventArgs e);
        public event DataReceiveEventHandler DataReceive; //接收字节数据事件
        public delegate void ExceptionEventHandler(HttpWebClient Sender, ExceptionEventArgs e);
        public event ExceptionEventHandler ExceptionOccurrs; //发生异常事件
        public delegate void ThreadProcessEventHandler(HttpWebClient Sender, ThreadProcessEventArgs e);
        public event ThreadProcessEventHandler ThreadProcessEnd; //发生多线程处理完毕事件
        private int _FileLength; //下载文件的总大小
        public int FileLength
        {
            get
            {
                return _FileLength;
            }
        }
        /// <summary>
        /// 分块下载文件
        /// </summary>
        /// <param name="Address">URL 地址</param>
        /// <param name="FileName">保存到本地的路径文件名</param>
        /// <param name="ChunksCount">块数,线程数</param>
        public void DownloadFile(string Address, string FileName, int ChunksCount)
        {
            int p = 0; // position
            int s = 0; // chunk size
            string a = null;
            HttpWebRequest hwrq;
            HttpWebResponse hwrp = null;
            try
            {
                hwrq = (HttpWebRequest)WebRequest.Create(this.GetUri(Address));
                hwrp = (HttpWebResponse)hwrq.GetResponse();
                long L = hwrp.ContentLength;
                hwrq.Credentials = this.m_credentials;
                L = ((L == -1) || (L > 0x7fffffff)) ? ((long)0x7fffffff) : L; //Int32.MaxValue 该常数的值为 2,147,483,647; 即十六进制的 0x7FFFFFFF
                int l = (int)L;
                this._FileLength = l;
                //    在本地预定空间(竟然在多线程下不用先预定空间)
                //    FileStream sw = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                //    sw.Write(new byte[l], 0, l);
                //    sw.Close();
                //    sw = null;
                bool b = (hwrp.Headers["Accept-Ranges"] != null & hwrp.Headers["Accept-Ranges"] == "bytes");
                a = hwrp.Headers["Content-Disposition"]; //attachment
                if (a != null)
                {
                    a = a.Substring(a.LastIndexOf("filename=") + 9);
                }
                else
                {
                    a = FileName;
                }
                int ss = s;
                if (b)
                {
                    s = l / ChunksCount;
                    if (s < 2 * 64 * 1024) //块大小至少为 128 K 字节
                    {
                        s = 2 * 64 * 1024;
                    }
                    ss = s;
                    int i = 0;
                    while (l > s)
                    {
                        l -= s;
                        if (l < s)
                        {
                            s += l;
                        }
                        if (i++ > 0)
                        {
                            DownLoadState x = new DownLoadState(Address, hwrp.ResponseUri.AbsolutePath, FileName, a, p, s, new ThreadCallbackHandler(this.DownloadFileChunk));
                            //       单线程下载
                            //       x.StartDownloadFileChunk();
                            x.httpWebClient = this;
                            //多线程下载
                            Thread t = new Thread(new ThreadStart(x.StartDownloadFileChunk));
                            //this.OnThreadProcess(t);
                            t.Start();
                        }
                        p += s;
                    }
                    s = ss;
                    byte[] buffer = this.ResponseAsBytes(Address, hwrp, s, FileName);
                    this.OnThreadProcess(Thread.CurrentThread);
                    //    lock (_SyncLockObject)
                    //    {
                    //     this._Bytes += buffer.Length;
                    //    }
                }
            }
            catch (Exception e)
            {
                ExceptionActions ea = ExceptionActions.Throw;
                if (this.ExceptionOccurrs != null)
                {
                    DownLoadState x = new DownLoadState(Address, hwrp.ResponseUri.AbsolutePath, FileName, a, p, s);
                    ExceptionEventArgs eea = new ExceptionEventArgs(e, x);
                    ExceptionOccurrs(this, eea);
                    ea = eea.ExceptionAction;
                }
                if (ea == ExceptionActions.Throw)
                {
                    if (!(e is WebException) && !(e is SecurityException))
                    {
                        throw new WebException("net_webclient", e);
                    }
                    throw;
                }
            }
        }
        internal void OnThreadProcess(Thread t)
        {
            if (ThreadProcessEnd != null)
            {
                ThreadProcessEventArgs tpea = new ThreadProcessEventArgs(t);
                ThreadProcessEnd(this, tpea);
            }
        }
        /// <summary>
        /// 下载一个文件块,利用该方法可自行实现多线程断点续传
        /// </summary>
        /// <param name="Address">URL 地址</param>
        /// <param name="FileName">保存到本地的路径文件名</param>
        /// <param name="Length">块大小</param>
        public void DownloadFileChunk(string Address, string FileName, int FromPosition, int Length)
        {
            HttpWebResponse hwrp = null;
            string a = null;
            try
            {
                //this._FileName = FileName;
                HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(this.GetUri(Address));
                //hwrq.Credentials = this.m_credentials;
                hwrq.AddRange(FromPosition);
                hwrp = (HttpWebResponse)hwrq.GetResponse();
                a = hwrp.Headers["Content-Disposition"]; //attachment
                if (a != null)
                {
                    a = a.Substring(a.LastIndexOf("filename=") + 9);
                }
                else
                {
                    a = FileName;
                }
                byte[] buffer = this.ResponseAsBytes(Address, hwrp, Length, FileName);
                //   lock (_SyncLockObject)
                //   {
                //    this._Bytes += buffer.Length;
                //   }
            }
            catch (Exception e)
            {
                ExceptionActions ea = ExceptionActions.Throw;
                if (this.ExceptionOccurrs != null)
                {
                    DownLoadState x = new DownLoadState(Address, hwrp.ResponseUri.AbsolutePath, FileName, a, FromPosition, Length);
                    ExceptionEventArgs eea = new ExceptionEventArgs(e, x);
                    ExceptionOccurrs(this, eea);
                    ea = eea.ExceptionAction;
                }
                if (ea == ExceptionActions.Throw)
                {
                    if (!(e is WebException) && !(e is SecurityException))
                    {
                        throw new WebException("net_webclient", e);
                    }
                    throw;
                }
            }
        }
        internal byte[] ResponseAsBytes(string RequestURL, WebResponse Response, long Length, string FileName)
        {
            string a = null; //AttachmentName
            int P = 0; //整个文件的位置指针
            int num2 = 0;
            try
            {
                a = Response.Headers["Content-Disposition"]; //attachment
                if (a != null)
                {
                    a = a.Substring(a.LastIndexOf("filename=") + 9);
                }
                long num1 = Length; //Response.ContentLength;
                bool flag1 = false;
                if (num1 == -1)
                {
                    flag1 = true;
                    num1 = 0x10000; //64k
                }
                byte[] buffer1 = new byte[(int)num1];
                int p = 0; //本块的位置指针
                string s = Response.Headers["Content-Range"];
                if (s != null)
                {
                    s = s.Replace("bytes ", "");
                    s = s.Substring(0, s.IndexOf("-"));
                    P = Convert.ToInt32(s);
                }
                int num3 = 0;
                Stream S = Response.GetResponseStream();
                do
                {
                    num2 = S.Read(buffer1, num3, ((int)num1) - num3);
                    num3 += num2;
                    if (flag1 && (num3 == num1))
                    {
                        num1 += 0x10000;
                        byte[] buffer2 = new byte[(int)num1];
                        Buffer.BlockCopy(buffer1, 0, buffer2, 0, num3);
                        buffer1 = buffer2;
                    }
                    //    lock (_SyncLockObject)
                    //    {
                    //     this._bytes += num2;
                    //    }
                    if (num2 > 0)
                    {
                        if (this.DataReceive != null)
                        {
                            byte[] buffer = new byte[num2];
                            Buffer.BlockCopy(buffer1, p, buffer, 0, buffer.Length);
                            DownLoadState dls = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2, buffer);
                            DownLoadEventArgs dlea = new DownLoadEventArgs(dls);
                            //触发事件
                            this.OnDataReceive(dlea);
                            //System.Threading.Thread.Sleep(100);
                        }
                        p += num2; //本块的位置指针
                        P += num2; //整个文件的位置指针
                    }
                    else
                    {
                        break;
                    }
                }
                while (num2 != 0);
                S.Close();
                S = null;
                if (flag1)
                {
                    byte[] buffer3 = new byte[num3];
                    Buffer.BlockCopy(buffer1, 0, buffer3, 0, num3);
                    buffer1 = buffer3;
                }
                return buffer1;
            }
            catch (Exception e)
            {
                ExceptionActions ea = ExceptionActions.Throw;
                if (this.ExceptionOccurrs != null)
                {
                    DownLoadState x = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2);
                    ExceptionEventArgs eea = new ExceptionEventArgs(e, x);
                    ExceptionOccurrs(this, eea);
                    ea = eea.ExceptionAction;
                }
                if (ea == ExceptionActions.Throw)
                {
                    if (!(e is WebException) && !(e is SecurityException))
                    {
                        throw new WebException("net_webclient", e);
                    }
                    throw;
                }
                return null;
            }
        }
        private void OnDataReceive(DownLoadEventArgs e)
        {
            //触发数据到达事件
            DataReceive(this, e);
        }
        public byte[] UploadFile(string address, string fileName)
        {
            return this.UploadFile(address, "POST", fileName, "file");
        }
        public string UploadFileEx(string address, string method, string fileName, string fieldName)
        {
            return Encoding.ASCII.GetString(UploadFile(address, method, fileName, fieldName));
        }
        public byte[] UploadFile(string address, string method, string fileName, string fieldName)
        {
            byte[] buffer4;
            FileStream stream1 = null;
            try
            {
                fileName = Path.GetFullPath(fileName);
                string text1 = "---------------------" + DateTime.Now.Ticks.ToString("x");
                string text2 = "application/octet-stream";
                stream1 = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                WebRequest request1 = WebRequest.Create(this.GetUri(address));
                request1.Credentials = this.m_credentials;
                request1.ContentType = "multipart/form-data; boundary=" + text1;
                request1.Method = method;
                string[] textArray1 = new string[7] { "--", text1, "\r\nContent-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"", Path.GetFileName(fileName), "\"\r\nContent-Type: ", text2, "\r\n\r\n" };
                string text3 = string.Concat(textArray1);
                byte[] buffer1 = Encoding.UTF8.GetBytes(text3);
                byte[] buffer2 = Encoding.ASCII.GetBytes("\r\n--" + text1 + "\r\n");
                long num1 = 0x7fffffffffffffff;
                try
                {
                    num1 = stream1.Length;
                    request1.ContentLength = (num1 + buffer1.Length) + buffer2.Length;
                }
                catch
                {
                }
                byte[] buffer3 = new byte[Math.Min(0x2000, (int)num1)];
                using (Stream stream2 = request1.GetRequestStream())
                {
                    int num2;
                    stream2.Write(buffer1, 0, buffer1.Length);
                    do
                    {
                        num2 = stream1.Read(buffer3, 0, buffer3.Length);
                        if (num2 != 0)
                        {
                            stream2.Write(buffer3, 0, num2);
                        }
                    }
                    while (num2 != 0);
                    stream2.Write(buffer2, 0, buffer2.Length);
                }
                stream1.Close();
                stream1 = null;
                WebResponse response1 = request1.GetResponse();
                buffer4 = this.ResponseAsBytes(response1);
            }
            catch (Exception exception1)
            {
                if (stream1 != null)
                {
                    stream1.Close();
                    stream1 = null;
                }
                if (!(exception1 is WebException) && !(exception1 is SecurityException))
                {
                    //throw new WebException(SR.GetString("net_webclient"), exception1);
                    throw new WebException("net_webclient", exception1);
                }
                throw;
            }
            return buffer4;
        }
        private byte[] ResponseAsBytes(WebResponse response)
        {
            int num2;
            long num1 = response.ContentLength;
            bool flag1 = false;
            if (num1 == -1)
            {
                flag1 = true;
                num1 = 0x10000;
            }
            byte[] buffer1 = new byte[(int)num1];
            Stream stream1 = response.GetResponseStream();
            int num3 = 0;
            do
            {
                num2 = stream1.Read(buffer1, num3, ((int)num1) - num3);
                num3 += num2;
                if (flag1 && (num3 == num1))
                {
                    num1 += 0x10000;
                    byte[] buffer2 = new byte[(int)num1];
                    Buffer.BlockCopy(buffer1, 0, buffer2, 0, num3);
                    buffer1 = buffer2;
                }
            }
            while (num2 != 0);
            stream1.Close();
            if (flag1)
            {
                byte[] buffer3 = new byte[num3];
                Buffer.BlockCopy(buffer1, 0, buffer3, 0, num3);
                buffer1 = buffer3;
            }
            return buffer1;
        }
        private NameValueCollection m_requestParameters;
        private Uri m_baseAddress;
        private ICredentials m_credentials = CredentialCache.DefaultCredentials;
        public ICredentials Credentials
        {
            get
            {
                return this.m_credentials;
            }
            set
            {
                this.m_credentials = value;
            }
        }
        public NameValueCollection QueryString
        {
            get
            {
                if (this.m_requestParameters == null)
                {
                    this.m_requestParameters = new NameValueCollection();
                }
                return this.m_requestParameters;
            }
            set
            {
                this.m_requestParameters = value;
            }
        }
        public string BaseAddress
        {
            get
            {
                if (this.m_baseAddress != null)
                {
                    return this.m_baseAddress.ToString();
                }
                return string.Empty;
            }
            set
            {
                if ((value == null) || (value.Length == 0))
                {
                    this.m_baseAddress = null;
                }
                else
                {
                    try
                    {
                        this.m_baseAddress = new Uri(value);
                    }
                    catch (Exception exception1)
                    {
                        throw new ArgumentException("value", exception1);
                    }
                }
            }
        }
        private Uri GetUri(string path)
        {
            Uri uri1;
            try
            {
                if (this.m_baseAddress != null)
                {
                    uri1 = new Uri(this.m_baseAddress, path);
                }
                else
                {
                    uri1 = new Uri(path);
                }
                if (this.m_requestParameters == null)
                {
                    return uri1;
                }
                StringBuilder builder1 = new StringBuilder();
                string text1 = string.Empty;
                for (int num1 = 0; num1 < this.m_requestParameters.Count; num1++)
                {
                    builder1.Append(text1 + this.m_requestParameters.AllKeys[num1] + "=" + this.m_requestParameters[num1]);
                    text1 = "&";
                }
                UriBuilder builder2 = new UriBuilder(uri1);
                builder2.Query = builder1.ToString();
                uri1 = builder2.Uri;
            }
            catch (UriFormatException)
            {
                uri1 = new Uri(Path.GetFullPath(path));
            }
            return uri1;
        }
    }

    #region 测试类
    /* 
   * 
    /// <summary> 
    /// 测试类 
    /// </summary> 
    class AppTest
    {
        int _k = 0;
        int _K = 0;
        static void Main()
        {
            AppTest a = new AppTest();
            Microshaoft.Utils.HttpWebClient x = new Microshaoft.Utils.HttpWebClient();

            a._K = 10;
            //订阅 DataReceive 事件 
            x.DataReceive += new Microshaoft.Utils.HttpWebClient.DataReceiveEventHandler(a.x_DataReceive);
            //订阅 ExceptionOccurrs 事件 
            x.ExceptionOccurrs += new Microshaoft.Utils.HttpWebClient.ExceptionEventHandler(a.x_ExceptionOccurrs);
            x.ThreadProcessEnd += new Microshaoft.Utils.HttpWebClient.ThreadProcessEventHandler(a.x_ThreadProcessEnd);
            string F = "http://localhost/download/phpMyAdmin-2.6.1-pl2.zip";
            a._F = F;
            F = "http://localhost/download/jdk-1_5_0_01-windows-i586-p.aa.exe";
            //F = "http://localhost/download/ReSharper1.5.exe";
            //F = "http://localhost/mywebapplications/WebApplication7/WebForm1.aspx"; 
            //F = "http://localhost:1080/test/download.jsp";
            //F = "http://localhost/download/Webcast20050125_PPT.zip"; 
            //F = "http://www.morequick.com/greenbrowsergb.zip"; 
            //F = "http://localhost/download/test_local.rar"; 
            string f = F.Substring(F.LastIndexOf("/") + 1);
            //(new System.Threading.Thread(new System.Threading.ThreadStart(new ThreadProcessState(F, @"E:\temp\" + f, 10, x).StartThreadProcess))).Start();
            x.DownloadFile(F, @"E:\temp\temp\" + f, a._K);
            //  x.DownloadFileChunk(F, @"E:\temp\" + f,15,34556);
            System.Console.ReadLine();
            //  string uploadfile = "e:\\test_local.rar"; 
            //  string str = x.UploadFileEx("http://localhost/phpmyadmin/uploadaction.php", "POST", uploadfile, "file1"); 
            //  System.Console.WriteLine(str); 
            //  System.Console.ReadLine(); 
        }
        string bs = ""; //用于记录上次的位数 
        bool b = false;
        private int i = 0;
        private static object _SyncLockObject = new object();
        string _F;
        string _f;
        private void x_DataReceive(Microshaoft.Utils.HttpWebClient Sender, Microshaoft.Utils.DownLoadEventArgs e)
        {
            if (!this.b)
            {
                lock (_SyncLockObject)
                {
                    if (!this.b)
                    {
                        System.Console.Write(System.DateTime.Now.ToString() + " 已接收数据:           ");
                        //System.Console.Write( System.DateTime.Now.ToString() + " 已接收数据:           "); 
                        this.b = true;
                    }
                }
            }
            string f = e.DownloadState.FileName;
            if (e.DownloadState.AttachmentName != null)
                f = System.IO.Path.GetDirectoryName(f) + @"\" + e.DownloadState.AttachmentName;
            this._f = f;
            using (System.IO.FileStream sw = new System.IO.FileStream(f, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite))
            {
                sw.Position = e.DownloadState.Position;
                sw.Write(e.DownloadState.Data, 0, e.DownloadState.Data.Length);
                sw.Close();
            }
            string s = System.DateTime.Now.ToString();
            lock (_SyncLockObject)
            {
                this.i += e.DownloadState.Data.Length;
                System.Console.Write(bs + "\b\b\b\b\b\b\b\b\b\b" + i + " / " + Sender.FileLength + " 字节数据 " + s);
                //System.Console.Write(bs + i + " 字节数据 " + s); 
                this.bs = new string('\b', Digits(i) + 3 + Digits(Sender.FileLength) + s.Length);
            }
        }
        int Digits(int n) //数字所占位数 
        {
            n = System.Math.Abs(n);
            n = n / 10;
            int i = 1;
            while (n > 0)
            {
                n = n / 10;
                i++;
            }
            return i;
        }
        private void x_ExceptionOccurrs(Microshaoft.Utils.HttpWebClient Sender, Microshaoft.Utils.ExceptionEventArgs e)
        {
            System.Console.WriteLine(e.Exception.Message);
            //发生异常重新下载相当于断点续传,你可以自己自行选择处理方式 
            Microshaoft.Utils.HttpWebClient x = new Microshaoft.Utils.HttpWebClient();
            x.DownloadFileChunk(this._F, this._f, e.DownloadState.Position, e.DownloadState.Length);
            e.ExceptionAction = Microshaoft.Utils.ExceptionActions.Ignore;
        }
        private void x_ThreadProcessEnd(Microshaoft.Utils.HttpWebClient Sender, Microshaoft.Utils.ThreadProcessEventArgs e)
        {
            //if (e.thread.ThreadState == System.Threading.ThreadState.Stopped) 
            if (this._k++ == this._K - 1)
                System.Console.WriteLine("\nend");
        }
    }
     * 
    */
    #endregion 测试类

}

2、监听文件:

我刚可代码就发现不对了,php好像被忽略了。其实最好一个通过min映射来判断,当然那时候肯定不懂。

View Code
        /// <summary>
        /// 导航发生后发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webMain_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            Url = this.WebBrowserArr[tabMain.SelectedIndex].Url.ToString();
            this.cmbBoxUrl.Text = Url;
            if (!Url.EndsWith(".html") && !Url.EndsWith(".htm") && !Url.EndsWith(".jsp")
                && !Url.EndsWith(".aspx") && !Url.EndsWith(".asp") && !Url.EndsWith("/")
                && Url.Substring(Url.Length - 6, 5).Contains("."))
            {
                Frm_Download_Dialog frm_Download_Dialog = new Frm_Download_Dialog();
                frm_Download_Dialog.Show();
                WebBrowserArr[tabMain.SelectedIndex].Stop();//停止导航
            }
        }

关于页面

这个没什么好像是的仿照搜狗浏览器的,同样继承了无边框圆矩形窗体,然后加了一些背景图片什么的。

clip_image004

最后

虽然做出的这东西华而不实,代码也不怎么样,但我那时候真的是很努力的在做,而且最终也实现了所有功能,其实也学到了很多。

人之所以努力更多时候在于兴趣。 

源代码下载:

原文地址:https://www.cnblogs.com/cyehu/p/2908677.html