代码改变世界

c#window程序开发入门系列--自学笔记之WebBrowser

2010-04-08 13:35  zhaoyang  阅读(2187)  评论(2编辑  收藏  举报

c#window程序开发入门系列--自学笔记之WebBrowser

该控件是本软件最核心的部分,实现文档的编辑。类似文本编辑器。为什么选择该控件作为文本编辑器呢?主要考虑到网络文章很多,该控件可以实现简单的粘贴,可以保留网页的样式。首先合适预览一下编辑器界面。

图2-1界面预览

   看起来跟web中的文笔编辑器相似。这个文本编辑器主要有两大控件组成,上半部分是ToolStrip控winform的典型控件,下半部分WebBrowser控件。单击上半部分的快捷按钮,下面的文本进行相应的设置。

     开发这样的一个文本编辑器会牵涉到许多知识点,下面将一一记录。

如何进入WebBrowser控件的可编辑状态?

   假设WebBrowser控件name为“EditStp”,则可以用如下代码使其进入编辑状态。

   EditStp.DocumentText = string.Empty;
   EditStp.Document.ExecCommand("EditMode", false, null);
   EditStp.Document.ExecCommand("LiveResize", false, null);

如何监控WebBrowser控件的内容发生变化?

   WebBrowser控件没有监视内容变化的方法,所以我决定自己开发这一功能。首先我们记录该控件初始状态到一变量“oldString”,添加一定时器时刻监控WebBrowser控件的内容,与“oldString”进行比较如果内容相同没有发生变化,否则发生变化(也许高人有更好的方法请告知,谢谢)。

如何设置WebBrowser控件的字体,大小等样式?

1、字体加粗         EditStp.Document.ExecCommand("Bold", false, null);

2、斜体                 EditStp.Document.ExecCommand("Italic", false, null);

3、添加下划线     EditStp.Document.ExecCommand("Underline", false, null);

4、设置前景色     EditStp.Document.ExecCommand("ForeColor", false, colorstr);

5、设置背景色     EditStp.Document.ExecCommand("BackColor", false, colorstr);

6、添加链接         EditStp.Document.ExecCommand("CreateLink", false, link);

7、插入图片         EditStp.Document.ExecCommand("InsertImage", false, link);

8、左侧对齐         EditStp.Document.ExecCommand("JustifyLeft", false, null);

9、居中侧对齐     EditStp.Document.ExecCommand("JustifyCenter", false, null);

10、右侧对齐       EditStp.Document.ExecCommand("JustifyRight", false, null);

11、两端对齐      EditStp.Document.ExecCommand("JustifyFull", false, null);

 12、插入标号     EditStp.Document.ExecCommand("InsertOrderedList", false, null);

13、                      EditStp.Document.ExecCommand("InsertUnorderedList", false, null);

14、                      EditStp.Document.ExecCommand("Indent", false, null);

15、                      EditStp.Document.ExecCommand("Outdent", false, null);

如何实现保存网络图片文件到本地?

首先分析WebBrowser控件的内容,用正则表达式取出图片地址,把含有“http”的图片从服务器端下载到本地。具体的代码如下所示:

//正则查找图片地址

public static string[] GetHtmlImageUrlList(string sHtmlText)
        {
            // 定义正则表达式用来匹配 img 标签[
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);

            // 搜索匹配的字符串
            MatchCollection matches = regImg.Matches(sHtmlText);

            int i = 0;
            string[] sUrlList = new string[matches.Count];

            // 取得匹配项列表
            foreach (Match match in matches)
                sUrlList[i++] = match.Groups["imgUrl"].Value;

            return sUrlList;
        }

string tempText = EditStp.DocumentText;
            string[] TempPic = GetHtmlImageUrlList(tempText);
            if (TempPic.Length == 0)
            {
                MessageBox.Show("没有图片需要下载!");
            }
            else
            {
                for (int i = 0; i < TempPic.Length; i++)
                {
                    string webPicURL = TempPic[i].ToString();
                    if (webPicURL.IndexOf("http://") >= 0)
                    {
                        WebClient myclient = new WebClient();
                        string newpicurl = CurrentPath + "\\" + CurrentFolder + "\\stplist\\images\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + webPicURL.Substring(webPicURL.LastIndexOf("."), webPicURL.Length - webPicURL.LastIndexOf("."));
                        myclient.DownloadFile(webPicURL,newpicurl);
                        tempText = tempText.Replace(webPicURL, newpicurl);
                    }
                }
                EditStp.Document.OpenNew(true);
                EditStp.DocumentText = tempText;
                MessageBox.Show("保存成功");
            }

存在这样一个问题就是图片下载到本地用相对路径显示,图片显示不出来必须用绝对路径才可显示。请高手指点。

 

 

源程序下载:源文件

安装程序下载:安装文件