//-----------------------------------------------------------------------------
// begin get words
//-----------------------------------------------------------------------------
//此函数执行的是真正的取词操作。所以,是最重要的一个函数。
//此函数只是在CNhdemoApp::PreTranslateMessage(MSG* pMsg)函数中被调用了一次。
void NHD_BeginGetWord(POINT& ptMousePos)
{
char szAppClassName[NHD_CLASSNAME_LEN + 1];//NHD_CLASSNAME_LEN==64
HWND hAppWin;
int nFlyWinLeft;
int nFlyWinWidth;
RECT rcAppWin;
//get window from mouse point;
//The WindowFromPoint function retrieves a handle to the window that contains the specified point.
//The WindowFromPoint function does not retrieve a handle to a hidden or disabled window, even if the point is within the window. An application should use the ChildWindowFromPoint function for a nonrestrictive search.
hAppWin = WindowFromPoint(ptMousePos);//调用此函数,获取鼠标点下的窗口的句柄
//add begin
if(
g_hPushpin2Dlg == hAppWin||
g_hPushpinButton== hAppWin||
g_hIconButton_A == hAppWin||
g_hIconButton_B == hAppWin||
g_hIconButton_C == hAppWin||
g_hIconButton_D == hAppWin
)//不对Pushpin2Dlg窗口及其内部的子窗口取词
return;
//add end
//check if the app window is EDIT, if it is, redraw whole line;
//The GetClassName function retrieves the name of the class to which the specified window belongs.
//第二个参数是输出参数。输出的是the class name string。
GetClassName(hAppWin, szAppClassName, NHD_CLASSNAME_LEN);//取出鼠标点下的窗口的class name
// special window class
if (stricmp(szAppClassName, "Edit") == 0 || //NotePad
stricmp(szAppClassName, "Internet Explorer_Server") == 0 || //IE4.0
stricmp(szAppClassName, "RichEdit") == 0 || //
stricmp(szAppClassName, "RichEdit20A") == 0 || //WordPad
stricmp(szAppClassName, "RichEdit20W") == 0 || //WordPad
stricmp(szAppClassName, "HTML_Internet Explorer") == 0 || //IE3.0
stricmp(szAppClassName, "ThunderTextBox") == 0 || //VB Edit
stricmp(szAppClassName, "ThunderRT5TextBox") == 0 || //VB Edit
stricmp(szAppClassName, "ThunderRT6TextBox") == 0 || //VB Edit
stricmp(szAppClassName, "EXCEL<") == 0 || //Excel 2000
stricmp(szAppClassName, "EXCEL7") == 0 || //Excel 2000
stricmp(szAppClassName, "EXCEL6") == 0 || //Excel 2000
stricmp(szAppClassName, "ConsoleWindowClass") == 0 || //NT V86
stricmp(szAppClassName, "_WwG") == 0 ||
stricmp(szAppClassName, "tty") == 0 ||
stricmp(szAppClassName, "ttyGrab") == 0) //Word97
{
//The GetWindowRect function retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
GetWindowRect(hAppWin, &rcAppWin);//获取鼠标点下方的窗口的RECT
nFlyWinLeft = rcAppWin.left - 4;
nFlyWinWidth = rcAppWin.right - rcAppWin.left - 8;//鼠标点下方的窗口的RECT减8,也是够宽的了。
//do not repaint whole line if too long;
if ((ptMousePos.x - nFlyWinLeft) > 200)
{
//这里的代码是用来保证nFlyWinLeft与ptMousePos.x之间的距离不会超过200
nFlyWinLeft = ptMousePos.x - 200;
}
//note: move the fly-window to cursor pos "y - 1" to aviod mouse shape changing between ARROW and EDIT in edit area;
//use SetWindowPos instead of MoveWindow, for MoveWindow can not make menu item redraw.
SetWindowPos(g_hFlyWin, HWND_TOPMOST,
nFlyWinLeft,
ptMousePos.y - 1 ,
nFlyWinWidth,
NHD_FLYWIN_HEIGHT,
SWP_NOACTIVATE | SWP_NOREDRAW);
//set flag to avoid re-entry;
g_bInGetWord = TRUE;
//-----------------------------------------------------------------------------
// copy words in buffer to destination
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// initial function called once before getting words
//-----------------------------------------------------------------------------
//调用此函数时,第二个参数传递的是对话框窗口的window handle
//此函数只是在CNhdemoDlg::OnInitDialog()函数体中被调用了一次。
HWND NHD_InitGetWords(HINSTANCE hInst, HWND hwnd)
{
//save NH main window to send run time error messages:
g_hNHMainWin = hwnd;//保存 对话框窗口的window handle
//如果 load nhw32.dll 不成功,则
if (!NHD_LoadGetWordLib())
{ //调用此函数,free nhw32.dll
NHD_FreeLoadedLib();
return NULL;
}
//Create fly_window (cause paint) and show text window;
//fly_window大概就是那个跟随鼠标移动的窗口,引起鼠标下的窗口的部分区域重绘,导致TextOutA函数被调用。
//text window是什么?
g_hFlyWin = NHD_CreateWindow(hInst);//create auxiliary window used to help get text on screen
//如果auxiliary window创建失败,则
if (!g_hFlyWin)
{
NHD_FreeLoadedLib();//调用此函数,free nhw32.dll
return NULL;
}
//The RegisterWindowMessage function defines a new window message that is guaranteed to be unique throughout the system. The message value can be used when sending or posting messages.
//If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.If the function fails, the return value is zero.
g_WM_GetWordOk = RegisterWindowMessage(GWMSG_GETWORDOK);//注册一个自定义消息GWMSG_GETWORDOK,需要注意的是:此消息在nhw32.dll中也被定义了一遍,但是由于两个消息的内容完全相同,所以,两次对RegisterWindowMessage函数的调用,返回的是同一个消息的注册编号。
//如果注册自定义消息失败,则
if (!g_WM_GetWordOk)
{
NHD_FreeLoadedLib();//调用此函数,free nhw32.dll
return NULL;
}
//-----------------------------------------------------------------------------
// deinit function called when not get words any more
//-----------------------------------------------------------------------------
//很简单的函数,没什么可以解释的。
BOOL NHD_ExitGetWords(void)
{
//free libarys:
NHD_FreeLoadedLib();
NHD_DestroyWindow();
return TRUE;
}
//-----------------------------------------------------------------------------
// timer process callback function used to get text from buffer
//-----------------------------------------------------------------------------
//定时器的timer procedure,延时到了之后,会调用此过程。定时器的timer identifier==2;time-out value==200毫秒;
void CALLBACK NHD_GetWordTimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime)
{
ASSERT (g_nGWTimerID != 0);//此变量是用来存放SetTimer函数的返回值用的。此变量不为0时,表示定时器有效
//may be prior finished by Getword message;???
//如果是处于取词状态下,则
if (g_bInGetWord)
{
g_bInGetWord = FALSE;//表示取词结束了
//UnHook TextOut; 将第一个参数设为GETWORD_DISABLE,表示: 停止取词。因为词已经被取出来了,不用再取了。
fpBL_SetFlag32(GETWORD_DISABLE, NULL, 0, 0);//此函数在ReadMe.rtf文件中有介绍。
//此函数在ReadMe.rtf文件中有介绍。
fpBL_GetText32(g_TextBuffer, NHD_MAX_TEXTLEN, &g_WordRect);//从内部缓冲区取出单词文本串,放到全局字符串缓冲区g_TextBuffer中。
}
//get word on cursor pos; 获取鼠标位置处的word
if (wParam == 0)//???为什么要加上这么一个条件判断呢?为什么???
{
//get word from 16BIT API HOOK for NORMAL App;
//此函数在ReadMe.rtf文件中有介绍。
fpBL_GetText32(g_TextBuffer, NHD_MAX_TEXTLEN, &g_WordRect);//从内部缓冲区取出单词文本串,放到全局字符串缓冲区g_TextBuffer中。
}
//-----------------------------------------------------------------------------
// create auxiliary window used to help get text on screen
//-----------------------------------------------------------------------------
//hInst参数传递的是对话框程序的模块句柄
HWND NHD_CreateWindow(HINSTANCE hInst)
{
HWND hwnd;
WNDCLASS wc;
if (hInst == NULL)
{
return NULL;
}
//create a very small window, cause AP paint by moving it;
wc.lpszMenuName = NULL;
wc.lpszClassName = "NHD_FLYWIN_DEMO";
wc.hInstance = hInst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.style = WS_EX_TOPMOST;
wc.lpfnWndProc = NHD_FlyWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
RegisterClass(&wc);//注册窗口类
//WS_EX_TOOLWINDOW Creates a tool window, which is a window intended to be used as a floating toolbar. A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font. A tool window does not appear in the task bar or in the window that appears when the user presses ALT+TAB.
hwnd = CreateWindowEx (WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
"NHD_FLYWIN_DEMO",
"NHD_FlyWindow_Demo",
WS_POPUP | WS_VISIBLE,
NHD_WIN_INITPOSX, //宏定义,等于-1
NHD_WIN_INITPOSY, //宏定义,等于-1
NHD_FLYWIN_WIDTH, //宏定义,等于1
NHD_FLYWIN_HEIGHT, //宏定义,等于1
NULL,
NULL,
hInst,
NULL);//宽度和高度都是1的窗口,不就是只显示一个像素吗???
//***************************************************************************//
//File Name : getwords.cpp
//Content : get words demo using nhw32.dll
//Version : 0.01
//Date : 2000/7/15
//Aurthor : ITC RD-VIOLET1
//***************************************************************************//
#include "stdafx.h"
#include "getwords.h"
//-----------------------------------------------------------------------------
// Global variables:
//-----------------------------------------------------------------------------
HINSTANCE g_hGetWordInst = NULL;
BOOL g_bInGetWord = FALSE;
char g_TextBuffer[NHD_MAX_TEXTLEN];
RECT g_WordRect;
UINT g_WM_GetWordOk;
UINT g_nGWTimerID = 0;//此变量是用来存放SetTimer函数的返回值用的。此变量不为0时,表示定时器有效
HWND g_hNHMainWin = NULL;
HWND g_hFlyWin = NULL;
HWND g_hPushpin2Dlg;//added by yjk
HWND g_hPushpinButton;//added by yjk
HWND g_hIconButton_A;//added by yjk
HWND g_hIconButton_B;//added by yjk
HWND g_hIconButton_C;//added by yjk
HWND g_hIconButton_D;//added by yjk
/*----------------------------------------------------------------------------
nhw32.dll exports two function mainly:
******************************************************************
DWORD WINAPI BL_SetFlag32(UINT nFlag,
HWND hNotifyWnd,
int MouseX,
int MouseY
)
functions:
Start or Stop getting word.
parameters:
nFlag
[in] specify the following values
GETWORD_ENABLE: start getting word. set this flag before repaint region where
the word is around. In order to get words, need to repaint
region where the word is around.
GETWORD_DISABLE: stop getting word.
hNotifyWnd
[in] handle to be notified window. when the word has been got, send a registered
GWMSG_GETWORDOK message to window which handle is hNotifyWnd.
MouseX
[in] Specify the x-coordinate of point where the word is around.
MouseY
[in] Specify the y-coordinate of point where the word is around.
return values:
can be ignored.
==============================================================================
DWORD WINAPI BL_GetText32(LPSTR lpszCurWord,
int nBufferSize,
LPRECT lpWordRect
)
functions:
get words text string from internal buffer.
Parameters:
lpszCurWord
[in] pointer to destination buffer to which copy words got.
nBufferSize
[in] size of destination buffer.
lpWordRect
[out] pointer to RECT struct where defines the coordinates of the upper-left and
lower-right corners of a corresponding word's rectangle.
Return values:
Current Caret place in total word.
**************************************************************************
The NT version nhw32.dll specially exports other two function beside
above two function:
**************************************************************************
BOOL WINAPI SetNHW32()
functions:
initial function only valid in Win NT/2000 environment.
return values:
TRUE if successful, FALSE if failed.
===========================================================================
BOOL WINAPI ResetNHW32()
functions:
only valid in Win NT/2000 environment, must be called before exit programm.
return values:
TRUE if successful, FALSE if failed.
=============================================================================
note: The WinNT/2000 version nhw32.dll is different from Win95/98 version nhw32.dll, but
BL_SetFlag32 and BL_GetText32 - two function's interface is same.
//-----------------------------------------------------------------------------
// function used to output debug message
//-----------------------------------------------------------------------------
va_start(marker, fmt);
wvsprintf(szBuf, fmt, marker);
va_end(marker);
//The OutputDebugString function sends a string to the debugger for display. If the application has no debugger, the system debugger displays the string. If the application has no debugger and the system debugger is not active, OutputDebugString does nothing.
OutputDebugString(szBuf);
OutputDebugString(TEXT("\r\n"));
}