我已经将拼写检查合并到了我的win窗体C#项目中。这是我的代码。C#拼写检查问题
public void CheckSpelling()
{
try
{
// declare local variables to track error count
// and information
int SpellingErrors = 0;
string ErrorCountMessage = string.Empty;
// create an instance of a word application
Microsoft.Office.Interop.Word.Application WordApp =
new Microsoft.Office.Interop.Word.Application();
// hide the MS Word document during the spellcheck
//WordApp.WindowState = WdWindowState.wdWindowStateMinimize;
// check for zero length content in text area
if (this.Text.Length > 0)
{
WordApp.Visible = false;
// create an instance of a word document
_Document WordDoc = WordApp.Documents.Add(ref emptyItem,
ref emptyItem,
ref emptyItem,
ref oFalse);
// load the content written into the word doc
WordDoc.Words.First.InsertBefore(this.Text);
// collect errors form new temporary document set to contain
// the content of this control
Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = WordDoc.SpellingErrors;
SpellingErrors = docErrors.Count;
// execute spell check; assumes no custom dictionaries
WordDoc.CheckSpelling(ref oNothing, ref oIgnoreUpperCase, ref oAlwaysSuggest,
ref oNothing, ref oNothing, ref oNothing, ref oNothing, ref oNothing,
ref oNothing, ref oNothing, ref oNothing, ref oNothing);
// format a string to contain a report of the errors detected
ErrorCountMessage = "Spell check complete; errors detected: " + SpellingErrors;
// return corrected text to control's text area
object first = 0;
object last = WordDoc.Characters.Count - 1;
this.Text = WordDoc.Range(ref first, ref last).Text;
}
else
{
// if nothing was typed into the control, abort and inform user
ErrorCountMessage = "Unable to spell check an empty text box.";
}
WordApp.Quit(ref oFalse, ref emptyItem, ref emptyItem);
System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);
// return report on errors corrected
// - could either display from the control or change this to
// - return a string which the caller could use as desired.
// MessageBox.Show(ErrorCountMessage, "Finished Spelling Check");
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
拼写检查效果很好,唯一的问题是,当我尝试检查的主要形式因为某些原因模糊了移动的咒语。另外,当我关闭拼写检查器时,主窗体恢复正常。它似乎是打开微软的话,然后隐藏窗口,只允许看到拼写检查器。请帮忙。
我也在寻找一个WinForms拼写检查器。我很好奇:你还在使用NetSpell吗?首先,答案中的链接指向一篇近10年的文章(2003年)。此外,图像只显示一个对话框。这是否意味着它无法在RichTextBox控件中显示这些红色波浪线?谢谢。 – 2011-04-20 16:48:32