www.pudn.com > Poly.rar > ScrollAssist.cpp
// ScrollAssist.cpp: implementation of the CScrollAssist class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ScrollAssist.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CScrollAssist_zhang::CScrollAssist_zhang()
{
m_pwnd = NULL;
m_zoom = 1.0;
ScrollStep = 1;
HPosHi = 0; HPosLow = 0;
VPosHi = 0; VPosLow = 0;
ScrollRange.cx = 0;
ScrollRange.cy = 0;
m_curUndo = 0;
}
CScrollAssist_zhang::~CScrollAssist_zhang()
{
}
// cx : window width , cy : window height
// size : Image width and height
// zoom : real zoomrate
// CENTER ZOOM
void CScrollAssist_zhang::Change(int cx,int cy,CSize size,float zoom)
{
int x_cen,y_cen;
x_cen = GetScrollPos(SB_HORZ)+cx/2;
y_cen = GetScrollPos(SB_VERT)+cy/2;
size.cx = (int)(size.cx*zoom);
size.cy = (int)(size.cy*zoom);
size.cx -= cx;
size.cy -= cy;
size.cx /= 10;
size.cy /= 10;
if( size.cx<0 ) size.cx = 0;
if( size.cy<0 ) size.cy = 0;
ScrollRange = size;
m_pwnd->SetScrollRange(SB_HORZ,0,size.cx,TRUE);
m_pwnd->SetScrollRange(SB_VERT,0,size.cy,TRUE);
SetBothPos(int(x_cen/m_zoom*zoom-cx/2),int(y_cen/m_zoom*zoom-cy/2));
m_zoom = zoom;
}
// rect : Zoom rectangle ;
// size : Image width and height
// zoom : real zoomrate
// RECTANGLE ZOOM
void CScrollAssist_zhang::Change(CPoint centerPoint,CSize clientSize,
CSize imageSize,float zoom)
{
centerPoint.x += GetScrollPos(SB_HORZ);
centerPoint.y += GetScrollPos(SB_VERT);
imageSize.cx = (int)(imageSize.cx*zoom);
imageSize.cy = (int)(imageSize.cy*zoom);
imageSize.cx -= clientSize.cx;
imageSize.cy -= clientSize.cy;
imageSize.cx /= 10;
imageSize.cy /= 10;
if( imageSize.cx<0 ) imageSize.cx = 0;
if( imageSize.cy<0 ) imageSize.cy = 0;
ScrollRange = imageSize;
centerPoint.x = int(centerPoint.x/m_zoom*zoom- clientSize.cx/2);
centerPoint.y = int(centerPoint.y/m_zoom*zoom- clientSize.cy/2);
m_pwnd->SetScrollRange(SB_HORZ,0,imageSize.cx,TRUE);
m_pwnd->SetScrollRange(SB_VERT,0,imageSize.cy,TRUE);
SetBothPos(centerPoint.x,centerPoint.y);
m_zoom = zoom;
}
void CScrollAssist_zhang::SetStep(int step)
{
ScrollStep = step/10;
}
bool CScrollAssist_zhang::OnVScroll(UINT nSBCode,UINT nPos)
{
switch( nSBCode )
{
case SB_THUMBPOSITION:
VPosHi = (int)nPos;
break;
case SB_PAGEDOWN:
VPosHi += ScrollStep;
break;
case SB_PAGEUP:
VPosHi -= ScrollStep;
break;
case SB_LINEUP:
VPosLow -= ScrollStep;
break;
case SB_LINEDOWN:
VPosLow += ScrollStep;
break;
default:
return false;
}
if( VPosLow>9 ){ VPosLow = 0; VPosHi++; }
if( VPosLow<0 ){ VPosLow = 9; VPosHi--; }
if( VPosHi<0 ){ VPosHi = 0; VPosLow = 0; }
if( VPosHi>ScrollRange.cy ){ VPosHi = ScrollRange.cy; VPosLow = 9; }
m_pwnd->SetScrollPos( SB_VERT,VPosHi );
return true;
}
bool CScrollAssist_zhang::OnHScroll(UINT nSBCode,UINT nPos)
{
switch( nSBCode )
{
case SB_THUMBPOSITION:
HPosHi = (int)nPos;
break;
case SB_PAGEDOWN:
HPosHi += ScrollStep;
break;
case SB_PAGEUP:
HPosHi -= ScrollStep;
break;
case SB_LINEUP:
HPosLow -= ScrollStep;
break;
case SB_LINEDOWN:
HPosLow += ScrollStep;
break;
default:
return false;
}
if( HPosLow>9 ){ HPosLow = 0; HPosHi++; }
if( HPosLow<0 ){ HPosLow = 9; HPosHi--; }
if( HPosHi<0 ){ HPosHi = 0; HPosLow = 0; }
if( HPosHi>ScrollRange.cx ){ HPosHi = ScrollRange.cx; HPosLow = 9; }
m_pwnd->SetScrollPos( SB_HORZ,HPosHi );
return true;
}
void CScrollAssist_zhang::OnCreate(CWnd *pwnd)
{
m_pwnd = pwnd;
}
void CScrollAssist_zhang::ResetScroll()
{
m_pwnd->SetScrollPos( SB_HORZ,0 );
HPosHi = 0; HPosLow = 0;
m_pwnd->SetScrollPos( SB_VERT,0 );
VPosHi = 0; VPosLow = 0;
}
int CScrollAssist_zhang::GetScrollPos( int nBar )
{
if( nBar==SB_HORZ )
return m_pwnd->GetScrollPos(SB_HORZ)*10+HPosLow;
if( nBar==SB_VERT )
return m_pwnd->GetScrollPos(SB_VERT)*10+VPosLow;
return -1;
}
void CScrollAssist_zhang::SetBothPos(int x,int y)
{
HPosHi = x/10;
VPosHi = y/10;
HPosLow = x - HPosHi*10;
VPosLow = y - VPosHi*10;
if( VPosLow>9 ){ VPosLow = 0; VPosHi++; }
if( VPosLow<0 ){ VPosLow = 9; VPosHi--; }
if( VPosHi<0 ){ VPosHi = 0; VPosLow = 0; }
if( VPosHi>ScrollRange.cy ){ VPosHi = ScrollRange.cy; VPosLow = 9; }
if( HPosLow>9 ){ HPosLow = 0; HPosHi++; }
if( HPosLow<0 ){ HPosLow = 9; HPosHi--; }
if( HPosHi<0 ){ HPosHi = 0; HPosLow = 0; }
if( HPosHi>ScrollRange.cx ){ HPosHi = ScrollRange.cx; HPosLow = 9; }
m_pwnd->SetScrollPos( SB_HORZ,HPosHi );
m_pwnd->SetScrollPos( SB_VERT,VPosHi );
}
void CScrollAssist_zhang::SetLastOP()
{
short i;
if( m_curUndo == 10 )
{
for( i = 0;i>10-1;i++ )
{
m_Undo[i] = m_Undo[i+1];
}
m_curUndo--;
}
m_Undo[m_curUndo].range = ScrollRange;
m_Undo[m_curUndo].posx = GetScrollPos(SB_HORZ);
m_Undo[m_curUndo].posy = GetScrollPos(SB_VERT);
m_Undo[m_curUndo].zoom = m_zoom;
m_curUndo++;
}
short CScrollAssist_zhang::Undo(float* zoom)
{
if ( m_curUndo == 0) return 0;
m_curUndo--;
ScrollRange = m_Undo[m_curUndo].range;
*zoom = m_zoom = m_Undo[m_curUndo].zoom;
SetBothPos( m_Undo[m_curUndo].posx,m_Undo[m_curUndo].posy );
m_pwnd->SetScrollRange(SB_HORZ,0,ScrollRange.cx,TRUE);
m_pwnd->SetScrollRange(SB_VERT,0,ScrollRange.cy,TRUE);
return 1;
}