TOOLS.CPP
上传用户:zhang8947
上传日期:2007-01-08
资源大小:1910k
文件大小:8k
- #include "stdafx.h"
- #include <windowsx.h>
- #include "cspublic.h"
- #include "tools.h"
- //把BIG5转换成GB的对象
- CChangeCode p_Big5ToGb( BIG5TOGB_LIB_NAME ) ;
- //把GB转换成BIG5的对象
- CChangeCode p_GbToBig5( GBTOBIG5_LIB_NAME ) ;
- //--------------------------------------------------------------------------------------------//
- //将内码从BIG5转换成GB码或GB到BIG5
- CChangeCode::CChangeCode( char * psFileName ):
- CFile( psFileName , CFile::modeRead )
- {
- DWORD dwFileLen =GetLength() ; //得到文件长度
- if( dwFileLen % 4 ) //不能被4整除
- {
- ::MessageBox( 0 , "The length of the file is wrong!" , 0 , MB_OK ) ;
- return ;
- }
- m_nItemNum =(int)(dwFileLen / 4) ; //表中项数
-
- //分配内存
- m_lpsList =(char huge*)GlobalAllocPtr( GMEM_FIXED ,
- dwFileLen ) ;
- if( !m_lpsList )
- {
- ::MessageBox( 0 , "GlobalAllocPtr error" , 0 , MB_OK ) ;
- return ;
- }
-
- //读出内码转换表
- ReadHuge( m_lpsList , dwFileLen ) ;
-
- Close() ;
- }
- CChangeCode::~CChangeCode( void )
- {
- if( m_lpsList )
- GlobalFreePtr( m_lpsList ) ;
- }
- //改变一个汉字的内码
- void CChangeCode::ChangeOneHzCode( LPSTR lpsHz )
- {
- LONG nFirst =0 ; //表头
- LONG nLast =m_nItemNum-1 ; //表尾
- LONG nMiddle ;
- LONG nPoint ; //在表中的字节偏移量
- int n ;
-
- while( nFirst <= nLast )
- {
- nMiddle =( nFirst + nLast ) / 2; //二分
-
- nPoint =nMiddle*4 ;
- n =_fstrncmp( m_lpsList+nPoint , lpsHz , 2 ) ; //比较
- if( !n ) //两者内码相同
- {
- //进行转换
- lpsHz[0] =m_lpsList[nPoint+2] ;
- lpsHz[1] =m_lpsList[nPoint+3] ;
- return ;
- }
-
- if( n<0 )
- nFirst =nMiddle+1 ;
- else
- nLast =nMiddle-1 ;
- }
-
- //表中没有该汉字内码,返回确省GB内码,即空格
- lpsHz[0] =0xa1 ;
- lpsHz[1] =0xa1 ;
- }
- #ifdef __cplusplus
- extern "C" {
- #endif
- //发送汉字
- #define WM_IME_CHAR 0x0286
- #define WM_IME_REPORT 0x0280
- BOOL __export FAR PASCAL SendHz( LPCSTR szResult )
- {
- int nLen =_fstrlen( szResult ) ;
-
- HWND hWnd =::GetFocus();
- if( !hWnd )
- hWnd =GetFocus32() ;
- if( !hWnd ) //没有输入窗口
- return FALSE ;
- int i ;
- //低字0为Win,1为95;高字0为西文,1为外挂,2为中文Win
- DWORD dwVersion =GetWindowsVersion() ;
- if( LOWORD(dwVersion) && HIWORD(dwVersion)==2 ) //为中文96
- {
- char szTmp[3] ;
- szTmp[2] =' ' ;
- for( i=0; i<nLen; i+=2)
- {
- if( !GetInputStatus123(2) ) //需要在汉字前加空格
- ::SendMessage( hWnd , WM_CHAR , ' ' , 0 ) ;
-
- szTmp[0] = szResult[i+1];
- szTmp[1] = szResult[i];
- ::SendMessage(hWnd, WM_IME_REPORT,
- *((WPARAM*)szTmp), 0 );
- ::SendMessage(hWnd, WM_IME_CHAR,
- *((WPARAM*)szTmp), 1);
- }
- }
- else
- {
- for( i=0 ; i<nLen ; i++ )
- {
- if( !(i%2) && !GetInputStatus123(2) ) //需要在汉字前加空格
- ::SendMessage( hWnd , WM_CHAR , ' ' , 0 ) ;
- ::SendMessage( hWnd , WM_CHAR , (BYTE)szResult[i] , 0 ) ;
- }
- ::InvalidateRect( hWnd , NULL , FALSE ) ;
- ::UpdateWindow( hWnd ) ;
- }
- return TRUE ;
- }
- //提取字符串中的汉字串或英文串
- int __export FAR PASCAL JudgeEnOrHz( int nStartPos , int cbString , LPCSTR lpszString ,
- int FAR *lpNowPos )
- {
- if( nStartPos < 0 || nStartPos >= cbString )
- return 0 ;
-
- if( nStartPos == cbString-1 )
- {
- *lpNowPos =cbString ;
- return 1 ;
- }
-
- int nStatus ;
- if( lpszString[nStartPos] & 0x80 )
- nStatus =2 ;
- else
- nStatus =1 ;
- for( int i=nStartPos ; i<cbString ; )
- {
- if( i == cbString-1 )
- {
- if( nStatus == 2 )
- {
- *lpNowPos =i ;
- return 2 ;
- }
- else
- {
- *lpNowPos =cbString ;
- return 1 ;
- }
- }
-
- if( lpszString[i]&0x80 )
- {
- if( nStatus == 1 )
- {
- *lpNowPos =i ;
- return( 1 ) ;
- }
- i +=2 ;
- }
- else
- {
- if( nStatus == 2 )
- {
- *lpNowPos =i ;
- return( 2 ) ;
- }
- i++ ;
- }
- }
- *lpNowPos =cbString ;
- return( nStatus ) ;
- }
- //把BIG5码转换成GB码
- void __export FAR PASCAL ChangeBig5ToGb( LPSTR lpsStr , int nLen )
- {
- for( int i=0 ; i<nLen ; i++ )
- {
- if( i < nLen-1 )
- {
- if( lpsStr[i]&0x80 ) //是一个汉字
- {
- //改变该汉字的内码
- p_Big5ToGb.ChangeOneHzCode( lpsStr+i ) ;
- i++ ;
- }
- }
- }
- }
- //把GB码转换成BIG5码
- void __export FAR PASCAL ChangeGbToBig5( LPSTR lpsStr , int nLen )
- {
- for( int i=0 ; i<nLen ; i++ )
- {
- if( i < nLen-1 )
- {
- if( lpsStr[i]&0x80 ) //是一个汉字
- {
- //改变该汉字的内码
- p_GbToBig5.ChangeOneHzCode( lpsStr+i ) ;
- i++ ;
- }
- }
- }
- }
- //把GB码转换成HZ码
- void __export FAR PASCAL ChangeGbToHz( LPSTR lpsStr , int nLen )
- {
- for( int i=0 ; i<nLen ; i++ )
- lpsStr[i] &=0x7f ;
- }
- //画一个双线的立体边框.
- //RECT描述的是要画的四边形的左上角和右下角的坐标位置,包括左上角和右下角
- //bFlag为1,没有按下,0按下
- //color为面上的颜色
- void __export FAR PASCAL DrawTwoLineBox( HDC hdc ,
- const RECT &rect , BOOL bFlag , COLORREF color )
- {
- CDC dc ;
- dc.m_hDC =hdc ;
- //浅灰笔
- CPen LowGrayPen( PS_SOLID , 1 , RGB( 192 , 192 , 192 ) ) ;
- //深灰笔
- CPen HighGrayPen( PS_SOLID , 1 , RGB( 128 , 128 , 128 ) ) ;
- CBrush brush( color ) ;
-
- //画上底色
- CPen *pOldPen =(CPen*)dc.SelectStockObject( BLACK_PEN ) ;//黑笔
- CBrush *pOldBrush =dc.SelectObject( &brush ) ;
- dc.Rectangle( rect.left , rect.top , rect.right+1 , rect.bottom+1 ) ;
- dc.SelectObject( pOldBrush ) ; //恢复刷子
- dc.SelectObject( &LowGrayPen ) ; //浅灰笔
- if( bFlag ) //是没有按下的按钮
- {
- dc.MoveTo( rect.right-1 , rect.top ) ;
- //画左上角外面的浅灰线
- dc.LineTo( rect.left , rect.top ) ;
- dc.LineTo( rect.left , rect.bottom ) ;
- }
- else //是按下的按钮
- {
- dc.MoveTo( rect.right , rect.top ) ;
- //画右下角外面的浅灰线
- dc.LineTo( rect.right , rect.bottom ) ;
- dc.LineTo( rect.left-1 , rect.bottom ) ;
- }
-
- //画左上角内线,没有按下是白色,按下是深灰色的
- if( bFlag )
- dc.SelectStockObject( WHITE_PEN ) ; //白笔
- else
- dc.SelectObject( &HighGrayPen ) ; //深灰笔
- dc.MoveTo( rect.right-2 , rect.top+1 ) ;
- dc.LineTo( rect.left+1 , rect.top+1 ) ;
- dc.LineTo( rect.left+1 , rect.bottom-1 ) ;
- //画右下角内线,没有按下是深灰色,按下是白色
- if( bFlag )
- dc.SelectObject( &HighGrayPen ) ;
- else
- dc.SelectStockObject( WHITE_PEN ) ;
- dc.MoveTo( rect.left+1 , rect.bottom-1 ) ;
- dc.LineTo( rect.right-1 , rect.bottom-1 ) ;
- dc.LineTo( rect.right-1 , rect.top ) ;
- //恢复笔
- dc.SelectObject( pOldPen ) ;
- }
- //画一个单线的立体边框
- //RECT描述的是要画的四边形的左上角和右下角的坐标位置,包括左上角和右下角
- //bFlag为1,没有按下,0按下
- //没有按下时,color1为左上角线的颜色,color2为右下角线的颜色
- //按下相反。color为面上的颜色
- void __export FAR PASCAL DrawOneLineBox( HDC hdc ,
- const RECT &rect , BOOL bFlag ,
- COLORREF color1 , COLORREF color2 , COLORREF color )
- {
- CDC dc ;
- dc.m_hDC =hdc ;
- CPen pen1( PS_SOLID , 1 , color1 ) ;
- CPen pen2( PS_SOLID , 1 , color2 ) ;
- CBrush brush( color ) ;
-
- //画上底色
- CPen *pOldPen =dc.SelectObject( &pen2 ) ;
- CBrush *pOldBrush =dc.SelectObject( &brush ) ;
- dc.Rectangle( rect.left , rect.top , rect.right+1 , rect.bottom+1 ) ;
- dc.SelectObject( pOldBrush ) ;
- dc.SelectObject( &pen1 ) ;
- if( bFlag ) //没有按下
- {
- dc.MoveTo( rect.right-1 , rect.top ) ;
- //画左上角线
- dc.LineTo( rect.left , rect.top ) ;
- dc.LineTo( rect.left , rect.bottom ) ;
- }
- else
- {
- dc.MoveTo( rect.right , rect.top ) ;
- //画右下角线
- dc.LineTo( rect.right , rect.bottom ) ;
- dc.LineTo( rect.left-1 , rect.bottom ) ;
- }
- //恢复笔
- dc.SelectObject( pOldPen ) ;
- }
- //清屏
- void __export FAR PASCAL UpdateScreen( void )
- {
- ::InvalidateRect( NULL , NULL , FALSE ) ;
- ::UpdateWindow( GetDesktopWindow() ) ;
- }
- #ifdef __cplusplus
- }
- #endif