www.pudn.com > 智能内码识别,支持屏幕取词翻译的程序.zip > FINGER.CPP


//屏幕指取翻译 
 
#include	"stdafx.h" 
 
#include	"cspublic.h"            
#include	"cskernel.h" 
#include	"finger.h" 
 
extern	HINSTANCE	hCsKernel ;		//标识该DLL 
 
static UINT			idTimer=0 ;		//时间器标识 
static HHOOK		hHook=NULL ;			//钩子句柄 
 
static FARPROC		lpHookProc=NULL ;	//鼠标函数 
static FARPROC		lpTimerProc=NULL ;	//时间器函数 
 
static CTimer	OTimer ;	//时间器对象 
static CFingerTranslate	OFingerTranslate ;	//指取翻译对象 
 
#ifdef	__cplusplus 
extern	"C"	{ 
#endif 
 
//挂上指取模块 
BOOL __export FAR PASCAL	HookFingerOn( void ) 
{ 
	//装入翻译字库 
	if( !LoadDict() ) 
		return	0 ; 
 
	return	HookFingerOn1() ; 
/* 
	lpHookProc	=::MakeProcInstance( (FARPROC)MouseFilter ,  
									hCsKernel) ; 
	VERIFY( lpHookProc ) ; 
	//挂上鼠标动作过滤函数 
	hHook	=::SetWindowsHookEx(  
						WH_MOUSE , 
						(HOOKPROC)lpHookProc ,  
						hCsKernel ,  
						(HTASK)NULL ) ; 
	VERIFY( hHook ) ;						 
 
	lpTimerProc	=::MakeProcInstance( (FARPROC)SetTime ,  
								hCsKernel ) ; 
	VERIFY( lpTimerProc ) ; 
	//挂上时间器 
	idTimer	=::SetTimer( NULL , 1 , 1 , (TIMERPROC)lpTimerProc ) ; 
	VERIFY( idTimer ) ; 
			 
	return TRUE;*/ 
} 
 
//卸掉指取模块 
void	__export FAR PASCAL	HookFingerOff( void ) 
{ 
	HookFingerOff1() ; 
/*    	HideFinger() ; 
 
	//摘掉时间器 
	if( idTimer ) 
	{ 
		SetTimerStatus( 0 ) ;	//不计时 
		::KillTimer( NULL , idTimer) ; 
		::FreeProcInstance( lpTimerProc ) ; 
		idTimer	=0 ; 
		lpTimerProc	=NULL ; 
	} 
	 
	if( hHook ) 
	{ 
		::UnhookWindowsHookEx( hHook ) ; 
		::FreeProcInstance( lpHookProc ) ; 
		hHook	=NULL ; 
		lpHookProc	=NULL ; 
	} 
*/	 
	//卸掉翻译字库 
	UnloadDict() ; 
} 
 
//挂上指取模块 
BOOL __export FAR PASCAL	HookFingerOn1( void ) 
{ 
	lpHookProc	=::MakeProcInstance( (FARPROC)MouseFilter ,  
									hCsKernel) ; 
	VERIFY( lpHookProc ) ; 
	//挂上鼠标动作过滤函数 
	hHook	=::SetWindowsHookEx(  
						WH_MOUSE , 
						(HOOKPROC)lpHookProc ,  
						hCsKernel ,  
						(HTASK)NULL ) ; 
	VERIFY( hHook ) ;						 
 
	lpTimerProc	=::MakeProcInstance( (FARPROC)SetTime ,  
								hCsKernel ) ; 
	VERIFY( lpTimerProc ) ; 
	//挂上时间器 
	idTimer	=::SetTimer( NULL , 1 , 1 , (TIMERPROC)lpTimerProc ) ; 
	VERIFY( idTimer ) ; 
			 
	return TRUE; 
} 
 
//卸掉指取模块 
void	__export FAR PASCAL	HookFingerOff1( void ) 
{ 
   	HideFinger() ; 
 
	//摘掉时间器 
	if( idTimer ) 
	{ 
		SetTimerStatus( 0 ) ;	//不计时 
		::KillTimer( NULL , idTimer) ; 
		::FreeProcInstance( lpTimerProc ) ; 
		idTimer	=0 ; 
		lpTimerProc	=NULL ; 
	} 
	 
	if( hHook ) 
	{ 
		::UnhookWindowsHookEx( hHook ) ; 
		::FreeProcInstance( lpHookProc ) ; 
		hHook	=NULL ; 
		lpHookProc	=NULL ; 
	} 
} 
 
//开始指取翻译 
void __export FAR PASCAL	StartFingerTranslation( void ) 
{ 
	OFingerTranslate.BeginTranslate() ; 	//进行指取翻译 
	SetTimerStatus( 2 ) ;	//消失开始计时 
} 
 
#ifdef	__cplusplus 
} 
#endif 
 
//----------------------------------------------------------------------------------------------------------// 
 
//初始化 
CTimer::CTimer( void ) 
{ 
	m_nTimerStatus	=0 ;		//初始状态为0,不需要进行计时 
} 
 
//设置计时状态 
void	CTimer::SetTimerStatus( int status ) 
{ 
	m_nTimerStatus	=status ; 
	if( status )	//需要进行计时 
		m_dwTime	=GetTickCount() ;	//得到当前时间 
} 
 
//设置时间 
void	CTimer::SetTime( DWORD time ) 
{ 
	switch( m_nTimerStatus ) 
	{ 
		case	1:		//设置显示时间 
			if( time-m_dwTime >= TO_SHOW_TIME )	//可以进行显示了 
				StartFingerTranslation() ; 
			break ; 
		case	2:		//设置消失时间	 
			if( time-m_dwTime >= TO_DISAPPEAR_TIME )	//要消失了 
			{ 
				HideFinger() ;	//隐藏翻译结果 
				SetTimerStatus( 0 ) ;	//不进行计时 
			} 
			break ; 
	} 
} 
 
//每隔1毫秒调一次 
extern "C" void __export FAR PASCAL SetTime( 
				HWND hwnd ,	/* handle of window for timer messages	*/ 
				UINT msg ,	/* WM_TIMER message	*/ 
				UINT idTimer ,	/* timer identifier	*/ 
				DWORD dwTime )	/* current system time	*/ 
{ 
	OTimer.SetTime( dwTime ) ; 
} 
 
//设置计时状态 
void	SetTimerStatus( int status ) 
{ 
	OTimer.SetTimerStatus( status ) ; 
} 
 
//----------------------------------------------------------------------------------------------------------//				 
 
extern "C" LRESULT __export FAR PASCAL MouseFilter(  
						int code, WPARAM wParam , LPARAM lParam) 
{ 
	if( code >=0 ) 
	{ 
		if( !GetFingerAidedKeyFlag() ||	//不需要指取翻译辅助键 
			GetFingerAidedKeyFlag()==2 )	//已经按下辅助键 
		{ 
			if( wParam == WM_MOUSEMOVE || 
			  	wParam == WM_NCMOUSEMOVE )	//鼠标移动 
			{ 
				POINT	tempPoint ; 
				GetCursorPos( (LPPOINT)&tempPoint ) ;		//当前鼠标所在点 
				POINT	currentPoint ; 
				//以前鼠标所在点 
				OFingerTranslate.GetCurrentPoint( (LPPOINT)¤tPoint ) ; 
				//移动了位置 
				if( currentPoint.x != tempPoint.x || 
					 currentPoint.y != tempPoint.y ) 
				{ 
			    	HideFinger() ;	//隐藏以前显示的结果 
					OTimer.SetTimerStatus( 1 ) ;		//进行显示计时 
				} 
			} 
			else if( 
				wParam == WM_LBUTTONDOWN ||            
				wParam == WM_RBUTTONDOWN || 
				wParam == WM_LBUTTONDBLCLK || 
				wParam == WM_RBUTTONDBLCLK || 
				wParam == WM_NCLBUTTONDOWN ||            
				wParam == WM_NCRBUTTONDOWN || 
				wParam == WM_NCLBUTTONDBLCLK || 
				wParam == WM_NCRBUTTONDBLCLK )		 
			 	HideFinger() ; 
		} 
	} 
 
	//调用下一个钩子函数 
	return CallNextHookEx( hHook , code , wParam , lParam ) ; 
} 
 
CFingerTranslate::CFingerTranslate( void ) 
{ 
	m_CurrentPoint.x	=-1 ; 
	m_CurrentPoint.y	=-1 ; 
} 
 
void	CFingerTranslate::GetCurrentPoint( LPPOINT lp ) 
{ 
	lp->x	=m_CurrentPoint.x ; 
	lp->y	=m_CurrentPoint.y ; 
} 
 
void	CFingerTranslate::BeginTranslate( void ) 
{ 
/*	POINT	tempPoint ; 
	GetCursorPos( &tempPoint ) ;		//临时点 
	if( m_CurrentPoint.x == tempPoint.x && 
		m_CurrentPoint.y == tempPoint.y )	//没有移动位置 
		return ; 
 
	m_CurrentPoint	=tempPoint ; 
*/	 
	GetCursorPos( &m_CurrentPoint ) ;	//得到当前点 
	//重新显示鼠标下的字符串 
	GetString() ; 
} 
 
//得到当前光标下的字符串,进行翻译 
void	CFingerTranslate::GetString( void ) 
{ 
	//得到当前点下的窗口 
	HWND	hParentWnd	=::WindowFromPoint( m_CurrentPoint ) ; 
	if( !hParentWnd ) 
		return ; 
 
	//关闭窗口 
	HWND	closeWnd	=::FindWindow( NULL , "Close Program" ) ; 
	if( closeWnd==hParentWnd || ::IsChild( closeWnd , hParentWnd ) ) 
		return ; 
	HWND	close1Wnd	=::FindWindow( NULL , "关闭程序" ) ; 
	if( close1Wnd==hParentWnd || ::IsChild( close1Wnd , hParentWnd ) ) 
		return ; 
	//WIN切换窗口的窗口句柄 
	HWND	alt_tabWnd	=::FindWindow( "#32771" , NULL ) ; 
	if( alt_tabWnd==hParentWnd )	//不对切换窗口处理 
	{ 
/*		SetFingerTranslate( 2 ) ;		//设成可以写 
		//设上当前点,为了判断字符串是否在该点上 
		SetCurrentPoint( m_CurrentPoint ) ; 
		 
		RECT	rect ; 
		//得到窗口大小 
		::GetWindowRect( hParentWnd , &rect ) ; 
		//重画该区域 
		ShowTempWin( rect.left , m_CurrentPoint.y ,  
					rect.right-rect.left , 1 ) ; 
						 
		HideTempWin() ; 
*/		return ; 
	} 
 
	POINT	pp	=m_CurrentPoint ; 
	ScreenToClient( hParentWnd , &pp ) ; 
	HWND	hwnd	=::ChildWindowFromPoint( hParentWnd , pp ) ; 
	if( !hwnd )	 
		hwnd	=hParentWnd ;	 
//	HWND	hwnd	=::WindowFromPoint( m_CurrentPoint ) ; 
//	if( !hwnd ) 
//		return ; 
 
	SetFingerTranslate( 2 ) ;		//设成可以写 
	//设上当前点,为了判断字符串是否在该点上 
	SetCurrentPoint( m_CurrentPoint ) ;	 
//---------------------------------------------------------------//	 
/* 
	//Notepad的窗口句柄 
	HWND	notepadWnd	=::FindWindow( "Notepad" , NULL ) ; 
	//DBWin的窗口句柄 
	HWND	dbwinWnd	=::FindWindow( "DBWin" , NULL ) ; 
	//DOS窗口的名柄 
	HWND	dosWnd	=::FindWindow( "tty" , NULL ) ; 
	//Telnet的窗口句柄 
	HWND	telnetWnd	=::FindWindow( "TelnetWClass" , NULL ) ; 
	//TeraTerm的窗口句柄 
	HWND	teratermWnd	=::FindWindow( "VTWin32" , NULL ) ; 
	//Eudora的窗口句柄 
	HWND	eudoraWnd	=::FindWindow( "UIW_MDIFRAME" , NULL ) ; 
	//中文之星WINDOS的窗口句柄 
	HWND	windosWnd	=::FindWindow( "WINDOS" , NULL ) ; 
 
	BOOL	bFlag=0 ; 
	if( notepadWnd ) 
		if(	::IsChild( notepadWnd , hwnd ) ) 
			bFlag	=1 ; 
	if( dbwinWnd ) 
		if( ::IsChild( dbwinWnd   , hwnd ) ) 
			bFlag	=1 ; 
	if( dosWnd ) 
		if( ::IsChild( dosWnd   , hwnd ) ) 
			bFlag	=1 ; 
	if( eudoraWnd ) 
		if( ::IsChild( eudoraWnd , hwnd ) ) 
			bFlag	=1 ; 
	if( dosWnd==hwnd || telnetWnd == hwnd || teratermWnd == hwnd || windosWnd==hwnd ) 
		bFlag	=1 ; 
*/ 
//	if( bFlag )		//需要特殊的窗口 
	{ 
		RECT	rect ; 
		POINT	tempPoint ; 
		 
		tempPoint	=m_CurrentPoint ; 
		::ScreenToClient( hwnd , &tempPoint ) ; 
		::GetClientRect( hwnd , &rect ) ; 
		if( !PtInRect( &rect , tempPoint ) )	//不在客户区内 
			goto l100 ; 
			 
		//得到窗口大小 
		::GetWindowRect( hwnd , &rect ) ; 
		tempPoint.x	=rect.left ; 
		tempPoint.y	=m_CurrentPoint.y ;//rect.top ; 
		::ScreenToClient( hwnd , &tempPoint ) ; 
		rect.left	=tempPoint.x ; 
		rect.top	=tempPoint.y ; 
 
		tempPoint.x	=rect.right ; 
		tempPoint.y	=rect.bottom ; 
		::ScreenToClient( hwnd , &tempPoint ) ; 
		rect.right	=tempPoint.x ; 
		rect.bottom	=rect.top+1 ;//tempPoint.y ; 
		//重画该区域 
//		char	sBuff[100]; 
//		wsprintf( sBuff , "%d %d %d %d" , rect.left , rect.top,rect.right, 
//			rect.bottom ) ; 
//		WritePrivateProfileString( "test" , sBuff , "ok" , "test.ini" ) ; 
//		if( GetParent( hwnd ) ) 
		{ 
			::InvalidateRect( hwnd , &rect , FALSE ) ; 
		//	::SendMessage( hwnd , WM_NCPAINT , 0 , 0 ) ; 
			::SendMessage( hwnd , WM_PAINT , 0 , 0 ) ; 
			return ; 
		} 
//		::UpdateWindow( hwnd ) ; 
//		::DrawMenuBar( hwnd ) ; 
//		ShowTempWin( rect.left , m_CurrentPoint.y ,  
//					rect.right-rect.left , 1 ) ; 
//		HideTempWin() ; 
//		return ;						 
	} 
 
	//重画该区域 
l100:	ShowTempWin( m_CurrentPoint.x , m_CurrentPoint.y ,  
		1 , 1) ; 
	HideTempWin() ; 
}