⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 取词内部细节.txt

📁 这是我改造并完整屏幕取词程序的过程中完成的文档
💻 TXT
📖 第 1 页 / 共 2 页
字号:
2005-06-03
在屏幕取词中,使用很多的图形图像、消息机制(回调函数是它的具体应用)、字符集判断、结构体,变量作用域等语法知识。

取词的流程
1、建立主程序
创建hello world 控制台应用程序
控制程序只能被执行一次
	更改.rc文件,title = "moMain"
	if(FindWindow(NULL,"moMain") != NULL)
	{
		return FALSE;
	}
在InitInstance中隐藏窗口
// 	ShowWindow(hWnd, nCmdShow);
   ShowWindow(hWnd, SW_HIDE);

2、使用时间钩子
导入时间钩子头文件
	#include "MouseHook.h"
申明计时器变量和对象
#define MY_GETWORD_TIMER	5     //取词定时器
#define MY_GW_WAITING_TIME	1000   //取词等待时间;

//my variables
UINT	g_nMyGWTimerID = 0;	//我的取词计时器标识
POINT CurMousePoint = {0,0};	//current mouse pos-----时间钩子的目的
char	g_MyTextBuffer[NHD_MAX_TEXTLEN];	//词的空间


//声明回调取词计时器
void CALLBACK GetMousePosTimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime);

在InitInstance中创建窗口后启动时间钩子
	InstallHook(TRUE);
然后启动计时器
	g_nMyGWTimerID = SetTimer(hWnd, MY_GETWORD_TIMER, MY_GW_WAITING_TIME, (TIMERPROC)GetMousePosTimerProc);
进行取词初始化
	///////////////////////////////init getwords
	if (!NHD_InitGetWords(hInst, hWnd))
	{
		MessageBox(hWnd,"NHD_InitGetWords error!","ffffd" ,MB_OK);	   
		return FALSE;
	}

在window过程WndProc的case WM_DESTROY:中的取词结束之后终止时间钩子
		case WM_DESTROY:
			NHD_ExitGetWords();//终止取词
			EndHook();//终止取点
计时器回调
/*------------------------------------20050617以前--------------------------------------*/
void CALLBACK GetMousePosTimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime)
{
	//-----------------------------------------------------------------------------
	// timer process callback function used to get text from buffer
	//计时器过程回调函数,用来从缓冲器中取出文本
	//-----------------------------------------------------------------------------

	//find module path 
	TCHAR FilePath[MAX_LOADSTRING];
	
	GetModuleFileName(hInst,FilePath,MAX_LOADSTRING); 
	(_tcsrchr(FilePath,'\\'))[1] = 0; 
	lstrcat(FilePath,("mo.kill")); 

    /* Find first .c file in current directory */
	struct _finddata_t c_file;
    long hFile;
    //if( (hFile = _findfirst( "mo.kill", &c_file )) != -1L )
    if( (hFile = _findfirst( FilePath, &c_file )) != -1L )
	{
		_unlink(FilePath);
		SendMessage(hwnd, WM_DESTROY, 0, 0); 
	}

	//1 read point from mouse hook dll

	CurMousePoint = GetCurMousePoint();——————————————————时间钩子的作用

	//2 get words
	if (!(CurMousePoint.x ==0 && CurMousePoint.y ==0) )
	{
		/////begin get words
--------------------------------时间钩子的目的
		NHD_BeginGetWord(CurMousePoint);
		if(NHD_CopyWordsTo(g_MyTextBuffer, sizeof(g_MyTextBuffer)))
		{
			if(strlen(g_MyTextBuffer)>1)
			{
				//strcpy(g_MyNewTextBuffer,"\0");
				GetInfoFromBuffer();//modified by lgs in 20050603 
				if(strlen(g_MyNewTextBuffer) > 0)
				{
					//write text into file
					GetModuleFileName(hInst,FilePath,MAX_LOADSTRING); 
					(_tcsrchr(FilePath,'\\'))[1] = 0; 
					lstrcat(FilePath,("data\\temp\\moText.dat")); 
					
					FILE *fp;
					//	fp=fopen(FilePath,"w"); 
				//	fclose(fp);
					fp=fopen(FilePath,"w"); 
					//fp=fopen("moText.dat","a"); 
					fprintf(fp,"%s\n",g_MyNewTextBuffer);
					//fprintf(fp,"%s\n",g_MyTextBuffer);//词
					//NHD_CopyWordsTo1(g_MyTextBuffer, sizeof(g_MyTextBuffer));//行
					//fprintf(fp,"%s\n",g_MyTextBuffer);
					fclose(fp);
				}
			}
			strcpy(g_MyTextBuffer,"\0");
			strcpy(g_MyNewTextBuffer,"\0");
		}
	}
	
	//ASSERT (g_nGWTimerID == 0);

	//3 resettimer
	KillTimer(hwnd, MY_GETWORD_TIMER);
	g_GetWord_Ok = false;
	g_nMyGWTimerID = SetTimer(hwnd, MY_GETWORD_TIMER, MY_GW_WAITING_TIME, (TIMERPROC)GetMousePosTimerProc);

	return ;
}
////////////////////////////////////取出()中的词//////////////////////////////////////
//code by lgs 2005-06-03
//get first words between '(' and ')' or between  '<' and '>'
void GetInfoFromBuffer()
{
	if (!g_GetWord_Ok || strlen(g_MyTextBuffer) < 1 )return;

	int iIndex = 0;
	int iLen = 0;
	iLen = strlen(g_MyTextBuffer);
	int iNewLen = 0;
	
	int iLeft = 0;
	int iRight = 0;
	int iFlag = 0;
	
	char * pTextBuffer;
	pTextBuffer = g_MyTextBuffer;

	strcpy(g_MyNewTextBuffer,"\0");
	
	while (iLen--) 
	{
		if ((*pTextBuffer == '(' || *pTextBuffer == '<' ) && iFlag == 0)
		{
			iLeft = iIndex;
			iFlag = 1;
		}
		
		if ((*pTextBuffer == ')' ||  *pTextBuffer == '>') && iFlag == 1)
		{
			iRight = iIndex;
			iFlag = 2;
			break;
		}


		if (iFlag == 1 && *pTextBuffer != ' ' && iIndex != iLeft )//过滤掉空格
		{
			iNewLen = strlen(g_MyNewTextBuffer);
			g_MyNewTextBuffer[iNewLen] = *pTextBuffer;
			g_MyNewTextBuffer[iNewLen+1] = 0;
		}

		iIndex++;
		pTextBuffer ++;
	}

	
	if ((iFlag != 2)&&(iRight-iLeft < 1)) 
	{
		strcpy(g_MyNewTextBuffer,"\0");
		return;
	}
	pTextBuffer = NULL;
}
//code by lgs end
//////////////////////////////////////////////////////////////////////////
/*------------------------------------20050617以后--------------------------------------*/
void CALLBACK GetMousePosTimerProc(HWND hwnd, UINT msg, UINT idTimer, DWORD dwTime)
{
	//-----------------------------------------------------------------------------
	// timer process callback function used to get text from buffer
	//-----------------------------------------------------------------------------
	if (g_GetWord_Ok == false) 
	{
		
	//find module path 
	TCHAR FilePath[MAX_LOADSTRING];
	
	GetModuleFileName(hInst,FilePath,MAX_LOADSTRING); 
	(_tcsrchr(FilePath,'\\'))[1] = 0; 
	lstrcat(FilePath,("mo.kill")); 
	
    /* Find first .c file in current directory */
	struct _finddata_t c_file;
    long hFile;
    if( (hFile = _findfirst( FilePath, &c_file )) != -1L )
	{
		_unlink(FilePath);
		SendMessage(hwnd, WM_DESTROY, 0, 0); 
	}
	
	//1 read point from mouse hook dll
	
	CurMousePoint = GetCurMousePoint();
	
	//2 get words
	if (!(CurMousePoint.x ==0 && CurMousePoint.y ==0) )
	{
		/////begin get words
		NHD_BeginGetWord(CurMousePoint);
		//
	}
	
	//ASSERT (g_nGWTimerID == 0);
	}
	//3 resettimer
	KillTimer(hwnd, MY_GETWORD_TIMER);
	g_GetWord_Ok = false;
	g_nMyGWTimerID = SetTimer(hwnd, MY_GETWORD_TIMER, MY_GW_WAITING_TIME, (TIMERPROC)GetMousePosTimerProc);
	
	return ;
}

//////////////////////////////////////////////////////////////////////////
//get first words between '(' and ')' or between  '<' and '>'
void GetInfoFromText()
{
	if (!g_GetWord_Ok )
	{
		strcpy(g_MyWordBuffer,"\0");
		strcpy(g_MyTextBuffer,"\0");
		return;
	}
	if(NHD_CopyWordsTo(g_MyWordBuffer, sizeof(g_MyWordBuffer)))
	{
		if(strlen(g_MyWordBuffer)>1) 
		{
			//strcpy(g_MyNewTextBuffer,"\0");
			strcpy(g_MyWordBuffer, GetWithoutBlankWord(g_MyWordBuffer));
			//			strcpy(TextBuffer,"\0");
			NHD_CopyWordsTo1(g_MyTextBuffer, sizeof(g_MyTextBuffer));//行
			if(strlen(g_MyTextBuffer)<1) goto RetBlank;
			/**/
			g_bracketHaving = false;
			if (!g_bracketHaving)CheckBracket("(",")");
			if (!g_bracketHaving)CheckBracket("<",">");
			if (!g_bracketHaving)CheckBracket("[","]");
			if (!g_bracketHaving)CheckBracket("(",")");
			if (!g_bracketHaving)CheckBracket("《","》");
			if (!g_bracketHaving) goto RetBlank;
			
			try{
				//write text into file
				TCHAR FilePath[MAX_LOADSTRING];
				GetModuleFileName(hInst,FilePath,MAX_LOADSTRING); 
				(_tcsrchr(FilePath,'\\'))[1] = 0; 
				lstrcat(FilePath,("data\\temp\\moText.dat")); 
				
				FILE *fp;
				//	fp=fopen(FilePath,"w");
				//	fclose(fp);
				char oneword[NHD_MAX_TEXTLEN];
				fp = fopen(FilePath,"r");
				if (fp != NULL)
				{
					if( fgets(oneword,1024, fp) == NULL)
					{
						strcpy(oneword,"\0");
					}
					fclose(fp);
				}
				else
				{
					strcpy(oneword,"\0");
				}
				
				if ((strcmp (oneword,g_MyWordBuffer) != 0) ) 
				{
					fp=fopen(FilePath,"w"); 
					fprintf(fp,"%s",g_MyWordBuffer);//词
					fclose(fp);
				}
			}
			catch (...) {
			}
RetBlank: 
			strcpy(g_MyWordBuffer,"\0");
			strcpy(g_MyTextBuffer,"\0");
		}
	}
}


bool CheckBracket(char* chLeftBracket,char* chRightBracket)
{
	if (strlen(g_MyWordBuffer) < 1) return false;
	
	char strTempText[NHD_MAX_TEXTLEN];//临时行数据
	char strTempWord[NHD_MAX_TEXTLEN];//临时字数据
	
	char * pch1;//临时变量
	char * pch2;//临时变量
	strcpy (strTempText,g_MyTextBuffer);
	pch1 = 	strTempText;
	strcpy(strTempWord, "\0");
	
	//	strcpy(g_MyTextBuffer, GetWithoutBlankWord(g_MyTextBuffer));
	
	while (pch1 != NULL )
	{
		pch1 = strstr(strTempText,chLeftBracket);
		pch2 = strstr(strTempText,chRightBracket);
		if(pch1 == NULL || pch2 == NULL) return false;
		if (pch2-pch1 <= 1) {
			if(pch2 == NULL) return false;
			pch2 = strstr(pch1,"(");
		}
		else
		{
			strncpy(strTempWord,pch1,pch2-pch1+strlen(chLeftBracket));
			strTempWord[pch2-pch1+strlen(chLeftBracket)]=0;

			strcpy(strTempWord, GetWithoutBlankWord(strTempWord));
			//			if (strcmp(g_MyWordBuffer,strTempWord) == 0)
			if (strstr(strTempWord,g_MyWordBuffer) != NULL)
			{
				strcpy(g_MyWordBuffer,strTempWord);
				g_bracketHaving = true;
				return true;
			}
			pch2 = pch2+strlen(chRightBracket);
			strcpy(strTempText, pch2);
			
		}
	}
	//	strcpy(g_MyWordBuffer,"\0");//还有下一次检查。
	return false;
}

//////////////////////////////////////////////////////////////////////////
//code by lgs 2005-06-15
//get first words without blank 
char * GetWithoutBlankWord(LPSTR strWithBlank)
{
	if (strlen(strWithBlank) < 1 )return "\0";//!g_GetWord_Ok || 
	
	int iNewLen = 0;
	
	int iLen = 0;
	iLen = strlen(strWithBlank);
	
	strcpy(TextBuffer,"\0");
	
	while (iLen--) 
	{
		if (*strWithBlank != ' ')//过滤掉空格
		{
			iNewLen = strlen(TextBuffer);
			TextBuffer[iNewLen] = *strWithBlank;
			TextBuffer[iNewLen+1] = 0;
		}
		strWithBlank ++;
	}
	//strcpy(strWithBlank,TextBuffer);
	return TextBuffer;
}

//////////////////////////////////////////////////////////////////////////
/*-----------------------------完毕-------------------------------------*/

3、钩子动态库头文件,导入使用钩子的函数
///#include "MouseHook.h"

#define DllExport extern "C"__declspec(dllexport)

// This class is exported from the MouseHook.dll
DllExport LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam, LPARAM lParam );
DllExport void InstallHook(int nCode);
DllExport void EndHook(void);
DllExport POINT GetCurMousePoint();

4、时间钩子c文件
// MouseHook.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "MouseHook.h"

HINSTANCE hInst;

#pragma data_seg("hookdata")
HHOOK oldkeyhook=0;
#pragma data_seg()
#pragma comment(linker,"/SECTION:hookdata,RWS")

POINT MousePos;


BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call,  LPVOID lpReserved )
{
    switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		hInst = (HINSTANCE)hModule;
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
    }
    return TRUE;
}


DllExport void InstallHook(int nCode)
{
	////设置底层钩子,只用底层钩子才能得到所有的窗口鼠标消息
	oldkeyhook = SetWindowsHookEx(WH_MOUSE_LL,(HOOKPROC)MouseProc,hInst,0);
}

DllExport LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam, LPARAM lParam )
{
	MousePos.x = 0;
	MousePos.y = 0;
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -