1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.Drawing; 7 using mshtml; 8 9 //参考 10 11 //http://code.google.com/p/msit88-gold-coast/source/browse/sandbox/GoldCoastTestSolution/HtmlEditor/DocumentEx.cs 12 13 namespace WebBrowserCtrl 14 { 15 class DHTMLClass 16 { 17 public DHTMLClass( ) 18 { 19 webBrowser1 = null; 20 } 21 22 public WebBrowser webBrowser1; 23 24 25 #region javascript语法 在CSharp中的写法 26 /* 27 更多的要参考 DHTML 手册.chm 28 @javascript 29 function AddLink() 30 { 31 //identify selected text 32 var sText = document.selection.createRange(); 33 if (sText.text != "") 34 { 35 //create link 36 document.execCommand("CreateLink"); 37 //change the color to indicate success 38 if (sText.parentElement().tagName == "A") 39 { 40 sText.execCommand("ForeColor", false, "#FF0033"); 41 } 42 } 43 else 44 { 45 alert("Please select some text!"); 46 } 47 } 48 #1 49 C# 这么写 50 @javascript 51 document.execCommand("CreateLink"); 52 @C# 53 webbrowser1.Document.ExecCommand("CreateLink", false, null); 54 55 #2 56 @javascript 57 vCmdValue = object.queryCommandValue(sCmdID) 58 @C# 59 IHTMLDocument2 document2 = (IHTMLDocument2)webBrowser1.Document.DomDocument; 60 bool b1 = (bool)document2.queryCommandEnabled("CreateLink"); 61 bool b2= (bool)document2.queryCommandIndeterm("CreateLink"); 62 bool b3 = (bool)document2.queryCommandState("CreateLink"); 63 bool b4 = (bool)document2.queryCommandSupported("CreateLink"); 64 string s = document2.queryCommandText("CreateLink"); 65 object obj = (object)document2.queryCommandValue("CreateLink"); 66 67 68 #3 69 @javascript 70 range = selection.createRange() 71 @C# 72 IHTMLDocument2 document2 = (IHTMLDocument2)webBrowser.Document.DomDocument; 73 IHTMLTxtRange range = (IHTMLTxtRange)document2.selection.createRange(); 74 75 # 76 @javascript 77 78 @C# 79 80 # 81 @javascript 82 83 @C# 84 85 # 86 @javascript 87 88 @C# 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 */ 118 #endregion 119 120 121 122 /// <summary> 123 ///"document" 就是 "webBrowser.Document" 124 ///获取一个 HtmlDocument,它表示当前显示在 WebBrowser 控件中的网页。 125 /// </summary> 126 public HtmlDocument document 127 { 128 get{ 129 return webBrowser1.Document; 130 } 131 } 132 133 /// <summary> 134 /// "document2" 就是 "(IHTMLDocument2)webBrowser.Document.DomDocument" 135 /// 获取此 HtmlDocument 的非托管接口指针 136 /// </summary> 137 public IHTMLDocument2 document2 138 { 139 get{ 140 try 141 { 142 return (IHTMLDocument2)webBrowser1.Document.DomDocument; 143 } 144 catch (System.Exception ex) 145 { 146 return null; 147 } 148 } 149 } 150 151 /// <summary> 152 ///当前选中的对象 153 /// TxtRange = 154 /// IHTMLDocument2 document2 = (IHTMLDocument2)webBrowser.Document.DomDocument; 155 /// IHTMLTxtRange range = (IHTMLTxtRange)document2.selection.createRange(); 156 /// </summary> 157 public IHTMLTxtRange TxtRange 158 { 159 get{ 160 //try 161 //{ 162 return (IHTMLTxtRange)document2.selection.createRange(); 163 } 164 //catch (System.Exception ex) 165 //{ 166 // //return null; 167 //} 168 //} 169 } 170 171 #region DesignMode 172 /// <summary> 173 /// 设置焦点 174 /// </summary> 175 public void SetFocus() 176 { 177 document.Focus(); 178 } 179 /// <summary> 180 /// 是否设置焦点 181 /// </summary> 182 /// <returns></returns> 183 public bool focused() 184 { 185 return document.Focused; 186 } 187 188 /// <summary> 189 /// 设置 编辑模式 190 /// </summary> 191 public void SetEditMode() 192 { 193 document.ExecCommand("EditMode", false, null); 194 } 195 196 /// <summary> 197 /// 获得 编辑模式 198 /// </summary> 199 /// <returns></returns> 200 public bool GetEditMode() 201 { 202 return (bool)document2.queryCommandValue("EditMode"); 203 } 204 205 /// <summary> 206 /// 设置 浏览模式 207 /// </summary> 208 public void SetBrowseMode() 209 { 210 document.ExecCommand("BrowseMode", false, null); 211 } 212 213 /// <summary> 214 /// 获得 浏览模式 215 /// </summary> 216 /// <returns></returns> 217 public bool GetBrowseMode() 218 { 219 return (bool)document2.queryCommandValue("BrowseMode"); 220 } 221 222 #endregion 223 224 #region 基本编辑操作 225 #region 撤销 重做 复制 剪切 ... 226 ///bSuccess = object.execCommand(sCommand [, bUserInterface] [, vValue]) 227 228 /// <summary> 229 /// 撤销 230 /// </summary> 231 public void Undo() 232 { 233 document.ExecCommand("Undo", false, null); 234 235 } 236 237 /// <summary> 238 /// 重做 239 /// </summary> 240 public void Redo() 241 { 242 document.ExecCommand("Redo", false, null); 243 } 244 245 /// <summary> 246 /// 将当前选中区复制到剪贴板。 247 /// </summary> 248 public void Copy() 249 { 250 document.ExecCommand("Copy", false, null); 251 } 252 253 /// <summary> 254 /// 将当前选中区复制到剪贴板并删除之。 255 /// </summary> 256 public void Cut() 257 { 258 document.ExecCommand("Cut", false, null); 259 } 260 261 /// <summary> 262 /// 用剪贴板内容覆盖当前选中区。 263 /// </summary> 264 public void Paste() 265 { 266 document.ExecCommand("Paste", false, null); 267 } 268 269 /// <summary> 270 /// 删除当前选中区 271 /// </summary> 272 public void Delete() 273 { 274 document.ExecCommand("Delete", false, null); 275 } 276 277 /// <summary> 278 /// 选中整个文档。 279 /// </summary> 280 public void SelectAll() 281 { 282 document.ExecCommand("SelectAll", false, null); 283 } 284 285 /// <summary> 286 /// 清除当前选中区的选中状态。 287 /// </summary> 288 public void DeSelect() 289 { 290 document.ExecCommand("Unselect", false, null); 291 } 292 293 #region Can 编辑? 294 #region queryCommandEnabled 295 //bEnabled = object.queryCommandEnabled(sCmdID) 296 #endregion 297 /// <summary> 298 /// 是否可以执行Undo 299 /// </summary> 300 /// <returns></returns> 301 public bool CanUndo() 302 { 303 return document2.queryCommandEnabled("Undo"); 304 } 305 306 /// <summary> 307 /// 是否可以执行Redo 308 /// </summary> 309 /// <returns></returns> 310 public bool CanRedo() 311 { 312 return document2.queryCommandEnabled("Redo"); 313 } 314 315 /// <summary> 316 /// 是否可以执行Copy 317 /// </summary> 318 /// <returns></returns> 319 public bool CanCopy() 320 { 321 return document2.queryCommandEnabled("Copy"); 322 } 323 324 /// <summary> 325 /// 是否可以执行Cut 326 /// </summary> 327 /// <returns></returns> 328 public bool CanCut() 329 { 330 return document2.queryCommandEnabled("Cut"); 331 } 332 333 /// <summary> 334 /// 是否可以执行Paste 335 /// </summary> 336 /// <returns></returns> 337 public bool CanPaste() 338 { 339 return document2.queryCommandEnabled("Paste"); 340 } 341 342 /// <summary> 343 /// 是否可以执行Delete 344 /// </summary> 345 /// <returns></returns> 346 public bool CanDelete() 347 { 348 return document2.queryCommandEnabled("Delete"); 349 } 350 351 #endregion 352 #endregion 353 #endregion 354 355 #region SelStart http://www.cnblogs.com/xe2011/p/3463124.html 356 //这里的变量要单独存在 如果使用本类中的全局变量TxtRange 357 //则在WebbrowserControl中 358 //无法获得getSelectionStart()的值和 359 //setSelection 无法选择 360 361 #region 获得webBrowser光标所在的位置 362 /* 363 function getSelectionStart(){ 364 var range=document.selection.createRange(); 365 range.moveStart('character', -document.body.innerText.length); 366 return range.text.length; 367 } 368 */ 369 370 /// <summary> 371 /// 获得webBrowser光标所在的位置 372 /// </summary> 373 /// <returns></returns> 374 public int __GetSelectionStart() 375 { 376 try 377 { 378 IHTMLDocument2 document2 = (IHTMLDocument2)webBrowser1.Document.DomDocument; 379 IHTMLTxtRange TxtRange = (IHTMLTxtRange)document2.selection.createRange(); 380 TxtRange.moveStart("character", -document2.body.innerText.Length); 381 return TxtRange.text.Length; 382 } 383 catch 384 { 385 //当光标在0这个位置使用功能引起错误 386 return 0; 387 } 388 } 389 390 /// <summary> 391 /// 获得webBrowser光标所在的准确位置 392 /// 当有文本被选中时返回 的位置应为当 getSelectionStart() - getSelectionLength() 393 /// 由于换行符导致结果不正确 394 /// </summary> 395 /// <returns></returns> 396 public int getSelectionStart() 397 { 398 int SelStart = __GetSelectionStart(); 399 int SelLength = getSelectionLength(); 400 if (SelLength > 0) 401 return SelStart - SelLength; 402 else 403 404 return SelStart; 405 } 406 407 #endregion 408 409 #region 获得webBrowser选中的文本长度 410 /* 411 function getSelectionLength() 412 { 413 return document.selection.createRange().text.length; 414 } 415 */ 416 /// <summary> 417 /// 获得webBrowser选中的文本长度 418 /// </summary> 419 /// <returns></returns> 420 public int getSelectionLength() 421 { 422 try 423 { 424 IHTMLDocument2 document = (IHTMLDocument2)webBrowser1.Document.DomDocument; 425 IHTMLTxtRange TxtRange = (IHTMLTxtRange)document.selection.createRange(); 426 return TxtRange.text.Length; 427 //return TxtRange.text.Length; 428 } 429 catch (System.Exception ex) 430 { 431 return 0; 432 //当光标在0这个位置使用功能引起错误 433 } 434 } 435 #endregion 436 437 #region 设置webBrowser光标的位置 438 //请看这个 选中指定的字符串 设置length=0,设置start的值就是光标的所在的位置 439 440 // webBrowser1.Document.Focus(); 441 // setSelection(webBrowser1, 6, 0); 442 #endregion 443 444 #region 选中webBrowser指定的字符串 445 /* 446 function setSelection(start,length){ 447 var range=document.selection.createRange(); 448 range.collapse(true); 449 range.moveStart('character', start); 450 range.moveEnd('character', length); 451 range.select(); 452 } 453 */ 454 /// <summary> 455 /// 设置webBrowser光标的位置 456 /// </summary> 457 /// <param name="start"></param> 458 /// <param name="length"></param> 459 public void setSelection(int start, int length) 460 { 461 IHTMLDocument2 document2 = (IHTMLDocument2)webBrowser1.Document.DomDocument; 462 IHTMLTxtRange TxtRange = (IHTMLTxtRange)document2.selection.createRange(); 463 TxtRange.collapse(true); 464 TxtRange.move("textedit", -1); //光标移动到第0位处 465 TxtRange.moveStart("character", start); 466 TxtRange.moveEnd("character", length); 467 TxtRange.select(); 468 } 469 #endregion 470 471 472 #endregion 473 474 #region 操作选中的字符串对象 475 476 /// <summary> 477 /// 获得 webBrowser选中的字符串 478 /// </summary> 479 /// <returns></returns> 480 public string GetSelText() 481 { 482 return TxtRange.text; 483 } 484 485 /// <summary> 486 /// 设置 webBrowser选中的字符串 487 /// </summary> 488 /// <returns></returns> 489 public void SetSelText(string text) 490 { 491 TxtRange.text = text; 492 } 493 494 /// <summary> 495 /// 获得webBrowser选中的字符的HTML格式字符串 496 /// </summary> 497 /// <returns></returns> 498 public string GetSelHtml() 499 { 500 return TxtRange.htmlText; 501 } 502 503 /// <summary> 504 /// 设置 webBrowser选中的字符的HTML格式字符串 505 /// </summary> 506 /// <returns></returns> 507 public void SetSelHtml(string html) 508 { 509 PasteHtml(html); 510 } 511 512 /// <summary> 513 /// 获得webBrowser的全部字符串 514 /// </summary> 515 /// <returns></returns> 516 public string GetText() 517 { 518 return document.Body.InnerText; 519 } 520 521 /// <summary> 522 /// 设置 webBrowser的全部字符串 523 /// </summary> 524 /// <returns></returns> 525 public void SetText(string text) 526 { 527 document.Body.InnerText = text; 528 } 529 530 /// <summary> 531 /// 获得 webBrowser的全部HTML字符串 532 /// </summary> 533 /// <returns></returns> 534 public string GetHtml() 535 { 536 try 537 { 538 return document.Body.InnerHtml; 539 } 540 catch (System.Exception ex) 541 { 542 return ""; 543 } 544 } 545 /// <summary> 546 /// 设置 webBrowser的全部HTML字符串 547 /// </summary> 548 /// <returns></returns> 549 public void SetHtml(string html) 550 { 551 document.Body.InnerHtml = html; 552 } 553 554 /// <summary> 555 /// Webbrowser粘贴HTML功能 556 /// </summary> 557 /// <returns></returns> 558 public void PasteHtml(string HtmlString) 559 { 560 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 561 TxtRange.pasteHTML(HtmlString); 562 } 563 #endregion 564 565 #region 文件档操作 566 /// <summary> 567 /// 返回文档是否修改了 568 /// </summary> 569 /// <returns></returns> 570 public bool GetModify() 571 { 572 return true; 573 } 574 575 /// <summary> 576 /// 设置文档的修改状态 577 /// </summary> 578 public void SetModify(bool bModify) 579 { 580 581 } 582 /// <summary> 583 /// 新建文档 584 /// </summary> 585 public void NewDocument() 586 { 587 588 } 589 590 /// <summary> 591 /// 打开文档 592 /// </summary> 593 /// <param name="fileName"></param> 594 public void LoadFromFile(string fileName) 595 { 596 597 } 598 599 /// <summary> 600 /// 保存文档 601 /// </summary> 602 public void SaveToFile() 603 { 604 //this.ExecWB(OLECMDID.OLECMDID_SAVE); 605 } 606 607 /// <summary> 608 /// 另存文档 609 /// </summary> 610 /// <param name="fileName"></param> 611 public void SaveToFile(string fileName) 612 { 613 614 } 615 616 /// <summary> 617 /// 618 /// </summary> 619 /// <param name="fileName1"></param> 620 /// <param name="fileName2"></param> 621 public void SaveToFile(string fileName1, string fileName2) 622 { 623 624 } 625 626 /// <summary> 627 /// 保存为HTML格式的文档 628 /// </summary> 629 /// <param name="fileName"></param> 630 public void SaveToHtmlFormat(string fileName) 631 { 632 633 } 634 635 /// <summary> 636 /// 保存为Text格式的文档 637 /// </summary> 638 /// <param name="fileName"></param> 639 public void SaveToMhtFormat(string fileName) 640 { 641 642 } 643 /// <summary> 644 /// 保存为Mht格式的文档 645 /// </summary> 646 /// <param name="fileName"></param> 647 public void SaveToTextFormat(string fileName) 648 { 649 650 } 651 652 653 #endregion 654 655 #region 字体 656 657 /// <summary> 658 /// 设置 当前选中区的字体。 659 /// </summary> 660 /// <returns></returns> 661 public void SetSelFontName(string fontName) 662 { 663 document.ExecCommand("FontName", false, fontName); 664 } 665 /// <summary> 666 /// 获取 当前选中区的字体。 667 /// </summary> 668 /// <returns></returns> 669 public string GetSelFontName() 670 { 671 //if (webBrowser.ReadyState == WebBrowserReadyState.Complete) 672 return document2.queryCommandValue("FontName") as string; 673 } 674 675 /// <summary> 676 /// 设置 当前选中区的字体大小 677 /// </summary> 678 /// <returns></returns> 679 public void SetSelFontSize(int fontSize) 680 { 681 document.ExecCommand("FontSize", false, fontSize); 682 } 683 684 /// <summary> 685 /// 获得 当前选中区的字体大小 686 /// </summary> 687 /// <returns></returns> 688 public int GetSelFontSize() 689 { 690 return (int)document2.queryCommandValue("FontSize"); 691 } 692 693 #region ColorToHtml 694 /// <summary> 695 /// 颜色转换成HTML格式的颜色 696 /// </summary> 697 /// <param name="color"></param> 698 /// <returns>#FF0000</returns> 699 private string ColorToHtmlColor(Color color) 700 { 701 return string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B); 702 } 703 #endregion 704 705 /// <summary> 706 /// 设置 当前选中区的前景(文本)颜色 707 /// </summary> 708 /// <returns></returns> 709 public void SetSelFontForeColor(Color color) 710 { 711 document.ExecCommand("ForeColor", false, ColorToHtmlColor(color)); 712 } 713 714 /// <summary> 715 /// 获得 当前选中区的前景(文本)颜色 716 /// </summary> 717 /// <returns></returns> 718 public Color GetSelFontForeColor() 719 { 720 return (Color)document2.queryCommandValue("ForeColor"); 721 } 722 723 /// <summary> 724 /// 设置 当前选中区的背景颜色 725 /// </summary> 726 /// <returns></returns> 727 public void SetSelFontBackColor(Color color) 728 { 729 document.ExecCommand("BackColor", false, ColorToHtmlColor(color)); 730 } 731 732 /// <summary> 733 /// 获得 当前选中区的背景颜色 734 /// </summary> 735 /// <returns></returns> 736 public Color GetSelFontBackColor() 737 { 738 return (Color)document2.queryCommandValue("BackColor"); 739 } 740 741 742 /// <summary> 743 /// 设置 切换当前选中区的粗体显示与否。 744 /// </summary> 745 /// <returns></returns> 746 public void SetSelFontStyleBold() 747 { 748 document.ExecCommand("Bold", false, null); 749 } 750 751 /// <summary> 752 /// 获得 切换当前选中区的粗体显示与否。 753 /// </summary> 754 /// <returns></returns> 755 public bool GetSelFontStyleBold() 756 { 757 return (bool)document2.queryCommandValue("Bold"); 758 } 759 760 /// <summary> 761 /// 设置 切换当前选中区斜体显示与否。 762 /// </summary> 763 /// <returns></returns> 764 public void SetSelFontStyleItalic() 765 { 766 document.ExecCommand("Italic", false, null); 767 } 768 769 /// <summary> 770 /// 获得 切换当前选中区斜体显示与否。 771 /// </summary> 772 /// <returns></returns> 773 public bool GetSelFontStyleItalic() 774 { 775 return (bool)document2.queryCommandValue("Italic"); 776 } 777 778 /// <summary> 779 /// 设置 切换当前选中区的下划线显示与否。 780 /// </summary> 781 /// <returns></returns> 782 public void SetSelFontStyleUnderline() 783 { 784 document.ExecCommand("Underline", false, null); 785 } 786 787 /// <summary> 788 /// 获得 切换当前选中区的下划线显示与否。 789 /// </summary> 790 /// <returns></returns> 791 public bool GetSelFontStyleUnderline() 792 { 793 return (bool)document2.queryCommandValue("Underline"); 794 } 795 796 /// <summary> 797 /// 设置 选中的字体 删除线 798 /// </summary> 799 /// <returns></returns> 800 public void SetSelFontStyleStrikeThrough() 801 { 802 document.ExecCommand("StrikeThrough", false, null); 803 } 804 805 /// <summary> 806 /// 获得 选中的字体 删除线 807 /// </summary> 808 /// <returns></returns> 809 public bool GetSelFontStyleStrikeThrough() 810 { 811 return (bool)document2.queryCommandValue("StrikeThrough"); 812 } 813 814 /// <summary> 815 /// 设置 选中的字体 格式 816 /// </summary> 817 /// <returns></returns> 818 public void SetSelFontFormat() 819 { 820 document.ExecCommand("RemoveFormat", false, null); 821 } 822 823 ///// <summary> 824 ///// 获得 选中的字体 格式 825 ///// </summary> 826 ///// <returns></returns> 827 public bool GetSetSelFontFormat() 828 { 829 return (bool)document2.queryCommandValue("RemoveFormat"); 830 } 831 832 833 /// <summary> 834 /// 获取选中部分的标签。 835 /// </summary> 836 /// <returns>标签。</returns> 837 public string GetSelectionTag() 838 { 839 return (string)document2.queryCommandValue("FormatBlock"); 840 } 841 842 /// <summary> 843 /// 设置选中部分的标签。 844 /// </summary> 845 /// <param name="value">标签。</param> 846 public void SetSelectionTag(string value) 847 { 848 if (value == null) 849 { 850 document.ExecCommand("FormatBlock", false, "<P>"); 851 document.ExecCommand("RemoveParaFormat", false, null); 852 return; 853 } 854 try 855 { 856 document.ExecCommand("FormatBlock", false, "<" + value + ">"); 857 } 858 catch 859 { 860 //document.SetSelectedHtml(String.Format("<{0}>{1}</{0}>", value, document.GetSelectedHtml())); 861 } 862 } 863 864 865 /// <summary> 866 /// 设置 选中的字体 左对齐 867 /// </summary> 868 /// <returns></returns> 869 public void SetSelJustifyLeft() 870 { 871 document.ExecCommand("JustifyLeft", false, null); 872 } 873 874 /// <summary> 875 /// 获得 选中的字体 左对齐 876 /// </summary> 877 /// <returns></returns> 878 public bool GetSelJustifyLeft() 879 { 880 return (bool)document2.queryCommandValue("JustifyLeft"); 881 } 882 883 /// <summary> 884 /// 设置 选中的字体 中间对齐 885 /// </summary> 886 /// <returns></returns> 887 public void SetSelJustifyCenter() 888 { 889 document.ExecCommand("JustifyCenter", false, null); 890 } 891 892 /// <summary> 893 /// 获得 选中的字体 中间对齐 894 /// </summary> 895 /// <returns></returns> 896 public bool GetSelJustifyCenter() 897 { 898 return (bool)document2.queryCommandValue("JustifyCenter"); 899 } 900 901 /// <summary> 902 /// 设置 选中的字体 右边对齐 903 /// </summary> 904 /// <returns></returns> 905 public void SetSelJustifyRight() 906 { 907 document.ExecCommand("JustifyRight", false, null); 908 } 909 910 /// <summary> 911 /// 获得 选中的字体 右边对齐 912 /// </summary> 913 /// <returns></returns> 914 public bool GetSelJustifyRight() 915 { 916 return (bool)document2.queryCommandValue("JustifyRight"); 917 } 918 919 /// <summary> 920 /// 设置 选中的字体 两边对齐 921 /// </summary> 922 /// <returns></returns> 923 public void SetSelJustifyFull() 924 { 925 document.ExecCommand("JustifyFull", false, null); 926 } 927 928 /// <summary> 929 /// 获得 选中的字体 两边对齐 930 /// </summary> 931 /// <returns></returns> 932 public bool GetSelJustifyFull() 933 { 934 return (bool)document2.queryCommandValue("JustifyFull"); 935 } 936 937 938 /// <summary> 939 /// 设置 选中的字体 数字对齐 940 /// </summary> 941 /// <returns></returns> 942 public void SetSelOrderedList() 943 { 944 document.ExecCommand("InsertOrderedList", false, null); 945 } 946 947 /// <summary> 948 /// 获得 选中的字体 数字对齐 949 /// </summary> 950 /// <returns></returns> 951 public bool GetSelOrderedList() 952 { 953 return (bool)document2.queryCommandValue("InsertOrderedList"); 954 } 955 956 /// <summary> 957 /// 设置 选中的字体 圆点对齐 958 /// </summary> 959 /// <returns></returns> 960 public void SetSelUnorderedList() 961 { 962 document.ExecCommand("InsertUnorderedList", false, null); 963 } 964 965 /// <summary> 966 /// 获得 选中的字体 圆点对齐 967 /// </summary> 968 /// <returns></returns> 969 public bool GetSelUnorderedList() 970 { 971 return (bool)document2.queryCommandValue("InsertUnorderedList"); 972 } 973 974 /// <summary> 975 /// 设置 选中的字体 上标X2 976 /// </summary> 977 /// <returns></returns> 978 public void SetSelSuperScript() 979 { 980 document.ExecCommand("SuperScript", false, null); 981 } 982 983 /// <summary> 984 /// 获得 选中的字体 上标X2 985 /// </summary> 986 /// <returns></returns> 987 public bool GetSelSuperScript() 988 { 989 return (bool)document2.queryCommandValue("SuperScript"); 990 } 991 992 /// <summary> 993 /// 设置 选中的字体 下标X2 994 /// </summary> 995 /// <returns></returns> 996 public void SetSelSubScript() 997 { 998 document.ExecCommand("SubScript", false, null); 999 } 1000 1001 /// <summary> 1002 /// 获得 选中的字体 下标X2 1003 /// </summary> 1004 /// <returns></returns> 1005 public bool GetSelSubScript() 1006 { 1007 return (bool)document2.queryCommandValue("SubScript"); 1008 } 1009 1010 /// <summary> 1011 /// 设置 选中的字体 右缩进 1012 /// 增加空格才是正确的 默认的做法不符合这样的 1013 /// </summary> 1014 /// <returns></returns> 1015 public void SetSelRightIndent() 1016 { 1017 document.ExecCommand("Indent", false, null); 1018 } 1019 1020 /// <summary> 1021 /// 获得 选中的字体 右缩进 1022 /// 增加空格才是正确的 默认的做法不符合这样的 1023 /// </summary> 1024 /// <returns></returns> 1025 public bool GetSelRightIndent() 1026 { 1027 return (bool)document2.queryCommandValue("Indent"); 1028 } 1029 1030 /// <summary> 1031 /// 设置 选中的字体 左缩进 1032 /// 增加空格才是正确的 默认的做法不符合这样的 1033 /// </summary> 1034 /// <returns></returns> 1035 public void SetSelLeftIndent() 1036 { 1037 document.ExecCommand("Outdent", false, null); 1038 } 1039 1040 /// <summary> 1041 /// 获得 选中的字体 左缩进 1042 /// 增加空格才是正确的 默认的做法不符合这样的 1043 /// </summary> 1044 /// <returns></returns> 1045 public bool GetSelLeftIndent() 1046 { 1047 return (bool)document2.queryCommandValue("Outdent"); 1048 } 1049 1050 #endregion 1051 1052 #region 嵌入相关 1053 1054 1055 /// <summary> 1056 /// 设置 选中的字体 超链接 1057 /// </summary> 1058 /// <returns></returns> 1059 public void SetSelHyperLink() 1060 { 1061 document.ExecCommand("CreateLink", false, null); 1062 } 1063 1064 /// <summary> 1065 /// 获得 选中的字体 超链接 1066 /// </summary> 1067 /// <returns></returns> 1068 public bool GetSelHyperLink() 1069 { 1070 return (bool)document2.queryCommandValue("CreateLink"); 1071 } 1072 1073 /// <summary> 1074 /// 设置 取消选中的字体 超链接 1075 /// </summary> 1076 /// <returns></returns> 1077 public void SetSelUnLink() 1078 { 1079 document.ExecCommand("Unlink", false, null); 1080 } 1081 1082 /// <summary> 1083 /// 获得 取消选中的字体 超链接 1084 /// </summary> 1085 /// <returns></returns> 1086 public bool GetSelUnLink() 1087 { 1088 return (bool)document2.queryCommandValue("Unlink"); 1089 } 1090 1091 /// <summary> 1092 /// 设置 选中的 书签 1093 /// </summary> 1094 /// <returns></returns> 1095 public void SetSelBookmark() 1096 { 1097 document.ExecCommand("CreateBookmark", false, null); 1098 } 1099 1100 /// <summary> 1101 /// 获得 选中的 书签 1102 /// </summary> 1103 /// <returns></returns> 1104 public bool GetSelBookmark() 1105 { 1106 return (bool)document2.queryCommandValue("CreateBookmark"); 1107 } 1108 1109 /// <summary> 1110 /// 取消 设置 选中的 书签 1111 /// </summary> 1112 /// <returns></returns> 1113 public void SetSelUnBookmark() 1114 { 1115 document.ExecCommand("UnBookmark", false, null); 1116 } 1117 1118 /// <summary> 1119 /// 取消 获得 选中的 书签 1120 /// </summary> 1121 /// <returns></returns> 1122 public bool GetSelUnBookmark() 1123 { 1124 return (bool)document2.queryCommandValue("UnBookmark"); 1125 } 1126 1127 /// <summary> 1128 /// 设置 插入图片 1129 /// </summary> 1130 /// <returns></returns> 1131 public void SetInsertImage() 1132 { 1133 document.ExecCommand("InsertImage", true, null); 1134 } 1135 1136 /// <summary> 1137 /// 设置 插入图片 1138 /// </summary> 1139 /// <param name="fileName">图片路径</param> 1140 public void SetInsertImage(string fileName) 1141 { 1142 document.ExecCommand("InsertImage", true, fileName); 1143 } 1144 1145 /// <summary> 1146 /// 获得 插入图片 1147 /// </summary> 1148 /// <returns></returns> 1149 public bool GetInsertImage() 1150 { 1151 return (bool)document2.queryCommandValue("InsertImage"); 1152 } 1153 1154 1155 1156 #endregion 1157 1158 #region 对话框 1159 /// <summary> 1160 /// 查找对话框 1161 /// </summary> 1162 public void ShowFindDialog() 1163 { 1164 1165 } 1166 1167 1168 #endregion 1169 1170 #region ShowMargin 1171 ///下面代码已经可以在InitializeComponent();的下面正常工作 1172 ///但无法在webbrowserCtrl1 的初始化中正常工作 1173 /// <summary> 1174 /// 去除边距 像richTextBoxShowMargin =false; 1175 /// 1176 /// 为了使代码能在主窗体InitializeComponent()下初始化有效增加了2句 1177 /// for (int i = 0; i < 1; i++) 1178 /// Application.DoEvents(); 1179 /// 1180 /// </summary> 1181 public void HideMargin() 1182 { 1183 for (int i = 0; i < 1; i++) 1184 { 1185 Application.DoEvents(); 1186 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1187 { 1188 document2.body.style.marginLeft = "0px"; 1189 document2.body.style.marginTop = "0px"; 1190 document2.body.style.padding = "0px"; 1191 } 1192 } 1193 } 1194 1195 /// <summary> 1196 /// 显示边距 1197 /// </summary> 1198 public void ShowMargin() 1199 { 1200 for (int i = 0; i < 1; i++) 1201 { 1202 Application.DoEvents(); 1203 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1204 { 1205 document2.body.style.marginLeft = "10px"; 1206 document2.body.style.marginTop = "15px"; 1207 document2.body.style.padding = "0px"; 1208 } 1209 } 1210 } 1211 1212 /// <summary> 1213 /// 判断是否有边距 1214 /// </summary> 1215 /// <returns>默认的是已经设置了边距</returns> 1216 public bool GetShowMargin() 1217 { 1218 try 1219 { 1220 for (int i = 0; i < 1; i++) 1221 { 1222 Application.DoEvents(); 1223 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1224 { 1225 document2.body.style.padding = "0px"; 1226 string marginLeft = document2.body.style.marginLeft.ToString();//"10px"; 1227 string marginTop = document2.body.style.marginTop.ToString();// "15px"; 1228 1229 if (marginLeft == "10px" || marginTop == "15px") 1230 { 1231 return true; 1232 } 1233 } 1234 } 1235 return false; 1236 } 1237 catch (System.Exception ex) 1238 { 1239 //默认就是ShowMargin 1240 return true; 1241 } 1242 } 1243 1244 1245 #endregion 1246 1247 #region BordeStyle 1248 ///注 1249 ///下面这种写法要求 1250 ///在获得属性之前 确定已经用该 属性设置过一次 1251 ///否则 1252 ///引起异常:未将对象引用设置到对象的实例。 1253 1254 1255 /// <summary> 1256 ///设置无边框 1257 ///default none Default. No border is drawn, regardless of any specified borderWidth. 1258 /// </summary> 1259 public void SetStyleNone() 1260 { 1261 for (int i = 0; i < 1; i++) 1262 { 1263 Application.DoEvents(); 1264 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1265 { 1266 document2.body.style.borderWidth = "0px"; 1267 document2.body.style.borderStyle = "none"; 1268 } 1269 } 1270 } 1271 /// <summary> 1272 ///获得 无边框 1273 ///default none Default. No border is drawn, regardless of any specified borderWidth. 1274 /// </summary> 1275 public bool GetStyleNone() 1276 { 1277 try 1278 { 1279 for (int i = 0; i < 1; i++) 1280 { 1281 Application.DoEvents(); 1282 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1283 { 1284 //document2.body.style.borderWidth = "0px"; 1285 //document2.body.style.borderStyle = "none"; 1286 1287 string borderStyle = document2.body.style.borderStyle.ToString();//"none"; 1288 string borderWidth = document2.body.style.borderWidth.ToString();// "0px"; 1289 1290 if (borderStyle == "none" || borderWidth == "0px") 1291 { 1292 return true; 1293 } 1294 } 1295 } 1296 return false; 1297 } 1298 catch (System.Exception ex) 1299 { 1300 MessageBox.Show(ex.Message); 1301 //默认就是StyleNone 1302 return true; 1303 } 1304 } 1305 1306 /// <summary> 1307 /// 设置为FixSingle 1308 ///FixedSingle Border is the same as inset, but is surrounded by an additional single line, drawn in colors based on the value. 1309 /// </summary> 1310 public void SetStyleFixSingle() 1311 { 1312 for (int i = 0; i < 1; i++) 1313 { 1314 Application.DoEvents(); 1315 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1316 { 1317 document2.body.style.borderWidth = "0.05cm"; 1318 document2.body.style.borderStyle = "window-inset"; 1319 } 1320 } 1321 } 1322 1323 /// <summary> 1324 ///获得 FixSingle 1325 ///FixedSingle Border is the same as inset, but is surrounded by an additional single line, drawn in colors based on the value. 1326 /// </summary> 1327 public bool GetStyleFixSingle() 1328 { 1329 try 1330 { 1331 for (int i = 0; i < 1; i++) 1332 { 1333 Application.DoEvents(); 1334 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1335 { 1336 string borderStyle = document2.body.style.borderStyle.ToString();//"none"; 1337 if (borderStyle == "window-inset") 1338 { 1339 return true; 1340 } 1341 } 1342 } 1343 return false; 1344 } 1345 catch (System.Exception ex) 1346 { 1347 MessageBox.Show(ex.Message); 1348 //默认是StyleNone 1349 return false; 1350 } 1351 } 1352 1353 1354 1355 /// <summary> 1356 /// 设置 Fixed3D 1357 /// Fixed3D 3-D inset is drawn in colors based on the value. 1358 /// </summary> 1359 public void SetStyleFixed3D() 1360 { 1361 for (int i = 0; i < 1; i++) 1362 { 1363 Application.DoEvents(); 1364 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1365 { 1366 document2.body.style.borderWidth = "0.05cm"; 1367 document2.body.style.borderStyle = "inset"; 1368 } 1369 } 1370 } 1371 1372 1373 /// <summary> 1374 ///获得 Fixed3D 1375 ///Fixed3D 3-D inset is drawn in colors based on the value. 1376 /// </summary> 1377 public bool GetStyleFixed3D() 1378 { 1379 try 1380 { 1381 for (int i = 0; i < 1; i++) 1382 { 1383 Application.DoEvents(); 1384 if (webBrowser1.ReadyState == WebBrowserReadyState.Complete) 1385 { 1386 string borderStyle = document2.body.style.borderStyle.ToString();//"none"; 1387 if (borderStyle == "inset") 1388 { 1389 return true; 1390 } 1391 } 1392 } 1393 return false; 1394 } 1395 catch (System.Exception ex) 1396 { 1397 MessageBox.Show(ex.Message); 1398 //默认是StyleNone 1399 return false; 1400 } 1401 } 1402 #endregion 1403 1404 #region HTML CTRL 1405 1406 1407 /// <summary> 1408 /// 设置 插入InsertButton 1409 /// 用按钮控件覆盖当前选中区。 1410 /// </summary> 1411 public void SetInsertButton() 1412 { 1413 document.ExecCommand("InsertButton", false, null); 1414 } 1415 1416 /// <summary> 1417 /// 获得 插入InsertButton 1418 /// 用按钮控件覆盖当前选中区。 1419 /// </summary> 1420 /// <returns></returns> 1421 public bool GetInsertButton() 1422 { 1423 return (bool)document2.queryCommandValue("InsertButton"); 1424 } 1425 1426 1427 /// <summary> 1428 /// 设置 插入InsertFieldset 1429 /// 用方框覆盖当前选中区。 1430 /// </summary> 1431 public void SetInsertFieldset() 1432 { 1433 document.ExecCommand("InsertFieldset", false, null); 1434 } 1435 1436 /// <summary> 1437 /// 获得 插入InsertFieldset 1438 /// 用方框覆盖当前选中区。 1439 /// </summary> 1440 /// <returns></returns> 1441 public bool GetInsertFieldset() 1442 { 1443 return (bool)document2.queryCommandValue("InsertFieldset"); 1444 } 1445 1446 1447 /// <summary> 1448 /// 设置 插入InsertHorizontalRule 1449 /// 用水平线覆盖当前选中区。 1450 /// </summary> 1451 public void SetInsertHorizontalRule() 1452 { 1453 document.ExecCommand("InsertHorizontalRule", false, null); 1454 } 1455 1456 /// <summary> 1457 /// 获得 插入 InsertHorizontalRule 1458 /// 用水平线覆盖当前选中区。 1459 /// </summary> 1460 /// <returns></returns> 1461 public bool GetInsertHorizontalRule() 1462 { 1463 return (bool)document2.queryCommandValue("InsertHorizontalRule"); 1464 } 1465 1466 /// <summary> 1467 /// 设置 插入InsertIFrame 1468 /// 用水平线覆盖当前选中区。 1469 /// </summary> 1470 public void SetInsertIFrame() 1471 { 1472 document.ExecCommand("InsertIFrame", false, null); 1473 } 1474 1475 /// <summary> 1476 /// 获得 插入InsertIFrame 1477 /// 用内嵌框架覆盖当前选中区。 1478 /// </summary> 1479 /// <returns></returns> 1480 public bool GetInsertIFrame() 1481 { 1482 return (bool)document2.queryCommandValue("InsertIFrame"); 1483 } 1484 1485 1486 /// <summary> 1487 /// 设置 插入InsertInputButton 1488 /// 用按钮控件覆盖当前选中区。 1489 /// </summary> 1490 public void SetInsertInputButton() 1491 { 1492 document.ExecCommand("InsertInputButton", false, null); 1493 } 1494 1495 /// <summary> 1496 /// 获得 插入InsertInputButton 1497 /// 用按钮控件覆盖当前选中区。 1498 /// </summary> 1499 /// <returns></returns> 1500 public bool GetInsertInputButton() 1501 { 1502 return (bool)document2.queryCommandValue("InsertIFrame"); 1503 } 1504 1505 /// <summary> 1506 /// 设置 插入InsertInputCheckbox 1507 /// 用复选框控件覆盖当前选中区。 1508 /// </summary> 1509 public void SetInsertInputCheckbox() 1510 { 1511 document.ExecCommand("InsertInputCheckbox", false, null); 1512 } 1513 1514 /// <summary> 1515 /// 获得 插入InsertInputCheckbox 1516 /// 用复选框控件覆盖当前选中区。 1517 /// </summary> 1518 /// <returns></returns> 1519 public bool GetInsertInputCheckbox() 1520 { 1521 return (bool)document2.queryCommandValue("InsertInputCheckbox"); 1522 } 1523 1524 /// <summary> 1525 /// 设置 插入InsertInputFileUpload 1526 /// 用文件上载控件覆盖当前选中区。 1527 /// </summary> 1528 public void SetInsertInputFileUpload() 1529 { 1530 document.ExecCommand("InsertInputFileUpload", false, null); 1531 } 1532 1533 /// <summary> 1534 /// 获得 插入InsertInputFileUpload 1535 /// 用文件上载控件覆盖当前选中区。 1536 /// </summary> 1537 /// <returns></returns> 1538 public bool GetInsertInputFileUpload() 1539 { 1540 return (bool)document2.queryCommandValue("InsertInputFileUpload"); 1541 } 1542 1543 1544 /// <summary> 1545 /// 设置 插入InsertInputHidden 1546 /// 插入隐藏控件覆盖当前选中区。 1547 /// </summary> 1548 public void SetInsertInputHidden() 1549 { 1550 document.ExecCommand("InsertInputHidden", false, null); 1551 } 1552 1553 /// <summary> 1554 /// 获得 插入InsertInputHidden 1555 /// 插入隐藏控件覆盖当前选中区。 1556 /// </summary> 1557 /// <returns></returns> 1558 public bool GetInsertInputHidden() 1559 { 1560 return (bool)document2.queryCommandValue("InsertInputHidden"); 1561 } 1562 1563 1564 /// <summary> 1565 /// 设置 插入InsertInputImage 1566 /// 用图像控件覆盖当前选中区。 1567 /// </summary> 1568 public void SetInsertInputImage() 1569 { 1570 document.ExecCommand("InsertInputImage", false, null); 1571 } 1572 1573 /// <summary> 1574 /// 获得 插入InsertInputImage 1575 ///用图像控件覆盖当前选中区。 1576 /// </summary> 1577 /// <returns></returns> 1578 public bool GetInsertInputImage() 1579 { 1580 return (bool)document2.queryCommandValue("InsertInputImage"); 1581 } 1582 1583 1584 /// <summary> 1585 /// 设置 插入InsertInputPassword 1586 /// 用密码控件覆盖当前选中区。 1587 /// </summary> 1588 public void SetInsertInputPassword() 1589 { 1590 document.ExecCommand("InsertInputPassword", false, null); 1591 } 1592 1593 /// <summary> 1594 /// 获得 插入InsertInputImage 1595 ///用密码控件覆盖当前选中区。 1596 /// </summary> 1597 /// <returns></returns> 1598 public bool GetInsertInputPassword() 1599 { 1600 return (bool)document2.queryCommandValue("InsertInputPassword"); 1601 } 1602 1603 /// <summary> 1604 /// 设置 插入InsertInputRadio 1605 /// 用单选钮控件覆盖当前选中区。 1606 /// </summary> 1607 public void SetInsertInputRadio() 1608 { 1609 document.ExecCommand("InsertInputRadio", false, null); 1610 } 1611 1612 /// <summary> 1613 /// 获得 插入InsertInputRadio 1614 ///用单选钮控件覆盖当前选中区。 1615 /// </summary> 1616 /// <returns></returns> 1617 public bool GetInsertInputRadio() 1618 { 1619 return (bool)document2.queryCommandValue("InsertInputRadio"); 1620 } 1621 1622 /// <summary> 1623 /// 设置 插入InsertInputReset 1624 /// 用重置控件覆盖当前选中区。。 1625 /// </summary> 1626 public void SetInsertInputReset() 1627 { 1628 document.ExecCommand("InsertInputReset", false, null); 1629 } 1630 1631 /// <summary> 1632 /// 获得 插入InsertInputReset 1633 ///用重置控件覆盖当前选中区。。 1634 /// </summary> 1635 /// <returns></returns> 1636 public bool GetInsertInputReset() 1637 { 1638 return (bool)document2.queryCommandValue("InsertInputReset"); 1639 } 1640 1641 /// <summary> 1642 /// 设置 插入InsertInputSubmit 1643 /// 用提交控件覆盖当前选中区。 1644 /// </summary> 1645 public void SetInsertInputSubmit() 1646 { 1647 document.ExecCommand("InsertInputSubmit", false, null); 1648 } 1649 1650 /// <summary> 1651 /// 获得 插入InsertInputSubmit 1652 ///用提交控件覆盖当前选中区。 1653 /// </summary> 1654 /// <returns></returns> 1655 public bool GetInsertInputSubmit() 1656 { 1657 return (bool)document2.queryCommandValue("InsertInputSubmit"); 1658 } 1659 1660 1661 /// <summary> 1662 /// 设置 插入InsertInputText 1663 /// 用文本控件覆盖当前选中区。。 1664 /// </summary> 1665 public void SetInsertInputText() 1666 { 1667 document.ExecCommand("InsertInputText", false, null); 1668 } 1669 1670 /// <summary> 1671 /// 获得 插入InsertInputText 1672 ///用文本控件覆盖当前选中区。。 1673 /// </summary> 1674 /// <returns></returns> 1675 public bool GetInsertInputText() 1676 { 1677 return (bool)document2.queryCommandValue("InsertInputText"); 1678 } 1679 1680 /// <summary> 1681 /// 设置 插入InsertMarquee 1682 ///用空字幕覆盖当前选中区。 1683 /// </summary> 1684 public void SetInsertMarquee() 1685 { 1686 document.ExecCommand("InsertMarquee", false, null); 1687 } 1688 1689 /// <summary> 1690 /// 获得 插入InsertMarquee 1691 ///用空字幕覆盖当前选中区。 1692 /// </summary> 1693 /// <returns></returns> 1694 public bool GetInsertMarquee() 1695 { 1696 return (bool)document2.queryCommandValue("InsertMarquee"); 1697 } 1698 1699 1700 /// <summary> 1701 /// 设置 插入InsertSelectDropdown 1702 ///用下拉框控件覆盖当前选中区。 1703 /// </summary> 1704 public void SetInsertSelectDropdown() 1705 { 1706 document.ExecCommand("InsertSelectDropdown", false, null); 1707 } 1708 1709 /// <summary> 1710 /// 获得 插入InsertSelectDropdown 1711 ///用下拉框控件覆盖当前选中区。 1712 /// </summary> 1713 /// <returns></returns> 1714 public bool GetInsertSelectDropdown() 1715 { 1716 return (bool)document2.queryCommandValue("InsertSelectDropdown"); 1717 } 1718 1719 /// <summary> 1720 /// 设置 插入InsertSelectListbox 1721 ///用列表框控件覆盖当前选中区。 1722 /// </summary> 1723 public void SetInsertSelectListbox() 1724 { 1725 document.ExecCommand("InsertSelectListbox", false, null); 1726 } 1727 1728 /// <summary> 1729 /// 获得 插入InsertSelectListbox 1730 ///用列表框控件覆盖当前选中区。 1731 /// </summary> 1732 /// <returns></returns> 1733 public bool GetInsertSelectListbox() 1734 { 1735 return (bool)document2.queryCommandValue("InsertSelectListbox"); 1736 } 1737 1738 /// <summary> 1739 /// 设置 插入InsertTextArea 1740 ///用多行文本输入控件覆盖当前选中区。 1741 /// </summary> 1742 public void SetInsertTextArea() 1743 { 1744 document.ExecCommand("InsertTextArea", false, null); 1745 } 1746 1747 /// <summary> 1748 /// 获得 插入InsertTextArea 1749 ///用多行文本输入控件覆盖当前选中区。 1750 /// </summary> 1751 /// <returns></returns> 1752 public bool GetInsertTextArea() 1753 { 1754 return (bool)document2.queryCommandValue("InsertTextArea"); 1755 } 1756 1757 #endregion 1758 } 1759 }