Recently, I am preparing for the CET-4 exam and got a doc document for "Class 4 high-frequency words" from my classmates. The document only has words and does not have phonetic symbols or explanations. It is very troublesome to translate one by one manually. Therefore, I have specially created a batch translation of words using Delphi and Kingsoft PowerWord 2002, and can directly save the translation results as RTF files. The program interface is as follows:
Principle analysis:
Use the translation function of "Kingsoft PowerWord 2002" to translate words, use Delphi to obtain the handle of the control that translates the result in "Kingsoft PowerWord 2002", and use the paste and copy function to obtain the translation results.
API function description:
HWND FindWindow(
LPCTSTR lpClassName, //The class name of the form to be searched for
LPCTSTR lpWindowName //The title name of the form to be searched
); //Find the first child window that matches the specified conditions
HWND FindWindowEx(
HWND hwndParent, //The handle of the parent window in which the child is searched
HWND hwndChildAfter, //Start search after this form
LPCTSTR lpszClass, // The class name of the form to be searched for
LPCTSTR lpszWindow // Title name of the form to be searched
); //Find the first child window that matches the specified conditions in the specified form list
BOOL ShowWindow(
HWND hWnd, //The handle of the form
int nCmdShow //How to display the form
); //Specify the visibility of the window
BOOL BringWindowToTop(
HWND hWnd // Handle of the form
); //Bring the specified window to the top of the window list
BOOL SetForegroundWindow(
HWND hWnd //The handle of the form
); //Set the window as the foreground program of the system
HWND SetFocus(
HWND hWnd //The handle of the focused form
); //Focus the window
VOID keybd_event(
BYTE bVk, // virtual-key code
BYTE bScan, // hardware scan code
DWord dwFlags, // flags specifying various function options
DWORD dwExtraInfo // additional data associated with keystroke
); //Simulate the generation of keys
Here we provide a statement of the basic API, specific usage methods, and you can use other relevant information.
Specific analysis:
First, use Spy++ tool to analyze "Kingsoft PowerWord 2002", and the analysis results are as follows:
The name of the form of Kingsoft PowerWord 2002 is: Kingsoft PowerWord 2002
Kingsoft PowerWord 2002's word input control class name: Edit (a child form of Combobox )
Kingsoft PowerWord 2002's translation result control class name: XDICT_ExplainView
Program interface:
A Timer control (Timer1, whose interval is 3 seconds), a Memo control (MList), and two RichEdit controls (RTrans, RConv), the specific code is as follows:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Clipbrd, Buttons, ExtCtrls, Menus;
type
TForm1 = class (TForm)
MList: TMemo;
RTrans: TRichEdit;
Button1: TButton;
Timer1: TTimer;
Button2: TButton;
RConv: TRichEdit;
Button3: TButton;
od: TOpenDialog;
RichEdit3: TRichEdit;
MainMenu1: TMainMenu;
F1: TMenuItem;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
C1: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
RTF1: TMenuItem;
Panel1: TPanel;
PRogressBar1: TProgressBar;
Splitter1: TSplitter;
Splitter2: TSplitter;
E1: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N6Click(Sender: TObject);
procedure N8Click(Sender: TObject);
Private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i:integer=0;//Declare a global variable for word memorization
Implementation
{$R *.dfm}
//Start the conversion event
procedure TForm1.Button1Click(Sender: TObject);
Begin
RTrans.Clear;//Clear the conversion area
RConv.Clear;
timer1.Interval:=strtoint(edit1.Text)*1000;//Set the interval time
timer1.Enabled :=true;//
progressbar1.Position:=0;//Set the progress bar status
i:=0; //Initialize variables for counting
progressbar1.Max:=MList.Lines.Count;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
King,//Kingsoft PowerWord 2002 Main window handle
combo,//Word input box parent window handle
edit,//word input box handle
means:thandle;//Translation display window handle
Begin
if i<=MList.Lines.Count-1 then
Begin
king:= findwindow (nil,pchar('Kingsoft PowerWord 2002'));
combo:= findwindowex (king,0,'ComboBox',nil);
edit:= findwindowex (combo,0,'Edit',nil);
means:= findwindowex (king,0,'XDICT_ExplainView',nil);
//Information display
label3.Caption :='('+inttostr(i+1)+'/'+inttostr(MList.Lines.Count)+') '+MList.Lines[i];
//Storage information
clipboard.AsText := MList.Lines[i];
showwindow (king,sw_shownormal);
bringwindowtotop (king);
SetForegroundWindow (king);
windows.SetFocus (edit);
//Simulate Ctrl+V Paste
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
keybd_event (Ord('V'), MapVirtualKey(Ord('V'), 0), 0, 0);
keybd_event (Ord('V'), MapVirtualKey(Ord('V'), 0), KEYEVENTF_KEYUP, 0);
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);
SetForegroundWindow (means);
windows.SetFocus (means);
//Simulation Ctrl+A Select all
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
keybd_event (Ord('A'), MapVirtualKey(Ord('A'), 0), 0, 0);
keybd_event (Ord('A'), MapVirtualKey(Ord('A'), 0), KEYEVENTF_KEYUP, 0);
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);
SetForegroundWindow (means);
windows.SetFocus (means);
//Simulate Ctrl+C Copy
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
keybd_event (Ord('C'), MapVirtualKey(Ord('C'), 0), 0, 0);
keybd_event (Ord('C'), MapVirtualKey(Ord('C'), 0), KEYEVENTF_KEYUP, 0);
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);
windows.SetFocus (RTrans.Handle);
//Simulate Ctrl+V Paste
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0);
keybd_event (Ord('V'), MapVirtualKey(Ord('V'), 0), 0, 0);
keybd_event (Ord('V'), MapVirtualKey(Ord('V'), 0), KEYEVENTF_KEYUP, 0);
keybd_event (VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0);
RTrans.Lines.Add('=========================='+#13);
progressbar1.StepIt;//Progress bar moves
inc(i);//Increase the count
end else
Begin
timer1.Enabled :=false;
RTrans.Lines.SaveToFile('Temp.txt');
end;
end;
//Read the word list into Mlist
procedure TForm1.Button2Click(Sender: TObject);
Begin
if od.Execute then
MList.Lines.LoadFromFile(od.FileName);
end;
//Conversion function, because the phonetic symbols cannot be displayed correctly in the translation obtained in RTrans
//You must first install the Phonetic Plain font in the Font directory in Kingsoft PowerWord
//Implement the correct display of phonetic symbols
procedure TForm1.Button3Click(Sender: TObject);
var
i:integer;
start,ends:integer;
Begin
RConv.Clear;
for i:=0 to RTrans.Lines.Count-1 do
Begin
richedit3.Text :=RTrans.Lines[i];
start:=pos('[',richedit3.Text);//Find the position of the phonetic symbol, the phonetic symbol is in []
if start>0 then
Begin
ends:=pos(']',richedit3.Text);
richedit3.SelStart :=start;
richedit3.SelLength :=ends-start-1;
richedit3.SelAttributes.Name :='Kingsoft Phonetic Plain';
end;
richedit3.SelectAll;
richedit3.CopyToClipboard;
RConv.PasteFromClipboard;
application.ProcessMessages;
end;
end;
//Program Exit
procedure TForm1.N3Click(Sender: TObject);
Begin
application.Terminate;
end;
//Copy function
procedure TForm1.N6Click(Sender: TObject);
Begin
RConv.SelectAll ;
RConv.CopyToClipboard;
end;
//Stop the conversion
procedure TForm1.N8Click(Sender: TObject);
Begin
timer1.Enabled :=false;
end;
end.
OK, so far, all the programs are designed.
How to run:
Ensure that "Kingsoft PowerWord 2002" runs with this program. If you use other versions of PowerWord, please change the search handle code in it yourself to support other versions.
OK, I have successfully translated these 600 words and printed them on paper in Word. It is very fast. If you have any good suggestions, please feel free to reply!
How to obtain compiled executable programs and source code:
You can send me an email: [email protected]
Or log in to my site and leave a message: http://redlegend.51.net