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

📄 findword.c

📁 屏幕取词源码.zip
💻 C
📖 第 1 页 / 共 3 页
字号:

__inline DWORD CheckMouseInCurWord(HDC   hDC, 
								   LPSTR szBuff, 
								   int   cbLen, 
								   int   x, 
								   int   y, 
								   int   FAR* lpDx,
								   int*  lpLeft,
								   int   nBegin,    // = nPrevWord + 1
								   int   nEnd,
								   int   nCharType)
{
	RECT  StringRect;

	GetStringRect(hDC, szBuff, nEnd + 1, x, y, &StringRect, lpDx);
	StringRect.left = *lpLeft;
	*lpLeft = StringRect.right;

	if (  ((g_CurMousePos.x >= StringRect.left    ) && (g_CurMousePos.x <= StringRect.right))
	    || (g_CurMousePos.x == StringRect.left - 1)
	    || (g_CurMousePos.x == StringRect.right + 1))
	{
		switch (nCharType)
		{
			case CHAR_TYPE_HZ:

			case CHAR_TYPE_ASCII:
				 CopyWord(g_szCurWord, szBuff, nBegin, nEnd);
				 g_CurWordRect.left   = StringRect.left;
				 g_CurWordRect.right  = StringRect.right;
				 g_CurWordRect.top    = StringRect.top;
				 g_CurWordRect.bottom = StringRect.bottom;
				 
				 g_nCurCaretPlace = -1;
				 CalculateCaretPlace(hDC, 
									 szBuff, 
									 cbLen,
									 x,
									 y,
									 lpDx,
									 nBegin,
									 nEnd,
									 nCharType);
				 
				 break;
			case CHAR_TYPE_OTHER:
				 break;
		}
		AddToTotalWord(szBuff, cbLen, nBegin, nEnd, nCharType, StringRect, TRUE);
		if (  (nCharType == CHAR_TYPE_OTHER)
		    &&(CalcCaretInThisPlace(g_CurMousePos.x, StringRect.right)))
		{
#ifdef _DICTING_
			return HAS_CURMOUSEWORD;
#else
			return NO_CURMOUSEWORD;
#endif
		}

		return HAS_CURMOUSEWORD;
	}
	else
	{
	}					

	AddToTotalWord(szBuff, cbLen, nBegin, nEnd, nCharType, StringRect, FALSE);

	return NO_CURMOUSEWORD;   
}

// 用於计算指定位置在字串中的位置·
__inline DWORD CalculateCaretPlace(HDC   hDC, 
								   LPSTR szBuff, 
								   int   cbLen, 
								   int   x, 
								   int   y, 
								   int   FAR* lpDx,
								   int   nBegin,    // = nPrevWord + 1
								   int   nEnd,
								   int   nCharType)
{
	RECT  StringRect, BeginRect;
	RECT  CaretPrevRect, CaretNextRect;
//	int   itemWidth; 
	double itemWidth; 
	int   TempPlace;
	int   i;

	if ((nCharType == CHAR_TYPE_HZ) && (nBegin == nEnd))
	{
		g_nCurCaretPlace = -1;
		return 0L;
	}

	GetStringRect(hDC, szBuff, nBegin, x, y, &BeginRect, lpDx);
	
	GetStringRect(hDC, szBuff, nEnd + 1,   x, y, &StringRect, lpDx);
	StringRect.left = BeginRect.right;
    if (StringRect.left == StringRect.right)
    {
		g_nCurCaretPlace = -1;
		return 0L;
    }
	
	switch (nCharType)
	{
		case CHAR_TYPE_HZ:
///////////////////////////////////////////////////////////////////////
// 汉字处理部分·
			 // 由於汉字都是等宽的字符,因此对於汉字的计算是通过计算
			 // 平均宽度来实现的·
//			 itemWidth = (StringRect.right - StringRect.left) / (nEnd - nBegin + 1);
/////////////////////////////////////////////////////////////////
// Modify by GY
// Ver0.036  074号 bug.
// 主要是由於在进行类型转换中有误·			 
			 itemWidth = ((double)StringRect.right - (double)StringRect.left + 1) / ((double)nEnd - (double)nBegin + 1);
// Modify end
////////////////////////////////////////////////////////////////////
//			 g_nCurCaretPlace = (g_CurMousePos.x - StringRect.left) * (nEnd - nBegin + 1) / (StringRect.right - StringRect.left);
			 for (i = 0; i <= (nEnd - nBegin + 1); i++)
			 {
			 	if (CalcCaretInThisPlace(g_CurMousePos.x, (double)((double)StringRect.left + (double)(itemWidth * i))))
			 	{
				 	g_nCurCaretPlace = i;
				 	i = nEnd - nBegin + 2;
			 	}
			 }
             break;
//
///////////////////////////////////////////////////////////////////////             
		case CHAR_TYPE_ASCII:
			 // 由於英文字符为不等宽字符· 按以下步骤来计算的:
			 // 1. 按等宽计算一个相对位置·
			 // 2. 计算该相对位置的的绝对位置·
			 // 3. 若在该位置,返回·
			 // 4. 若该绝对长小於相对位置乘以宽,转6
			 // 5. 向右计算·
			 // 6. 向左计算·
			 
			 // 1. 按等宽计算一个相对位置·
			 // 计算宽度·
			 itemWidth = (StringRect.right - StringRect.left + 1) / (nEnd - nBegin + 1);
			 // 计算位置·
			 TempPlace = (g_CurMousePos.x - StringRect.left) * (nEnd - nBegin + 1) / (StringRect.right - StringRect.left);
			 
			 // 2. 计算该相对位置的的绝对位置·
			 GetStringRect(hDC, szBuff, TempPlace,     x, y, &CaretPrevRect, lpDx);
			 GetStringRect(hDC, szBuff, TempPlace + 1, x, y, &CaretNextRect, lpDx);
			 
			 // 3. 若在该位置,返回·
			 if (CalcCaretInThisPlace(g_CurMousePos.x, CaretPrevRect.right)) 
			 {
			 	g_nCurCaretPlace = TempPlace - nBegin;
			 	break;
			 }
			 if (CalcCaretInThisPlace(g_CurMousePos.x, CaretNextRect.right))
			 {
			 	g_nCurCaretPlace = TempPlace - nBegin + 1;
			 	break;
			 }

			 // 4. 若该绝对长小於相对位置乘以宽,转6
			 if (g_CurMousePos.x > CaretNextRect.right)
			 {
				 // 5. 向右计算·      
				 GetEngLishCaretPlace(hDC, 
									  szBuff,
									  x,
									  y,
									  lpDx,
									  nBegin,    // = nPrevWord + 1
									  nEnd,
									  TempPlace,
									  RIGHT);
			 }
			 else
			 {
				 // 6. 向左计算·
				 GetEngLishCaretPlace(hDC, 
									  szBuff,
									  x,
									  y,
									  lpDx,
									  nBegin,    // = nPrevWord + 1
									  nEnd,
									  TempPlace,
									  LEFT);
			 }
			 
			 break;
	}
	return 0L;
}

__inline DWORD GetEngLishCaretPlace(HDC   hDC, 
									LPSTR szBuff,
									int   x,
									int   y,
									int   FAR* lpDx,
									int   nBegin,    // = nPrevWord + 1
									int   nEnd,
									int   TempPlace,
									int   turnto)
{
	int i;
	RECT  CaretPrevRect, CaretNextRect;
	
	if (turnto == RIGHT)
	{
		i = TempPlace;
		GetStringRect(hDC, szBuff, i, x, y, &CaretPrevRect, lpDx);
		
		for (i = TempPlace; i <= nEnd; i++)
		{
			 GetStringRect(hDC, szBuff, i + 1, x, y, &CaretNextRect, lpDx);
			 if (CalcCaretInThisPlace(g_CurMousePos.x, CaretPrevRect.right)) 
			 {
			 	g_nCurCaretPlace = i - nBegin;
			 	return 0;
			 }
			 if (CalcCaretInThisPlace(g_CurMousePos.x, CaretNextRect.right))
			 {
			 	g_nCurCaretPlace = i - nBegin + 1;
			 	return 0;
			 }

			 CopyRect(&CaretPrevRect, &CaretNextRect);
		}
		g_nCurCaretPlace = nEnd - nBegin + 1;
	}
	else
	{
		i = TempPlace;
		GetStringRect(hDC, szBuff, i + 1, x, y, &CaretNextRect, lpDx);

		for (i = TempPlace; i >= nBegin; i--)
		{
			 GetStringRect(hDC, szBuff, i, x, y, &CaretPrevRect, lpDx);
			 if (CalcCaretInThisPlace(g_CurMousePos.x, CaretPrevRect.right)) 
			 {
			 	g_nCurCaretPlace = i - nBegin;
			 	return 0;
			 }
			 if (CalcCaretInThisPlace(g_CurMousePos.x, CaretNextRect.right))
			 {
			 	g_nCurCaretPlace = i - nBegin + 1;
			 	return 0;
			 }

			 CopyRect(&CaretNextRect, &CaretPrevRect);
		}
		g_nCurCaretPlace = nBegin - nBegin;
	}
	
	return 0;
}

//BOOL CalcCaretInThisPlace(int CaretX, int nPlace)
BOOL CalcCaretInThisPlace(int CaretX, double nPlace)
{
	if (((double)CaretX >= nPlace - 3)&&((double)CaretX <= nPlace + 1))
	{
		return TRUE;
	}

	return FALSE;
}

// Get current Chinese char that under the mouse cursor.
// 1.                       2
// mouse place :    |            |
// chinese char:  英业达      英业达
// return place:    |           |
__inline int GetHZBeginPlace(LPSTR lpszHzBuff, int nBegin, int nEnd, LPRECT lpStringRect)
{
	int itemWidth; 
	int nReturn;

	itemWidth = (lpStringRect->right - lpStringRect->left + 1) / (nEnd - nBegin + 1);
//	nReturn = (g_CurMousePos.x - lpStringRect->left) / itemWidth;
	nReturn = (g_CurMousePos.x - lpStringRect->left) * (nEnd - nBegin + 1) / (lpStringRect->right - lpStringRect->left);

	if (nReturn == 0)
		return nBegin;		
    
	if (nReturn % 2 == 1)
	{
    	lpStringRect->left = lpStringRect->left + (nReturn - 1) * itemWidth;
    	return nBegin + nReturn - 1;
    }
    
   	lpStringRect->left = lpStringRect->left + nReturn * itemWidth;
	return (nBegin + nReturn);
}

//=========================================================
//
//  FUNCTION: void AddToTotalWord(LPSTR, int, int, int, int, RECT, BOOL);
//
//  PURPOSE:  用於将多次文本输出的单词合并为一个完整单词·
//
//  PARAMETER: 
//       
//       LPSTR : 由 TextOut 或 ExtTextOut 函数传递的字符串·
//       int   : 字符串长·
//       int   : 由切词程序处理後的单词在串中的起始位置·( 0 代表串的第一字节 )
//       int   : 由切词程序处理後的单词在串中的结束位置·( 0 代表串的第一字节 )
//       int   : 代表由 nBegin, nEnd 指定的单词的性质 ( 中文/英文/其他字符)
//       RECT  : 代表由 nBegin, nEnd 指定的单词的区域·
//       BOOL  : 用於判断当前鼠标位置是否在该单词中·
//
//  COMMENTS:
//
//
//=========================================================
_inline void AddToTotalWord(LPSTR szBuff, 
							int   cbLen, 
							int   nBegin,
							int   nEnd,
							int   nCharType,
							RECT  StringRect,   
							BOOL  bInCurWord)
{
	int nPos = 0 ; //temp variable

	if ((nCharType == CHAR_TYPE_OTHER) && (!g_bMouseInTotalWord))
	{
		// 处理串开始为空格的情况·
		g_szTotalWord[0] = 0x00;
		g_CharType = nCharType;
		g_bMouseInTotalWord = FALSE;
		return;
	}
	
	//Added by XGL, see GetCurWordEnd for reasons
	if (((BYTE)(szBuff[nBegin]) >= 0xa1 && (BYTE)(szBuff[nBegin]) <= 0xa9)
		&& (!g_bMouseInTotalWord))
	{
		// 处理串开始为空格的情况·
		g_szTotalWord[0] = 0x00;
		g_CharType = CHAR_TYPE_HZ;
		g_bMouseInTotalWord = FALSE;
		return;
	}

	if ((g_szTotalWord[0] == 0x00)&&(nCharType != CHAR_TYPE_OTHER))
	{
		// 如果完整词缓冲区为空,并且单词为中文或英文,
		// 拷贝字符串·
		CopyWord(g_szTotalWord, szBuff, nBegin, nEnd);
		g_TotalWordRect.left   = StringRect.left;
		g_TotalWordRect.right  = StringRect.right;
		g_TotalWordRect.top    = StringRect.top;
		g_TotalWordRect.bottom = StringRect.bottom;
		g_CharType = nCharType;
		if (bInCurWord)
		{
			g_bMouseInTotalWord = TRUE;
			//Added by XGL, Sep 17th, 1998
			//When the word pointed to by mouse was got,
			//attain it's char-type.
			g_nPhraseCharType = nCharType ;
			g_nWordsInPhrase++ ;
			//Adding ends.
			//Added by XGL, Sep 29th, 1998
			g_rcFirstWordRect.left   = StringRect.left;
			g_rcFirstWordRect.right  = StringRect.right;
			g_rcFirstWordRect.top    = StringRect.top;
			g_rcFirstWordRect.bottom = StringRect.bottom;
			//Adding ends.
		}
		else
		{
			g_bMouseInTotalWord = FALSE;
		}
		g_nCurCaretPlaceInTotalWord = -1;
		if (g_nCurCaretPlace != -1)
		{
			g_nCurCaretPlaceInTotalWord = g_nCurCaretPlace;
		}
		return;
	}

	if (!g_bMouseInTotalWord)
	{
		// Added by XGL
		//(nCharType == CHAR_TYPE_OTHER) && (!g_bMouseInTotalWord)
		//See the line above. How can conditions
		//in the following line happen.
		//Adding ends.
/*		if ((nCharType == CHAR_TYPE_OTHER)&&(g_CharType != nCharType))
		{
			// Clear buffer.
			g_szTotalWord[0] = 0x00;
			g_CharType = CHAR_TYPE_OTHER;
			g_bMouseInTotalWord = FALSE;
			g_nCurCaretPlaceInTotalWord = -1;
			if (g_nCurCaretPlace != -1)
			{
				g_nCurCaretPlaceInTotalWord = g_nCurCaretPlace;
			}
			return;
		}
*/
		if (g_CharType != nCharType)
		{
			CopyWord(g_szTotalWord, szBuff, nBegin, nEnd);
			g_TotalWordRect.left   = StringRect.left;
			g_TotalWordRect.right  = StringRect.right;
			g_TotalWordRect.top    = StringRect.top;
			g_TotalWordRect.bottom = StringRect.bottom;
			g_CharType = nCharType;
			if (bInCurWord)
			{
				g_bMouseInTotalWord = TRUE;
				//Added by XGL, Sep 17th, 1998
				//When the word pointed to by mouse was got,
				//attain it's char-type.
				g_nPhraseCharType = nCharType ;
				g_nWordsInPhrase++ ;
				//Adding ends.
				//Added by XGL, Sep 29th, 1998
				g_rcFirstWordRect.left   = StringRect.left;
				g_rcFirstWordRect.right  = StringRect.right;
				g_rcFirstWordRect.top    = StringRect.top;
				g_rcFirstWordRect.bottom = StringRect.bottom;
				//Adding ends.
			}
			else
			{
				g_bMouseInTotalWord = FALSE;
			}
			g_nCurCaretPlaceInTotalWord = -1;
			if (g_nCurCaretPlace != -1)
			{
				g_nCurCaretPlaceInTotalWord = g_nCurCaretPlace;
			}
			return;
		}
	}

	//Added by XGL
	//g_bMouseInTotalWord == FALSE && g_CharType != nCharType
	//has been dealed with.
	//Adding ends.
	if ((g_CharType != nCharType)) 
	{
/*		if (bInCurWord)
		{
			//Added by XGL
			//How can this condition happen
			//Adding ends.
			if (!g_bMouseInTotalWord)
			{
				CopyWord(g_szTotalWord, szBuff, nBegin, nEnd);
				g_TotalWordRect.left   = StringRect.left;
				g_TotalWordRect.right  = StringRect.right;
				g_TotalWordRect.top    = StringRect.top;
				g_TotalWordRect.bottom = StringRect.bottom;
				g_CharType = nCharType;
				g_bMouseInTotalWord = TRUE;
				g_nCurCaretPlaceInTotalWord = g_nCurCaretPlace;
			}
		}
*/
		//Added by XGL
		//now, g_bMouseInTotalWord == TRUE
		//let space char go through since we want to get phrase
		if ( ((nCharType == CHAR_TYPE_OTHER) && (szBuff[nBegin] == ' '))
			 || ((nCharType != CHAR_TYPE_OTHER)
				 && (g_nPhraseCharType == nCharType)) )
		{
			//Modified by XGL, don't allow space char in Chinese
//			if (   g_bPhraseNeeded 
//				   &&(g_nWordsInPhrase > MIN_WORDS_IN_PHRASE)
//				   &&(g_nWordsInPhrase < MAX_WORDS_IN_PHRASE) )
			if (   g_bPhraseNeeded 
				   &&(g_nWordsInPhrase > MIN_WORDS_IN_PHRASE)
				   &&(g_nWordsInPhrase < MAX_WORDS_IN_PHRASE)
				   &&(!(g_szTotalWord[0] < 0 && szBuff[nBegin] == ' ')) )
			{
			}
			else
			{
				g_nWordsInPhrase = MAX_WORDS_IN_PHRASE + 1 ;
				return;
			}
		}
		else
		{
			g_nWordsInPhrase = MAX_WORDS_IN_PHRASE + 1 ;
			return;
		}
		//Adding ends.
	}
	
/*	//Added by XGL, Sep 17th, 1998
	if ( g_bMouseInTotalWord && g_bPhraseNeeded
		 && (strlen(g_szTotalWord) > MAX_WORDS_IN_PHRASE * 2)
		 && (g_nPhraseCharType == CHAR_TYPE_HZ) )
	{
		//We cann't count Chinese words using space char
		//so g_nWordsInPhrase is useless now
		g_nWordsInPhrase = MAX_WORDS_IN_PHRASE + 1 ;
	}
	//Adding ends.
*/
	if (  ((UINT)(StringRect.left - g_TotalWordRect.right) <= (UINT)SEPERATOR)
		&&(abs((int)(StringRect.bottom - g_TotalWordRect.bottom)) <= SEPERATOR))
	{
		//Added by XGL, see GetCurWordEnd for reasons. Nov 13th, 1998.
		if ((BYTE)(szBuff[nBegin]) >= 0xa1 
			&& (BYTE)(szBuff[nBegin]) <= 0xa9)
			
		{
			return;
		}
		//Adding ends. XGL, Nov 13th, 1998.
		//Added by XGL, Sep 17th, 1998
		if (g_bMouseInTotalWord && g_bPhraseNeeded
			&& (g_nWordsInPhrase > MIN_WORDS_IN_PHRASE)
			&& (g_nWordsInPhrase < MAX_WORDS_IN_PHRASE)
			&& (szBuff[nBegin] == ' '))
		{
			//other chars between nBegin and nEnd must be space
			nPos = nBegin ;
			while (szBuff[nPos] == ' ' && nPos <= nEnd)
			{
				nPos++ ;
			}      
			//Modified by XGL, Nov 11th, 1998  
			//We allow just one space between words of a phrase
			//if (nPos <= nEnd)      
			if (nPos <= nEnd || nPos - nBegin > 1)			
			{
				//stop get word
				g_nWordsInPhrase = MAX_WORDS_IN_PHRASE + 1 ;
			}
			else
			{
				g_nWordsInPhrase++ ;
			}
			
		}

		if (g_nWordsInPhrase >= MAX_WORDS_IN_PHRASE)
		{
			//enough words, no need to do more work
			return ;
		}
		//Adding ends.

		if ((g_nCurCaretPlace != -1)&&(g_nCurCaretPlaceInTotalWord == -1))
		{
			g_nCurCaretPlaceInTotalWord = strlen(g_szTotalWord) + g_nCurCaretPlace;
		}
		CopyWord(g_szTotalWord + strlen(g_szTotalWord), szBuff, nBegin, nEnd);

⌨️ 快捷键说明

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