最近本人正在准备cet-4的考试,从同学那得到了一份“四级高频词”的doc文档,该文档只有单词,并没有音标和解释,如果进行人工一个一个翻译很是费事,因此本人利用delphi和金山词霸2002特别制作了一个单词批量翻译,并且可以将翻译结果直接保存为rtf文件的程序。其程序界面如下: interface uses type
原理分析:
利用“金山词霸2002”的翻译功能,进行单词的翻译,利用delphi获取“金山词霸2002”中翻译的结果的控件的句柄,利用粘贴复制功能,即可以获得翻译的结果了。
api函数说明:
hwnd findwindow(
lpctstr lpclassname, // 欲搜索的窗体的类名
lpctstr lpwindowname // 欲搜索的窗体的标题名称
);//查找与指定条件相符的第一个子窗口
hwnd findwindowex(
hwnd hwndparent, // 在其中查找子的父窗口的句柄
hwnd hwndchildafter, // 从这个窗体后开始查找
lpctstr lpszclass, // 欲搜索的窗体的类名
lpctstr lpszwindow // 欲搜索的窗体的标题名称
);//在指定窗体列表中查找与指定条件相符的第一个子窗口
bool showwindow(
hwnd hwnd, // 窗体的句柄
int ncmdshow // 窗体的显示方式
);//指定窗口的可见性
bool bringwindowtotop(
hwnd hwnd // 窗体的句柄
);//将指定的窗口带至窗口列表的顶部
bool setforegroundwindow(
hwnd hwnd //窗体的句柄
);//将窗口设为系统的前台程序
hwnd setfocus(
hwnd hwnd // 聚焦的窗体的句柄
);//将窗口聚焦
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
);//模拟按键的产生
这里提供了基本api的声明,具体的使用方法,你可以其它相关资料。
具体分析:
首先利用spy++工具,对“金山词霸2002”进行分析,分析结果如下:
金山词霸2002的窗体的名称是:金山词霸 2002
金山词霸2002的单词输入控件类名:edit (属于combobox的子窗体)
金山词霸2002的翻译结果控件类名:xdict_explainview
程序界面:
一个timer控件(timer1,其间隔时间为3秒),一个memo控件(mlist),两个richedit控件(rtrans,rconv),具体的代码如下:
unit unit1;
windows, messages, sysutils, variants, classes, graphics, controls,forms, dialogs, stdctrls, comctrls, clipbrd,buttons, extctrls, menus;
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 }