⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch9_demoform002.cs

📁 章立民-visual stadio 2005 c#编程技巧
💻 CS
📖 第 1 页 / 共 2 页
字号:
            maximizeToolStripItem(this.BrowserToolStrip, this.urlTextBox, myBUFFER);
        }

        private void maximizeToolStripItem(ToolStrip parentToolStrip, ToolStripItem maximizedItem, int myBuffer)
        {
            int width = parentToolStrip.Width - (parentToolStrip.Padding.Horizontal + myBuffer);

            foreach (ToolStripItem item in parentToolStrip.Items)
            {
                if (!(item.Equals(maximizedItem)))
                {
                    width -= item.Width;
                }

                width -= item.Margin.Horizontal;
            }

            maximizedItem.Width = width;
        }

        private void tsmiViewSource_Click(object sender, EventArgs e)
        {
            // 根据网址来确认文件名称。
            string myUrl = this.webBrowser1.Url.ToString();
            string myFileName = "";

            if (myUrl.ToString().StartsWith("http") || Path.GetExtension(myUrl).StartsWith(".htm"))
            {
                string myAbsolutePath = this.webBrowser1.Url.AbsolutePath;

                if (myAbsolutePath == @"/")
                {
                    myFileName = "Test.txt";
                }
                else
                {
                    myFileName = Path.GetFileName(myAbsolutePath);

                    if (myFileName == String.Empty)
                    {
                        int PosLastOneSlash, PosLastTwoSlash;

                        PosLastOneSlash = myAbsolutePath.LastIndexOf(@"/");
                        PosLastTwoSlash = myAbsolutePath.LastIndexOf(@"/", PosLastOneSlash - 1);

                        myFileName = myAbsolutePath.Substring(PosLastTwoSlash + 1, PosLastOneSlash - PosLastTwoSlash - 1) + ".htm";
                    }
                }
            }
            else
            {
                MessageBox.Show("这不是一个 HTML 文件,无法查看其源文件。");
                return;
            }

            myFileName = @"C:\" + myFileName;

            try
            {
                // 将网页的 HTML 内容写入指定的档案中。
                File.WriteAllText(myFileName, this.webBrowser1.DocumentText);

                // 使用记事本来开启刚刚所写入的档案。
                System.Diagnostics.Process.Start("notepad.exe", myFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void printButton_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Print();
        }

        private void tsmiPageSetup_Click(object sender, EventArgs e)
        {
            this.webBrowser1.ShowPageSetupDialog();
        }

        private void tsmiPrint_Click(object sender, EventArgs e)
        {
            this.webBrowser1.ShowPrintDialog();
        }

        private void tsmiPrintPreview_Click(object sender, EventArgs e)
        {
            this.webBrowser1.ShowPrintPreviewDialog();
        }

        private void RefreshDir(DirectoryInfo myFavDir)
        {
            // 清除所有的 ToolStripMenuItem。
            this.tsmiFavorite.DropDownItems.Clear();

            this.addToFavoritesToolStripMenuItem.Name = "addToFavoritesToolStripMenuItem";
            this.addToFavoritesToolStripMenuItem.Size = new System.Drawing.Size(167, 22);
            this.addToFavoritesToolStripMenuItem.Text = "添加到收藏夹(&A)...";

            // 加入 ToolStripMenuItem。
            this.tsmiFavorite.DropDownItems.Add(addToFavoritesToolStripMenuItem);

            this.organizeFavoritesToolStripMenuItem.Name = "organizeFavoritesToolStripMenuItem";
            this.organizeFavoritesToolStripMenuItem.Size = new System.Drawing.Size(167, 22);
            this.organizeFavoritesToolStripMenuItem.Text = "整理收藏夹(&O)...";

            // 加入 ToolStripMenuItem。
            this.tsmiFavorite.DropDownItems.Add(organizeFavoritesToolStripMenuItem);

            this.toolStripMenuItem13.Name = "toolStripMenuItem13";
            this.toolStripMenuItem13.Size = new System.Drawing.Size(164, 6);

            // 加入 ToolStripMenuItem。
            this.tsmiFavorite.DropDownItems.Add(toolStripMenuItem13);

            FileSystemInfo[] myFileSystemInfo = myFavDir.GetFileSystemInfos();

            GetDir(myFileSystemInfo, tsmiFavorite);
        }

        private void GetDir(FileSystemInfo[] myFavDir, ToolStripMenuItem myToolStripMenuItem)
        {
            string myStr, myUrl, myDescription;
            string URLPrefix = "url=";
            string DescriptionPrefix = "description=";

            if (myFavDir == null)
            {
                throw new ArgumentNullException("myFavDir");
            }

            int ItemIndex = 1;

            foreach (FileSystemInfo myFileSystemInfo in myFavDir)
            {
                // 判断是否为目录。
                if (myFileSystemInfo is DirectoryInfo)
                {
                    ToolStripMenuItem FileToolStripMenuItem = new ToolStripMenuItem(myFileSystemInfo.ToString(), Resources.Folder16);

                    myToolStripMenuItem.DropDownItems.Add(FileToolStripMenuItem);

                    DirectoryInfo dInfo = (DirectoryInfo)(myFileSystemInfo);

                    // 递归呼叫方法显示所有的子目录内容。
                    GetDir(dInfo.GetFileSystemInfos(), FileToolStripMenuItem);
                }
                // 判断是否为档案。
                else if (myFileSystemInfo is FileInfo)
                {
                    myUrl = "";
                    myDescription = "";

                    using (StreamReader SR = new StreamReader(myFileSystemInfo.FullName, Encoding.Default))
                    {
                        myStr = SR.ReadLine();

                        do
                        {
                            if (myStr.ToLower().StartsWith(URLPrefix))
                            {
                                myUrl = myStr.Substring(URLPrefix.Length);
                            }
                            else if (myStr.ToLower().StartsWith(DescriptionPrefix))
                            {
                                myDescription = myStr.Substring(DescriptionPrefix.Length);
                            }

                            myStr = SR.ReadLine();
                        }
                        while (myStr != null);
                    }

                    ToolStripMenuItem FileToolStripMenuItem = new ToolStripMenuItem(myFileSystemInfo.ToString().Substring(0, myFileSystemInfo.ToString().LastIndexOf(".")), Resources.link16, new System.EventHandler(FileToolStripMenuItem_Click));

                    // 将 Url 指派给菜单项目的 Tag 属性。
                    FileToolStripMenuItem.Tag = myUrl;

                    // 将 Url 指派给菜单项目的 ToolTipText 属性。
                    FileToolStripMenuItem.ToolTipText = myUrl;

                    myToolStripMenuItem.DropDownItems.Add(FileToolStripMenuItem);
                }

                ItemIndex += 1;
            }
        }

        private void FileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate(((ToolStripMenuItem)(sender)).Tag.ToString());
        }

        // 添加到收藏夹。

        private void addToFavoritesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SHDocVw.ShellUIHelper shl = new SHDocVw.ShellUIHelper();

            object title = (object)(webBrowser1.DocumentTitle);
            shl.AddFavorite(webBrowser1.Url.ToString(), ref title);

            DirectoryInfo myFavDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
            RefreshDir(myFavDir);
        }

        // 整理收藏夹。
        private void organizeFavoritesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int success;
            DirectoryInfo myFavDir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));

            success = DoOrganizeFavDlg(this.Handle, Environment.GetFolderPath(Environment.SpecialFolder.Favorites));

            RefreshDir(myFavDir);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -