C#
using DotNetBrowser; using DotNetBrowser.WPF; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; namespace SpellCheckerSample { class Program { public class WindowMain : Window { private WPFBrowserView browserView; private Button frLanguageButton; private Button enLanguageButton; private Grid layout; public WindowMain() { layout = new Grid(); ColumnDefinition gridCol1 = new ColumnDefinition(); layout.ColumnDefinitions.Add(gridCol1); RowDefinition gridRow1 = new RowDefinition(); gridRow1.Height = new GridLength(45); RowDefinition gridRow2 = new RowDefinition(); gridRow2.Height = new GridLength(45); RowDefinition gridRow3 = new RowDefinition(); layout.RowDefinitions.Add(gridRow1); layout.RowDefinitions.Add(gridRow2); layout.RowDefinitions.Add(gridRow3); Content = layout; enLanguageButton = new Button(); enLanguageButton.Content = "English"; enLanguageButton.Height = 23; enLanguageButton.Click += enLanguageButton_Click; Grid.SetRow(enLanguageButton, 0); Grid.SetColumn(enLanguageButton, 0); frLanguageButton = new Button(); frLanguageButton.Content = "French"; frLanguageButton.Height = 23; frLanguageButton.Click += frLanguageButton_Click; Grid.SetRow(frLanguageButton, 1); Grid.SetColumn(frLanguageButton, 0); Browser browser = BrowserFactory.Create(); browserView = new WPFBrowserView(browser); browser.ContextMenuHandler = new MyContextMenuHandler((FrameworkElement)browserView, browser); // Enable SpellChecker service. browser.Context.SpellCheckerService.Enabled = true; // Configure SpellChecker's language. browser.Context.SpellCheckerService.Language = "en-US"; Grid.SetRow(browserView, 2); Grid.SetColumn(browserView, 0); layout.Children.Add(enLanguageButton); layout.Children.Add(frLanguageButton); layout.Children.Add(browserView); Width = 1024; Height = 768; this.Loaded += WindowMain_Loaded; } private void frLanguageButton_Click(object sender, RoutedEventArgs e) { browserView.Browser.Context.SpellCheckerService.Language = "fr-FR"; } private void enLanguageButton_Click(object sender, RoutedEventArgs e) { browserView.Browser.Context.SpellCheckerService.Language = "en-US"; } void WindowMain_Loaded(object sender, RoutedEventArgs e) { browserView.Browser.LoadHTML("<html><body><textarea rows='20' cols='30'>" + "Smple text with mitake. \r\n \r\n Exmple de texte avec ereur.</textarea></body></html>"); } [STAThread] public static void Main() { Application app = new Application(); WindowMain wnd = new WindowMain(); app.Run(wnd); var browser = wnd.browserView.Browser; wnd.browserView.Dispose(); browser.Dispose(); } private class MyContextMenuHandler : ContextMenuHandler { private FrameworkElement component; private Browser browser; public MyContextMenuHandler(FrameworkElement parentComponent, Browser browser) { this.component = parentComponent; this.browser = browser; } public void ShowContextMenu(ContextMenuParams parameters) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { if (parameters.DictionarySuggestions.Count > 0) { this.component.ContextMenu = CreatePopupMenu(parameters); this.component.ContextMenu.IsOpen = true; } else if (this.component.ContextMenu != null) { this.component.ContextMenu.Items.Clear(); this.component.ContextMenu.Width = 0; this.component.ContextMenu.Height = 0; } })); } private static System.Windows.Controls.ContextMenu CreatePopupMenu(ContextMenuParams parameters) { System.Windows.Controls.ContextMenu result = new System.Windows.Controls.ContextMenu(); // Add suggestions menu items. List<String> suggestions = parameters.DictionarySuggestions; foreach (String suggestion in suggestions) { result.Items.Add(CreateMenuItem(suggestion, true, delegate { parameters.Browser.ReplaceMisspelledWord(suggestion); })); } if (suggestions.Count > 0) { // Add the "Add to Dictionary" menu item. result.Items.Add(new Separator()); result.Items.Add(CreateMenuItem("Add to Dictionary", true, delegate { String misspelledWord = parameters.MisspelledWord; parameters.Browser.AddWordToSpellCheckerDictionary(misspelledWord); })); } return result; } private static MenuItem CreateMenuItem(string item, bool isEnabled, RoutedEventHandler clickHandler) { MenuItem result = new MenuItem(); result.Header = item; result.IsEnabled = isEnabled; result.Click += clickHandler; return result; } } } } }
VB.NET
Imports DotNetBrowser Imports DotNetBrowser.WPF Imports System.Windows Imports System.Windows.Controls Namespace SpellCheckerSample Class Program Public Class WindowMain Inherits Window Private browserView As WPFBrowserView Private frLanguageButton As Button Private enLanguageButton As Button Private layout As Grid Public Sub New() layout = New Grid() Dim gridCol1 As ColumnDefinition = New ColumnDefinition() layout.ColumnDefinitions.Add(gridCol1) Dim gridRow1 As RowDefinition = New RowDefinition() gridRow1.Height = New GridLength(45) Dim gridRow2 As RowDefinition = New RowDefinition() gridRow2.Height = New GridLength(45) Dim gridRow3 As RowDefinition = New RowDefinition() layout.RowDefinitions.Add(gridRow1) layout.RowDefinitions.Add(gridRow2) layout.RowDefinitions.Add(gridRow3) Content = layout enLanguageButton = New Button() enLanguageButton.Content = "English" enLanguageButton.Height = 23 AddHandler enLanguageButton.Click, AddressOf enLanguageButton_Click Grid.SetRow(enLanguageButton, 0) Grid.SetColumn(enLanguageButton, 0) frLanguageButton = New Button() frLanguageButton.Content = "French" frLanguageButton.Height = 23 AddHandler frLanguageButton.Click, AddressOf frLanguageButton_Click Grid.SetRow(frLanguageButton, 1) Grid.SetColumn(frLanguageButton, 0) Dim browser As Browser = BrowserFactory.Create() browserView = New WPFBrowserView(browser) browser.ContextMenuHandler = New MyContextMenuHandler(CType(browserView, FrameworkElement), browser) browser.Context.SpellCheckerService.Enabled = True browser.Context.SpellCheckerService.Language = "en-US" Grid.SetRow(browserView, 2) Grid.SetColumn(browserView, 0) layout.Children.Add(enLanguageButton) layout.Children.Add(frLanguageButton) layout.Children.Add(browserView) Width = 1024 Height = 768 AddHandler Me.Loaded, AddressOf WindowMain_Loaded End Sub Private Sub frLanguageButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) browserView.Browser.Context.SpellCheckerService.Language = "fr-FR" End Sub Private Sub enLanguageButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) browserView.Browser.Context.SpellCheckerService.Language = "en-US" End Sub Private Sub WindowMain_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) browserView.Browser.LoadHTML( "<html><body><textarea rows='20' cols='30'>" & "Smple text with mitake. " & vbCrLf & " " & vbCrLf & " Exmple de texte avec ereur.</textarea></body></html>") End Sub <STAThread> Public Shared Sub Main() Dim app As Application = New Application() Dim wnd As WindowMain = New WindowMain() app.Run(wnd) Dim browser = wnd.browserView.Browser wnd.browserView.Dispose() browser.Dispose() End Sub Private Class MyContextMenuHandler Implements ContextMenuHandler Private component As FrameworkElement Private browser As Browser Public Sub New(ByVal parentComponent As FrameworkElement, ByVal browser As Browser) Me.component = parentComponent Me.browser = browser End Sub Public Sub ShowContextMenu(ByVal parameters As ContextMenuParams) _ Implements ContextMenuHandler.ShowContextMenu Application.Current.Dispatcher.BeginInvoke(New Action(Sub() If parameters.DictionarySuggestions.Count > 0 Then Me.component.ContextMenu = CreatePopupMenu(parameters) Me.component.ContextMenu.IsOpen = True ElseIf Me.component.ContextMenu IsNot Nothing Then Me.component.ContextMenu.Items.Clear() Me.component.ContextMenu.Width = 0 Me.component.ContextMenu.Height = 0 End If End Sub)) End Sub Private Shared Function CreatePopupMenu(ByVal parameters As ContextMenuParams) _ As System.Windows.Controls.ContextMenu Dim result As System.Windows.Controls.ContextMenu = New System.Windows.Controls.ContextMenu() Dim suggestions As List(Of String) = parameters.DictionarySuggestions For Each suggestion As String In suggestions result.Items.Add(CreateMenuItem(suggestion, True, sub() parameters.Browser.ReplaceMisspelledWord(suggestion) End sub)) Next If suggestions.Count > 0 Then result.Items.Add(New Separator()) result.Items.Add(CreateMenuItem("Add to Dictionary", True, Function() Dim misspelledWord As String = parameters.MisspelledWord parameters.Browser.AddWordToSpellCheckerDictionary(misspelledWord) End Function)) End If Return result End Function Private Shared Function CreateMenuItem(ByVal item As String, ByVal isEnabled As Boolean, ByVal clickHandler As RoutedEventHandler) As MenuItem Dim result As MenuItem = New MenuItem() result.Header = item result.IsEnabled = isEnabled AddHandler result.Click, clickHandler Return result End Function End Class End Class End Class End Namespace