|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionA while ago, I was working on a chat application where one of the chat clients was web based and written with ASP.NET 2.0. The other chat client was a Windows Forms based .NET application written in C#. I needed a rich text editor for my WinForms client that could output HTML, so that the ASP.NET client could display the formatting from the WinForms client. This was ruled out while using the RTF text box. The solution I worked out was to use a This article explains solutions to most of the issues involved in building an editor control from a Setting Design ModeApplying design mode and establishing an editing template for the document occurs automatically when using the component. But, for reference, here is a brief explanation of how it works. Applying design mode requires using a COM interface: adding a reference to "Microsoft HTML Object Library", which resolves to MSHTML, and adding a 'using' of 'MSHTML'. It is necessary to add a body to the control before you can apply changes to the DOM document. To do this, you can simply apply some text to the webBrowser1.DocumentText = "<html><body></body></html>"
Next, get a reference to the new IHTMLDocument2 doc =
webBrowser1.Document.DomDocument as IHTMLDocument2;
doc.designMode = "On";
Finally, I replace the context menu for the web browser control so the default IE browser context menu doesn't show up. webBrowser1.Document.ContextMenuShowing +=
new HtmlElementEventHandler(Document_ContextMenuShowing);
The browser is now in design mode, with a custom method to display the context menu. Applying FormattingYou can apply formatting and editor functions to a browser control in design mode with the Here are several examples: public void Cut()
{
webBrowser1.Document.ExecCommand("Cut", false, null);
}
public void Paste()
{
webBrowser1.Document.ExecCommand("Paste", false, null);
}
public void Copy()
{
webBrowser1.Document.ExecCommand("Copy", false, null);
}
Some commands will toggle the formatting state of the current selection. public void Bold()
{
webBrowser1.Document.ExecCommand("Bold", false, null);
}
public void Italic()
{
webBrowser1.Document.ExecCommand("Italic", false, null);
}
Synchronizing Formatting Buttons with Selected TextThis is a bit more tricky than applying formatting commands to the browser control. I literally query the state of the browser editor selection every 200 ms, and set the toolbar formatting buttons based on this. private void timer_Tick(object sender, EventArgs e)
{
SetupKeyListener();
boldButton.Checked = IsBold();
italicButton.Checked = IsItalic();
underlineButton.Checked = IsUnderline();
orderedListButton.Checked = IsOrderedList();
unorderedListButton.Checked = IsUnorderedList();
linkButton.Enabled = SelectionType == SelectionType.Text;
UpdateFontComboBox();
UpdateFontSizeComboBox();
if (Tick != null) Tick();
}
You'll notice that there is a I do this by using the COM document interface retrieved from the IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
Then I use public bool IsBold()
{
return doc.queryCommandState("Bold");
}
public bool IsItalic()
{
return doc.queryCommandState("Italic");
}
public bool IsUnderline()
{
return doc.queryCommandState("Underline");
}
The link button and font controls are managed in a similar way, but I'll leave that for you to check out in the code. FocusingFocusing the control isn't necessarily simple either. The private void SuperFocus()
{
if (webBrowser1.Document != null &&
webBrowser1.Document.Body != null)
webBrowser1.Document.Body.Focus();
}
Of course, you never need to call this method directly. Calling the Retrieving Text or HTMLThe Linking to the Component AssemblyIn Visual Studio 2005, you can link to an assembly (add a reference), even if the assembly is an executable. The editor is written as a component embedded in a form, so you can add this component to the control palette and drag and drop into your application. The control's name is ConclusionThe .NET 2.0 History
|
||||||||||||||||||||||