📄 strbuff.cpp
字号:
//输出串的缓冲区
#include "stdafx.h"
#include <windowsx.h>
#include "cspublic.h"
class CStrBuff //显示字符串缓冲类
{
public:
HDC hdc ; //当前HDC
POINT pos ; //当前显示位置
POINT point ; //当前鼠标所在点
LPSTR lpsStr ; //显示英文串缓冲区
int nLen ; //英文串长度
int m_nPosOfHz ; //如果是汉字串,标记光标在哪个汉字上
private:
BOOL m_bHzMatchFlag ; //0,标记汉字是最小匹配,1为最大
public:
CStrBuff( void ) ;
//判断字符串的哪部分在当前鼠标点上
BOOL JudgePosOfStr( LPSTR lps , int nLen ,
LPINT nFirst , LPINT nLast ) ;
//显示翻译结果
void Show( LPSTR s ) ;
//设置汉字匹配方式
void SetHzMatchFlag( BOOL bFlag ) ;
//得到汉字匹配方式
BOOL GetHzMatchFlag( void ) ;
} ;
CStrBuff::CStrBuff( void )
{
m_bHzMatchFlag =0 ;
}
//设置汉字匹配方式
void CStrBuff::SetHzMatchFlag( BOOL bFlag )
{
m_bHzMatchFlag =bFlag ;
}
//得到汉字匹配方式
BOOL CStrBuff::GetHzMatchFlag( void )
{
return m_bHzMatchFlag ;
}
//判断字符串的哪部分在当前鼠标点上
//#define _MYDEBUG
#ifdef _MYDEBUG
int nnn=1 ;
#endif
BOOL CStrBuff::JudgePosOfStr( LPSTR lps , int nLen ,
LPINT nFirst , LPINT nLast )
{
RECT rect ;
rect.top =pos.y ; //是TOP
POINT zeroPoint ={ 0 , 0 } ; //得到原点位置
LPtoDP( hdc , &zeroPoint , 1 ) ; //转换成设置坐标
POINT tempPoint ; //临时点,用以转换
tempPoint.y =HIWORD( GetTextExtent( hdc , lps , nLen ) ) ; //高度
LPtoDP( hdc , &tempPoint , 1 ) ;
tempPoint.y -=zeroPoint.y ; //得到字符串高度
// if( GetMapMode( hdc ) == MM_TEXT )
// {
// rect.top =pos.y ;
rect.bottom =pos.y + abs( tempPoint.y ) - 1 ;
/* }
else
{
rect.top =pos.y - abs( tempPoint.y ) + 1 ;
rect.bottom =pos.y ;
}
*/
int n1 , n2 ;
int nFlag ; //标记是汉字串还英文串,2为汉字串,1为英文串
for( n2=0 ; n2<nLen ; )
{
//消去非字母部分
for( n1=n2 ; n1<nLen ; n1++ )
{
if( n1<nLen-1 )
{
if( lps[n1] & 128 ) //是汉字
{
nFlag =2 ;
break ;
}
}
// if( isalpha( lps[n1] ) ) //是字母
if( ( lps[n1]>='a' && lps[n1]<='z' ) ||
( lps[n1]>='A' && lps[n1]<='Z' ) )
{
nFlag =1 ;
break ;
}
}
if( n1 == nLen ) //已经到头了
return 0 ;
//得到字母串或汉字串
for( n2=n1 ; n2<nLen ; n2++ )
{
if( nFlag == 1 ) //得到英文串
{
// if( !isalpha( lps[n2] ) ) //不是字母
if( !(( lps[n2]>='a' && lps[n2]<='z' ) ||
( lps[n2]>='A' && lps[n2]<='Z' ) ) )
break ;
}
else //得到汉字串
{
if( n2==nLen-1 )
break ;
//还可能是汉字
if( !(lps[n2] & 128) ) //不是汉字
break ;
n2++ ;
}
}
//计算字符串所包围的矩形
tempPoint.x =LOWORD( GetTextExtent( hdc , lps , n1 ) ) ;
LPtoDP( hdc , &tempPoint , 1 ) ;
tempPoint.x -=zeroPoint.x ;
rect.left =pos.x + tempPoint.x ;
tempPoint.x =LOWORD( GetTextExtent( hdc , lps , n2 ) ) ;
LPtoDP( hdc , &tempPoint , 1 ) ;
tempPoint.x -=zeroPoint.x ;
rect.right =pos.x + tempPoint.x ;
#ifdef _MYDEBUG
char sBuff[2000] ;
wsprintf( sBuff ,
"[1%c 2%s] len=%d n1=%dn2=%d nFlag=%d x=%d y=%d l=%d r=%d t=%d b=%d" ,
lps[n1] , lps+n1+1 , nLen , n1 , n2 , nFlag ,
point.x , point.y , rect.left , rect.right , rect.top , rect.bottom ) ;
char s[10] ;
wsprintf( s , "%d:" , nnn++ ) ;
WritePrivateProfileString( "judge" , s , sBuff , "test.ini" ) ;
#endif
//判断该字母串是否在当前鼠标点上
if( point.x >= rect.left && point.x <=rect.right &&
point.y>= rect.top && point.y <= rect.bottom ) //点落在其中
{
*nFirst =n1 ;
*nLast =n2 ;
//确定光标在第几个汉字上
if( lps[n1] & 0x80 ) //是汉字
for( m_nPosOfHz=0 ; //设置初始汉字位置为0
LOWORD(GetTextExtent( hdc , lps+n1 , (m_nPosOfHz+1)*2 ))
<(DWORD)(point.x-rect.left+1) ;
m_nPosOfHz++ )
;
return 1 ;
}
}
return 0 ;
}
//显示翻译结果
void CStrBuff::Show( LPSTR lpsDestination )
{
int nDestinationLen =_fstrlen( lpsDestination ) ;
//得到光标的高度
int nCursorHeight =GetSystemMetrics( SM_CYCURSOR ) ;
if( GetWinOrBar() ) //详细解释指取结果
ShowFinger( point.x ,
point.y+nCursorHeight/3 ,
lpsDestination , nDestinationLen ) ;
else //简单解释指取结果
ShowFinger( point.x ,
point.y+nCursorHeight/2 ,
lpsDestination , nDestinationLen ) ;
}
//------------------------------------------------------------------//
CStrBuff OStrBuff ;
#ifdef __cplusplus
extern "C" {
#endif
//设置输出串的起始点
void __export FAR PASCAL SetOutPos( POINT p , HDC dc )
{
OStrBuff.pos =p ;
OStrBuff.hdc =dc ;
}
//设置当前鼠标点
void __export FAR PASCAL SetCurrentPoint( POINT p )
{
OStrBuff.point =p ;
}
//设置输出串缓冲区
//0表示没有进行指取翻译
//1表示可以进行指取翻译,但不能进入显示字符串缓冲区
//2能够进入
//3表示不能进入
void __export FAR PASCAL SetStrBuff( LPSTR lps , int n )
{
HideFinger() ;
if( n<=0 ) //现在是空串
return ;
//判断串的哪部分落在当前鼠标点上
int nFirst , nLast ; //标识该部分
if( !OStrBuff.JudgePosOfStr( lps , n ,
(LPINT)&nFirst , (LPINT)&nLast ) )
return ; //没有落在鼠标点
//分配空间
OStrBuff.lpsStr =(LPSTR)GlobalAllocPtr( GMEM_FIXED ,
nLast-nFirst+1 ) ;
if( !OStrBuff.lpsStr )
{
::MessageBox( 0 , "GlobalAllocPtr error,in SetStrBuff" , 0 , MB_OK ) ;
return ;
}
//拷贝内容
_fstrncpy( OStrBuff.lpsStr , lps+nFirst , nLast-nFirst ) ;
OStrBuff.nLen =nLast-nFirst ;
OStrBuff.lpsStr[OStrBuff.nLen] ='\0' ; //结束标记
SetFingerTranslate( 3 ) ; //这次已经不能进入了
if( OStrBuff.lpsStr[0] & 0x80 ) //是汉字
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
return ;
}
nFirst =0 ;
if( _fstrlen( OStrBuff.lpsStr+nFirst )==0 ) //长度为0
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
return ;
}
//进行翻译
char lpStr[1024] ;
int nLenOfStr ;
if( !EnglishToChinese( OStrBuff.lpsStr , OStrBuff.nLen ,
lpStr , 1024 , &nLenOfStr ) )
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
return ;
}
/*
HANDLE hExplain ;
if( *OStrBuff.lpsStr & 0x80 ) //是汉字
{
//进行汉字分词
if( OStrBuff.GetHzMatchFlag() ) //最大匹配
GetChnMaxMatchWord( OStrBuff.lpsStr , OStrBuff.nLen ,
OStrBuff.m_nPosOfHz , (LPINT)&nFirst , (LPINT)&nLast ) ;
else //最小匹配
GetChnMinMatchWord( OStrBuff.lpsStr , OStrBuff.nLen ,
OStrBuff.m_nPosOfHz , (LPINT)&nFirst , (LPINT)&nLast ) ;
OStrBuff.lpsStr[nLast+1] ='\0' ; //设上结束标记
}
else //是英文串
nFirst =0 ;
if( _fstrlen( OStrBuff.lpsStr+nFirst )==0 ) //长度为0
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
return ;
}
//得到翻译结果
if( !GetWinOrBar() ) //详细解释指取结果
hExplain =FindWord( OStrBuff.lpsStr+nFirst , 1 ) ;
else //简单显示翻译结果
hExplain =FindWord( OStrBuff.lpsStr+nFirst , 0 ) ;
if( !hExplain )
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
return ;
}
LPSTR lpStr = (LPSTR)GlobalLock(hExplain);
if( !lpStr ) //查找有问题
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
GlobalUnlock(hExplain);
GlobalFree(hExplain);
return ;
}
if( *((LPINT)lpStr)==0 ) //返回个数为0
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
GlobalUnlock(hExplain);
GlobalFree(hExplain);
return ;
}
int nEndPos = sizeof(int);
size_t nLenOfStr =_fstrlen( lpStr+nEndPos ) ; //字符串长度
*/
int nEndPos =0 ;
//计算查询到的单词的长度
// for( size_t nLenOfResult=0 ; nLenOfResult<nLenOfStr ;nLenOfResult++ )
for( int nLenOfResult=0 ; nLenOfResult<nLenOfStr ;nLenOfResult++ )
if( *(lpStr+nEndPos+nLenOfResult)==':' )
break ;
if( nLenOfResult >= nLenOfStr ) //有问题
{
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
// GlobalUnlock(hExplain);
// GlobalFree(hExplain);
return ;
}
BOOL bSame ; //0,不同,1相同
if( nLenOfResult == _fstrlen( OStrBuff.lpsStr+nFirst ) ) //与原单词同长
{
//比较查询到的单词与原来的单词是否一样
LPSTR lpsBuff1 =(LPSTR)GlobalAllocPtr( GMEM_FIXED ,
_fstrlen( OStrBuff.lpsStr+nFirst )+1 ) ;
VERIFY( lpsBuff1 ) ;
_fstrcpy( lpsBuff1 , OStrBuff.lpsStr+nFirst ) ;
LPSTR lpsBuff2 =(LPSTR)GlobalAllocPtr( GMEM_FIXED ,
nLenOfStr+1 ) ;
VERIFY( lpsBuff2 ) ;
_fstrcpy( lpsBuff2 , lpStr+nEndPos ) ;
if( _fstrncmp( AnsiLower( lpsBuff1 ) ,
AnsiLower( lpsBuff2 ) ,
_fstrlen( OStrBuff.lpsStr+nFirst ) ) )
bSame =0 ; //查询到的单词与原来的单词不一样
else
bSame =1 ; //一样
GlobalFreePtr( lpsBuff1 ) ;
GlobalFreePtr( lpsBuff2 ) ;
}
else //不同长
bSame =0 ;
if( !bSame ) //不一样
{
//分配空间
LPSTR lpsBuff =(LPSTR)GlobalAllocPtr( GMEM_FIXED ,
_fstrlen( OStrBuff.lpsStr+nFirst ) + 4 +
nLenOfStr + 1 ) ;
VERIFY( lpsBuff ) ;
_fstrcpy( lpsBuff , OStrBuff.lpsStr+nFirst ) ;
char sss[5] ={ ' ' , 0xa1 , 0xfa , ' ' , '\0' } ; //->
_fstrcat( lpsBuff , sss ) ;
// _fstrcat( lpsBuff , " -> " ) ;
_fstrcat( lpsBuff , lpStr+nEndPos ) ;
// if( GetOutputCode()==1 ) //BIG5
// ChangeGbToBig5( lpsBuff , _fstrlen( OStrBuff.lpsStr+nFirst ) +
// 4 + nLenOfStr + 1 ) ;
OStrBuff.Show( lpsBuff ) ; //显示翻译结果
//释放空间
GlobalFreePtr( lpsBuff ) ;
}
else //一样
{
_fstrncpy( lpStr+nEndPos , OStrBuff.lpsStr+nFirst ,
_fstrlen( OStrBuff.lpsStr+nFirst ) ) ; //恢复成原来的单词
// if( GetOutputCode()==1 ) //BIG5
// ChangeGbToBig5( lpStr+nEndPos ,
// _fstrlen( lpStr+nEndPos ) ) ;
OStrBuff.Show( lpStr+nEndPos ) ; //显示翻译结果
}
//释放原来的东西
GlobalFreePtr( OStrBuff.lpsStr ) ;
// GlobalUnlock(hExplain);
// GlobalFree(hExplain);
}
//设置汉字匹配方式
void __export FAR PASCAL SetHzMatchFlag( BOOL bFlag )
{
OStrBuff.SetHzMatchFlag( bFlag ) ;
}
#ifdef __cplusplus
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -