using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Text;
using onlyconnect;
namespace DemoHtmlApp
{
///
/// Summary description for Form1.
///
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.Button btnSource;
private System.Windows.Forms.Button btnPrint;
private System.Windows.Forms.CheckBox chkEditMode;
private System.Windows.Forms.Button btnCount;
private System.Windows.Forms.Button btnParseNodes;
private onlyconnect.HtmlEditor htmlEditor1;
private System.Windows.Forms.MenuStrip mainMenu1;
private System.Windows.Forms.ToolStripMenuItem menuItem1;
private System.Windows.Forms.ToolStripMenuItem mnuSetBackColor;
private System.Windows.Forms.ToolStripMenuItem mnuComposeFont;
private System.Windows.Forms.ToolStripMenuItem mnuSaveAs;
private System.Windows.Forms.ToolStripMenuItem mnuClearFormatting;
private System.Windows.Forms.ToolStripMenuItem mnuFind;
private System.Windows.Forms.ToolStripMenuItem mnuExecScript;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ImageList imageList1;
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem exampleToolStripMenuItem;
private ToolStripMenuItem example2ToolStripMenuItem;
private ToolStripMenuItem mnuLoadUrlNoDisplay;
private ToolStripMenuItem mnuSelection;
private ToolStripMenuItem mnuShowDialog;
private ToolStripMenuItem mnuSaveSelection;
private ToolStripMenuItem mnuRestoreSelection;
private int selStart = 0;
private ToolStripButton toolBarButton4;
private ToolStripButton toolBarButton5;
private ToolStripButton toolBarButton6;
private ToolStripButton tableToolBarButton;
private ToolStrip toolBar1;
private ToolStripButton cutToolStripButton;
private ToolStripButton copyToolStripButton;
private ToolStripButton pasteToolStripButton;
private int selLength = 0;
public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
this.btnLoad = new System.Windows.Forms.Button();
this.btnSource = new System.Windows.Forms.Button();
this.btnPrint = new System.Windows.Forms.Button();
this.chkEditMode = new System.Windows.Forms.CheckBox();
this.btnCount = new System.Windows.Forms.Button();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.btnParseNodes = new System.Windows.Forms.Button();
this.mainMenu1 = new System.Windows.Forms.MenuStrip();
this.menuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.mnuSetBackColor = new System.Windows.Forms.ToolStripMenuItem();
this.mnuComposeFont = new System.Windows.Forms.ToolStripMenuItem();
this.mnuSaveAs = new System.Windows.Forms.ToolStripMenuItem();
this.mnuClearFormatting = new System.Windows.Forms.ToolStripMenuItem();
this.mnuFind = new System.Windows.Forms.ToolStripMenuItem();
this.mnuExecScript = new System.Windows.Forms.ToolStripMenuItem();
this.mnuShowDialog = new System.Windows.Forms.ToolStripMenuItem();
this.mnuLoadUrlNoDisplay = new System.Windows.Forms.ToolStripMenuItem();
this.mnuSelection = new System.Windows.Forms.ToolStripMenuItem();
this.mnuSaveSelection = new System.Windows.Forms.ToolStripMenuItem();
this.mnuRestoreSelection = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.exampleToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.example2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.htmlEditor1 = new onlyconnect.HtmlEditor();
this.toolBarButton4 = new System.Windows.Forms.ToolStripButton();
this.toolBarButton5 = new System.Windows.Forms.ToolStripButton();
this.toolBarButton6 = new System.Windows.Forms.ToolStripButton();
this.tableToolBarButton = new System.Windows.Forms.ToolStripButton();
this.toolBar1 = new System.Windows.Forms.ToolStrip();
this.cutToolStripButton = new System.Windows.Forms.ToolStripButton();
this.copyToolStripButton = new System.Windows.Forms.ToolStripButton();
this.pasteToolStripButton = new System.Windows.Forms.ToolStripButton();
this.mainMenu1.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.toolBar1.SuspendLayout();
this.SuspendLayout();
//
// btnLoad
//
this.btnLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnLoad.Location = new System.Drawing.Point(7, 292);
this.btnLoad.Name = "btnLoad";
this.btnLoad.Size = new System.Drawing.Size(78, 34);
this.btnLoad.TabIndex = 3;
this.btnLoad.Text = "Load HTML";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// btnSource
//
this.btnSource.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnSource.Location = new System.Drawing.Point(97, 292);
this.btnSource.Name = "btnSource";
this.btnSource.Size = new System.Drawing.Size(78, 34);
this.btnSource.TabIndex = 4;
this.btnSource.Text = "View source";
this.btnSource.Click += new System.EventHandler(this.btnSource_Click);
//
// btnPrint
//
this.btnPrint.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnPrint.Location = new System.Drawing.Point(187, 291);
this.btnPrint.Name = "btnPrint";
this.btnPrint.Size = new System.Drawing.Size(78, 34);
this.btnPrint.TabIndex = 5;
this.btnPrint.Text = "Print with preview";
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
//
// chkEditMode
//
this.chkEditMode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.chkEditMode.Location = new System.Drawing.Point(7, 332);
this.chkEditMode.Name = "chkEditMode";
this.chkEditMode.Size = new System.Drawing.Size(224, 16);
this.chkEditMode.TabIndex = 8;
this.chkEditMode.Text = "Edit mode (requires document reload)";
this.chkEditMode.CheckedChanged += new System.EventHandler(this.chkEditMode_CheckedChanged);
//
// btnCount
//
this.btnCount.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnCount.Location = new System.Drawing.Point(277, 292);
this.btnCount.Name = "btnCount";
this.btnCount.Size = new System.Drawing.Size(78, 34);
this.btnCount.TabIndex = 6;
this.btnCount.Text = "Edit using the DOM";
this.btnCount.Click += new System.EventHandler(this.btnCount_Click);
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.White;
this.imageList1.Images.SetKeyName(0, "");
this.imageList1.Images.SetKeyName(1, "");
this.imageList1.Images.SetKeyName(2, "");
this.imageList1.Images.SetKeyName(3, "");
this.imageList1.Images.SetKeyName(4, "");
this.imageList1.Images.SetKeyName(5, "");
this.imageList1.Images.SetKeyName(6, "");
this.imageList1.Images.SetKeyName(7, "");
//
// btnParseNodes
//
this.btnParseNodes.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnParseNodes.Location = new System.Drawing.Point(361, 292);
this.btnParseNodes.Name = "btnParseNodes";
this.btnParseNodes.Size = new System.Drawing.Size(78, 34);
this.btnParseNodes.TabIndex = 7;
this.btnParseNodes.Text = "Parse nodes";
this.btnParseNodes.Click += new System.EventHandler(this.btnParseNodes_Click);
//
// mainMenu1
//
this.mainMenu1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.menuItem1});
this.mainMenu1.Location = new System.Drawing.Point(0, 0);
this.mainMenu1.Name = "mainMenu1";
this.mainMenu1.Size = new System.Drawing.Size(538, 24);
this.mainMenu1.TabIndex = 0;
//
// menuItem1
//
this.menuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.mnuSetBackColor,
this.mnuComposeFont,
this.mnuSaveAs,
this.mnuClearFormatting,
this.mnuFind,
this.mnuExecScript,
this.mnuShowDialog,
this.mnuLoadUrlNoDisplay,
this.mnuSelection,
this.mnuSaveSelection,
this.mnuRestoreSelection});
this.menuItem1.Name = "menuItem1";
this.menuItem1.Size = new System.Drawing.Size(41, 20);
this.menuItem1.Text = "Test";
//
// mnuSetBackColor
//
this.mnuSetBackColor.Name = "mnuSetBackColor";
this.mnuSetBackColor.Size = new System.Drawing.Size(205, 22);
this.mnuSetBackColor.Text = "Set backcolour";
this.mnuSetBackColor.Click += new System.EventHandler(this.mnuSetBackColor_Click);
//
// mnuComposeFont
//
this.mnuComposeFont.Name = "mnuComposeFont";
this.mnuComposeFont.Size = new System.Drawing.Size(205, 22);
this.mnuComposeFont.Text = "Set compose font";
this.mnuComposeFont.Click += new System.EventHandler(this.mnuComposeFont_Click);
//
// mnuSaveAs
//
this.mnuSaveAs.Name = "mnuSaveAs";
this.mnuSaveAs.Size = new System.Drawing.Size(205, 22);
this.mnuSaveAs.Text = "Save As..";
this.mnuSaveAs.Click += new System.EventHandler(this.mnuSaveAs_Click);
//
// mnuClearFormatting
//
this.mnuClearFormatting.Name = "mnuClearFormatting";
this.mnuClearFormatting.Size = new System.Drawing.Size(205, 22);
this.mnuClearFormatting.Text = "Clear formatting";
this.mnuClearFormatting.Click += new System.EventHandler(this.mnuClearFormatting_Click);
//
// mnuFind
//
this.mnuFind.Name = "mnuFind";
this.mnuFind.Size = new System.Drawing.Size(205, 22);
this.mnuFind.Text = "Find...";
this.mnuFind.Click += new System.EventHandler(this.mnuFind_Click);
//
// mnuExecScript
//
this.mnuExecScript.Name = "mnuExecScript";
this.mnuExecScript.Size = new System.Drawing.Size(205, 22);
this.mnuExecScript.Text = "Execute script";
this.mnuExecScript.Click += new System.EventHandler(this.mnuExecScript_Click);
//
// mnuShowDialog
//
this.mnuShowDialog.Name = "mnuShowDialog";
this.mnuShowDialog.Size = new System.Drawing.Size(205, 22);
this.mnuShowDialog.Text = "Show dialog";
this.mnuShowDialog.Click += new System.EventHandler(this.mnuShowDialog_Click);
//
// mnuLoadUrlNoDisplay
//
this.mnuLoadUrlNoDisplay.Name = "mnuLoadUrlNoDisplay";
this.mnuLoadUrlNoDisplay.Size = new System.Drawing.Size(205, 22);
this.mnuLoadUrlNoDisplay.Text = "Load Url Without Display";
this.mnuLoadUrlNoDisplay.Click += new System.EventHandler(this.mnuLoadUrlNoDisplay_Click);
//
// mnuSelection
//
this.mnuSelection.Name = "mnuSelection";
this.mnuSelection.Size = new System.Drawing.Size(205, 22);
this.mnuSelection.Text = "Show selection";
this.mnuSelection.Click += new System.EventHandler(this.mnuSelection_Click);
//
// mnuSaveSelection
//
this.mnuSaveSelection.Name = "mnuSaveSelection";
this.mnuSaveSelection.Size = new System.Drawing.Size(205, 22);
this.mnuSaveSelection.Text = "Save selection";
this.mnuSaveSelection.Click += new System.EventHandler(this.mnuSaveSelection_Click);
//
// mnuRestoreSelection
//
this.mnuRestoreSelection.Name = "mnuRestoreSelection";
this.mnuRestoreSelection.Size = new System.Drawing.Size(205, 22);
this.mnuRestoreSelection.Text = "Restore selection";
this.mnuRestoreSelection.Click += new System.EventHandler(this.mnuRestoreSelection_Click);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.exampleToolStripMenuItem,
this.example2ToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(125, 48);
//
// exampleToolStripMenuItem
//
this.exampleToolStripMenuItem.Name = "exampleToolStripMenuItem";
this.exampleToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.exampleToolStripMenuItem.Text = "Example";
//
// example2ToolStripMenuItem
//
this.example2ToolStripMenuItem.Name = "example2ToolStripMenuItem";
this.example2ToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.example2ToolStripMenuItem.Text = "Example2";
//
// htmlEditor1
//
this.htmlEditor1.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.htmlEditor1.ContextMenuStrip = this.contextMenuStrip1;
this.htmlEditor1.DefaultComposeSettings.BackColor = System.Drawing.Color.White;
this.htmlEditor1.DefaultComposeSettings.DefaultFont = new System.Drawing.Font("Arial", 15F);
this.htmlEditor1.DefaultComposeSettings.Enabled = false;
this.htmlEditor1.DefaultComposeSettings.ForeColor = System.Drawing.Color.Black;
this.htmlEditor1.DefaultPreamble = onlyconnect.EncodingType.Auto;
this.htmlEditor1.DocumentEncoding = onlyconnect.EncodingType.WindowsCurrent;
this.htmlEditor1.IsActiveContentEnabled = false;
this.htmlEditor1.Location = new System.Drawing.Point(7, 64);
this.htmlEditor1.Name = "htmlEditor1";
this.htmlEditor1.OptionKeyPath = "";
this.htmlEditor1.SelectionAlignment = System.Windows.Forms.HorizontalAlignment.Left;
this.htmlEditor1.SelectionBackColor = System.Drawing.Color.Empty;
this.htmlEditor1.SelectionBullets = false;
this.htmlEditor1.SelectionFont = null;
this.htmlEditor1.SelectionForeColor = System.Drawing.Color.Empty;
this.htmlEditor1.SelectionNumbering = false;
this.htmlEditor1.Size = new System.Drawing.Size(512, 221);
this.htmlEditor1.TabIndex = 1;
this.htmlEditor1.Text = "htmlEditor1";
this.htmlEditor1.BeforeNavigate += new onlyconnect.BeforeNavigateEventHandler(this.htmlEditor1_BeforeNavigate);
this.htmlEditor1.HtmlEvent += new onlyconnect.HtmlEventHandler(this.htmlEditor1_HtmlEvent);
this.htmlEditor1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.htmlEditor1_MouseDown);
//
// toolBarButton4
//
this.toolBarButton4.MergeIndex = 3;
this.toolBarButton4.Name = "toolBarButton4";
this.toolBarButton4.Size = new System.Drawing.Size(35, 22);
this.toolBarButton4.Text = "Bold";
this.toolBarButton4.Click += new System.EventHandler(this.toolBarButton4_Click);
//
// toolBarButton5
//
this.toolBarButton5.MergeIndex = 4;
this.toolBarButton5.Name = "toolBarButton5";
this.toolBarButton5.Size = new System.Drawing.Size(36, 22);
this.toolBarButton5.Text = "Italic";
this.toolBarButton5.Click += new System.EventHandler(this.toolBarButton5_Click);
//
// toolBarButton6
//
this.toolBarButton6.MergeIndex = 5;
this.toolBarButton6.Name = "toolBarButton6";
this.toolBarButton6.Size = new System.Drawing.Size(56, 22);
this.toolBarButton6.Text = "Heading";
this.toolBarButton6.Click += new System.EventHandler(this.toolBarButton6_Click);
//
// tableToolBarButton
//
this.tableToolBarButton.MergeIndex = 6;
this.tableToolBarButton.Name = "tableToolBarButton";
this.tableToolBarButton.Size = new System.Drawing.Size(40, 22);
this.tableToolBarButton.Text = "Table";
this.tableToolBarButton.Click += new System.EventHandler(this.toolBarButton7_Click);
//
// toolBar1
//
this.toolBar1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
this.toolBar1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.cutToolStripButton,
this.copyToolStripButton,
this.pasteToolStripButton,
this.toolBarButton4,
this.toolBarButton5,
this.toolBarButton6,
this.tableToolBarButton});
this.toolBar1.Location = new System.Drawing.Point(0, 24);
this.toolBar1.Name = "toolBar1";
this.toolBar1.Size = new System.Drawing.Size(538, 25);
this.toolBar1.TabIndex = 9;
//
// cutToolStripButton
//
this.cutToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("cutToolStripButton.Image")));
this.cutToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.cutToolStripButton.Name = "cutToolStripButton";
this.cutToolStripButton.Size = new System.Drawing.Size(46, 22);
this.cutToolStripButton.Text = "C&ut";
this.cutToolStripButton.Click += new System.EventHandler(this.cutToolStripButton_Click);
//
// copyToolStripButton
//
this.copyToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("copyToolStripButton.Image")));
this.copyToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.copyToolStripButton.Name = "copyToolStripButton";
this.copyToolStripButton.Size = new System.Drawing.Size(55, 22);
this.copyToolStripButton.Text = "&Copy";
this.copyToolStripButton.Click += new System.EventHandler(this.copyToolStripButton_Click);
//
// pasteToolStripButton
//
this.pasteToolStripButton.Image = ((System.Drawing.Image)(resources.GetObject("pasteToolStripButton.Image")));
this.pasteToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.pasteToolStripButton.Name = "pasteToolStripButton";
this.pasteToolStripButton.Size = new System.Drawing.Size(55, 22);
this.pasteToolStripButton.Text = "&Paste";
this.pasteToolStripButton.Click += new System.EventHandler(this.pasteToolStripButton_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(538, 357);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.btnParseNodes);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.btnCount);
this.Controls.Add(this.htmlEditor1);
this.Controls.Add(this.btnSource);
this.Controls.Add(this.chkEditMode);
this.Controls.Add(this.btnPrint);
this.Controls.Add(this.mainMenu1);
this.MainMenuStrip = this.mainMenu1;
this.Name = "frmMain";
this.Text = "HTML Editor demo (no MSTHML PIA dependency)";
this.mainMenu1.ResumeLayout(false);
this.mainMenu1.PerformLayout();
this.contextMenuStrip1.ResumeLayout(false);
this.toolBar1.ResumeLayout(false);
this.toolBar1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
this.loadHTML();
}
private void loadHTML()
{
StringBuilder sb = new StringBuilder();
sb.Append("
");
sb.Append("");
sb.Append("");
sb.Append("Example HTML Document
Some body text - click to see if scripts are enabled.
");
//sb.Append(@"Link: itwriting.com
");
sb.Append("");
sb.Append("Coloured text
");
sb.Append("Extended chars: " + "♠" + " - Spades. Will not work on Win98.
");
sb.Append("");
string s = sb.ToString();
this.htmlEditor1.LoadDocument(s);
}
private void btnSource_Click(object sender, System.EventArgs e)
{
string s = this.htmlEditor1.GetDocumentSource();
MessageBox.Show(s);
}
private void btnPrint_Click(object sender, System.EventArgs e)
{
htmlEditor1.Print(true, string.Empty, false);
}
private void chkEditMode_CheckedChanged(object sender, System.EventArgs e)
{
if (chkEditMode.Checked)
{
this.htmlEditor1.IsDesignMode = true;
}
else
{
this.htmlEditor1.IsDesignMode = false;
}
}
private void btnCount_Click(object sender, System.EventArgs e)
{
//demo DOM
IHTMLDocument2 doc = this.htmlEditor1.HtmlDocument2;
IHTMLElement el = doc.GetBody();
MessageBox.Show("This example shows the innerHTML of the body element");
MessageBox.Show(el.innerHTML);
MessageBox.Show("Now we change this using the DOM");
el.innerHTML = "Edited through the DOM
";
}
private void htmlEditor1_HtmlEvent(object s, onlyconnect.HtmlEventArgs e)
{
//IHTMLEventObj Event = e.Event;
//Write out the event for debug
//Debug.WriteLine("Event Info\nType: " + Event.GetType() + "\nElement: " +
// Event.SrcElement + " Key: " + Event.keyCode.ToString());
//example of how to swallow a keystroke
//if (Event.keyCode == 13)
//{
// Event.keyCode = 0;
// Debug.WriteLine("Swallowed return");
//}
}
private void mnuItemCopy_Click(object sender, System.EventArgs e)
{
this.htmlEditor1.Copy();
}
private void btnParseNodes_Click(object sender, System.EventArgs e)
{
//parse all the nodes in the document using recursion
if (htmlEditor1.Document != null)
{
IHTMLDOMNode nd = (IHTMLDOMNode)htmlEditor1.HtmlDocument2.GetBody();
parseNodes(nd);
}
}
private void parseNodes(IHTMLDOMNode nd)
{
Console.WriteLine(nd.nodeName);
if (nd.hasChildNodes())
{
IHTMLDOMChildrenCollection nds = (IHTMLDOMChildrenCollection)nd.childNodes;
foreach (IHTMLDOMNode childnd in nds)
{
parseNodes(childnd);
}
}
}
private void mnuSetBackColor_Click(object sender, System.EventArgs e)
{
Debug.WriteLine("Ready State: " + this.htmlEditor1.ReadyState);
this.htmlEditor1.SelectionBackColor = Color.Red;
}
private void mnuComposeFont_Click(object sender, System.EventArgs e)
{
this.htmlEditor1.DefaultComposeSettings.Enabled = true;
this.htmlEditor1.DefaultComposeSettings.DefaultFont = new Font("Comic Sans MS", 14);
}
private void mnuClearFormatting_Click(object sender, System.EventArgs e)
{
this.htmlEditor1.ClearSelectionFormatting();
}
private void mnuSaveAs_Click(object sender, System.EventArgs e)
{
this.htmlEditor1.SaveAs("YourDocumentName.html");
}
private void mnuFind_Click(object sender, System.EventArgs e)
{
this.htmlEditor1.ShowFindDialog();
}
private void mnuExecScript_Click(object sender, System.EventArgs e)
{
try
{
IHTMLWindow2 win = this.htmlEditor1.HtmlDocument2.GetParentWindow();
win.execScript("runscript();", "javascript");
}
catch (Exception exc)
{
MessageBox.Show("Error: " + exc.Message);
}
}
private void htmlEditor1_MouseDown(object sender, MouseEventArgs e)
{
}
private void mnuShowDialog_Click(object sender, EventArgs e)
{
HtmlDialog hd = new HtmlDialog();
hd.ShowDialog();
}
private void mnuLoadUrlNoDisplay_Click(object sender, EventArgs e)
{
HTMLDocument doc;
doc = new HTMLDocument();
string url = "http://www.itwriting.com/";
onlyconnect.utils.LoadUrl(ref doc, url, true);
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
//ugly code for quick test
long timeout = System.DateTime.Now.Ticks + (10000000 * 10); //allow 10 seconds
bool success = false;
while (true)
{
Application.DoEvents();
string rs = doc2.GetReadyState().ToLower();
if ((rs == "complete") | (rs == "interactive"))
{
success = true;
break;
}
if (timeout < System.DateTime.Now.Ticks)
{
MessageBox.Show("Timed out");
break;
}
}
if (success)
{
MessageBox.Show("Doc title is: " + doc2.GetTitle());
}
}
private void mnuSelection_Click(object sender, EventArgs e)
{
IHTMLSelectionObject sel = htmlEditor1.HtmlDocument2.GetSelection();
try
{
IHTMLTxtRange tr = (IHTMLTxtRange)sel.createRange();
MessageBox.Show("Selected HTML: " + tr.htmlText);
}
catch (Exception exc) //you will get an exception if it is a control range
{
MessageBox.Show("Exception: " + exc.Message);
}
}
private void mnuSaveSelection_Click(object sender, EventArgs e)
{
IHTMLSelectionObject sel = htmlEditor1.HtmlDocument2.GetSelection();
try
{
IHTMLTxtRange tr = (IHTMLTxtRange)sel.createRange();
IHTMLTxtRange temp = tr.duplicate();
this.selStart = -temp.move("character", -0x7FFFFFFF);
temp = tr.duplicate();
temp.collapse(false); // move to end
int iEndOffset = -temp.move("character", -0x7FFFFFFF);
this.selLength = iEndOffset - selStart;
}
catch (Exception exc) //you will get an exception if it is a control range
{
MessageBox.Show("Exception: " + exc.Message);
}
}
private void mnuRestoreSelection_Click(object sender, EventArgs e)
{
IHTMLBodyElement el = (IHTMLBodyElement)htmlEditor1.HtmlDocument2.GetBody();
try
{
IHTMLTxtRange tr = el.createTextRange();
tr.move("character", this.selStart);
tr.moveEnd("character", this.selLength);
tr.select();
}
catch (Exception exc) //you will get an exception if it is a control range
{
MessageBox.Show("Exception: " + exc.Message);
}
}
private void toolBarButton3_Click(object sender, EventArgs e)
{
this.htmlEditor1.Paste();
}
private void toolBarButton4_Click(object sender, EventArgs e)
{
this.htmlEditor1.HtmlDocument2.ExecCommand("bold", false, null);
}
private void toolBarButton5_Click(object sender, EventArgs e)
{
this.htmlEditor1.HtmlDocument2.ExecCommand("italic", false, null);
}
private void toolBarButton6_Click(object sender, EventArgs e)
{
//This is not intended as solid code, but gives an
//idea about how you might edit the DOM
//to provide editor features
//it's actually easier to edit styles using CSS as you can change
//an attribute more easily than the tagname
IHTMLElement el = null;
try
{
if (this.htmlEditor1.IsDesignMode)
{
el = htmlEditor1.CurrentElement;
if (el != null && el.tagName != "BODY")
{
el.outerHTML = "" + el.innerHTML + "
";
}
}
}
catch (Exception exc)
{
string sMessage = "Could not apply heading: " + exc.Message;
if (el != null)
{
sMessage += " The possible reason is the the tag is " + el.tagName + ".";
}
MessageBox.Show(sMessage);
}
}
private void toolBarButton7_Click(object sender, EventArgs e)
{
testHtmlTable();
MessageBox.Show("Disabled - see comment");
//commented out because we yet don't have interop definitions
//for the IHTMLTable etc
// try
// {
// mshtml.IHTMLTable t = (mshtml.IHTMLTable)htmlEditor1.Document.createElement("table");
// //set the cols
// t.cols = 3;
// //insert two rows at the end
// t.border = 2;
// for (int i = 0; i <= 4; i++)
// {
// mshtml.IHTMLTableRow tr = (mshtml.IHTMLTableRow) t.insertRow(-1);
// for (int j = 0; j <= 3; j++)
// {
// mshtml.IHTMLElement c = (mshtml.IHTMLElement) tr.insertCell(-1);
// c.innerHTML = j.ToString();
// }
// }
// mshtml.IHTMLDOMNode nd = (mshtml.IHTMLDOMNode) t;
// mshtml.IHTMLDOMNode body = (mshtml.IHTMLDOMNode)htmlEditor1.Document.body;
// body.appendChild(nd);
// }
// catch (Exception exc)
// {
// MessageBox.Show(exc.Message);
// }
}
private void cutToolStripButton_Click(object sender, EventArgs e)
{
if (this.htmlEditor1.IsDesignMode)
{
this.htmlEditor1.Cut();
}
}
private void copyToolStripButton_Click(object sender, EventArgs e)
{
this.htmlEditor1.Copy();
}
private void pasteToolStripButton_Click(object sender, EventArgs e)
{
this.htmlEditor1.Paste();
}
private void htmlEditor1_BeforeNavigate(object s, BeforeNavigateEventArgs e)
{
e.Cancel = true;
}
private void testHtmlTable()
{
IHTMLTable table = htmlEditor1.HtmlDocument2.CreateElement("table") as IHTMLTable;
table.align = "center";
table.bgColor = "ghostwhite";
table.border = "2";
table.borderColor = "lightblue";
table.width = "100%";
table.height = "80%";
table.cellPadding = 4;
table.cellSpacing = 2;
IHTMLTableRow row = table.insertRow(-1) as IHTMLTableRow;
row.vAlign = "middle";
IHTMLTableCell cell = row.insertCell(-1) as IHTMLTableCell;
cell.align = "right";
cell.width = "40%";
(cell as IHTMLElement).innerText = "cell 1";
IHTMLTableRow row2 = table.insertRow(-1) as IHTMLTableRow;
row2.vAlign = "bottom";
IHTMLTableCell cell2 = row2.insertCell(-1) as IHTMLTableCell;
cell2.align = "left";
cell2.width = "60%";
//(cell2 as IHTMLElement).innerText = "cell 2";
(cell2 as IHTMLElement).insertAdjacentHTML("beforeEnd", "cell 2 (contentEditable = false)");
IHTMLDOMNode cell2Node = cell2 as IHTMLDOMNode;
IHTMLDOMNode spanNode = cell2Node.childNodes.item(0) as IHTMLDOMNode;
(spanNode as IHTMLElement3).contentEditable = "false";
IHTMLTable2 table2 = table as IHTMLTable2;
IHTMLTableRow movedRow = table2.moveRow(0, 1) as IHTMLTableRow;
IHTMLTable3 table3 = table as IHTMLTable3;
table3.summary = "summary for table";
IHTMLDocument2 doc = this.htmlEditor1.HtmlDocument2;
IHTMLElement body = doc.GetBody();
IHTMLDOMNode rootNode = body as IHTMLDOMNode;
rootNode.appendChild(table as IHTMLDOMNode);
string html = (table as IHTMLElement).outerHTML;
MessageBox.Show(html);
}
}
}