More word hacking...

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@249 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2011-01-15 10:31:29 +00:00
parent 053e360118
commit 7801d42aac
2 changed files with 44 additions and 8 deletions

View File

@ -2771,7 +2771,7 @@ namespace Nikse.SubtitleEdit.Forms
private void SpellCheckViaWord()
{
if (_subtitle == null | _subtitle.Paragraphs.Count == 0)
return;
return;
WordSpellChecker wordSpellChecker = null;
int totalCorrections = 0;
@ -2779,6 +2779,7 @@ namespace Nikse.SubtitleEdit.Forms
{
wordSpellChecker = new WordSpellChecker();
wordSpellChecker.NewDocument();
Application.DoEvents();
}
catch
{
@ -2792,17 +2793,20 @@ namespace Nikse.SubtitleEdit.Forms
index = 0;
_cancelWordSpellCheck = false;
foreach (Paragraph p in _subtitle.Paragraphs)
for (;index < _subtitle.Paragraphs.Count; index++)
{
Paragraph p = _subtitle.Paragraphs[index];
int errorsBefore;
int errorsAfter;
ShowStatus(string.Format(_language.SpellChekingViaWordXLineYOfX, version, index, _subtitle.Paragraphs.Count.ToString()));
SubtitleListview1.SelectIndexAndEnsureVisible(index - 1);
ShowStatus(string.Format(_language.SpellChekingViaWordXLineYOfX, version, index+1, _subtitle.Paragraphs.Count.ToString()));
SubtitleListview1.SelectIndexAndEnsureVisible(index);
string newText = wordSpellChecker.CheckSpelling(p.Text, out errorsBefore, out errorsAfter);
if (errorsAfter > 0)
{
wordSpellChecker.CloseDocument();
wordSpellChecker.Quit();
ShowStatus(string.Format(_language.SpellCheckAbortedXCorrections, totalCorrections));
Cursor = Cursors.Default;
return;
}
else if (errorsBefore != errorsAfter)
@ -2810,8 +2814,8 @@ namespace Nikse.SubtitleEdit.Forms
textBoxListViewText.Text = newText;
}
totalCorrections += (errorsBefore - errorsAfter);
index++;
Application.DoEvents();
if (_cancelWordSpellCheck)
break;
}

View File

@ -1,5 +1,8 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Nikse.SubtitleEdit.Logic
{
@ -9,10 +12,14 @@ namespace Nikse.SubtitleEdit.Logic
/// </summary>
internal class WordSpellChecker
{
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private object _wordApplication;
private object _wordDocument;
private Type _wordApplicationType;
private Type _wordDocumentType;
bool _firstShow = true;
public WordSpellChecker()
{
@ -75,12 +82,24 @@ namespace Nikse.SubtitleEdit.Logic
errorsBefore = int.Parse(spellingErrorsCount.ToString());
System.Runtime.InteropServices.Marshal.ReleaseComObject(spellingErrors);
// perform spell check
object p = Missing.Value;
_wordApplicationType.InvokeMember("Top", BindingFlags.SetProperty, null, _wordApplication, new object[] { -10000 }); // hide window - it's a hack
_wordApplicationType.InvokeMember("Visible", BindingFlags.SetProperty, null, _wordApplication, new object[] { true }); // set visible to true - otherwise it will appear in the background
_wordDocumentType.InvokeMember("CheckSpelling", BindingFlags.InvokeMethod, null, _wordDocument, new Object[] { p, p, p, p, p, p, p, p, p, p, p, p }); // 12 parameters
if (errorsBefore > 0)
{
if (_firstShow)
{
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(EnsureWordIsVisible);
bw.RunWorkerAsync();
_firstShow = false;
}
_wordApplicationType.InvokeMember("Top", BindingFlags.SetProperty, null, _wordApplication, new object[] { -10000 }); // hide window - it's a hack
_wordApplicationType.InvokeMember("Visible", BindingFlags.SetProperty, null, _wordApplication, new object[] { true }); // set visible to true - otherwise it will appear in the background
_wordDocumentType.InvokeMember("CheckSpelling", BindingFlags.InvokeMethod, null, _wordDocument, new Object[] { p, p, p, p, p, p, p, p, p, p, p, p }); // 12 parameters
}
// spell check error count
spellingErrors = _wordDocumentType.InvokeMember("SpellingErrors", BindingFlags.GetProperty, null, _wordDocument, null);
@ -98,5 +117,18 @@ namespace Nikse.SubtitleEdit.Logic
return resultText.ToString().TrimEnd(); // result needs a trimming at the end
}
void EnsureWordIsVisible(object sender, DoWorkEventArgs e)
{
int i = 0;
while (!e.Cancel && i <10)
{
Process[] processes = Process.GetProcessesByName("WINWORD");
if (processes.Length > 0)
SetForegroundWindow(processes[0].MainWindowHandle);
System.Threading.Thread.Sleep(100);
i++;
}
}
}
}