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

📄 scrolldialog.cpp

📁 WinCE开发技巧与实例的配套源码
💻 CPP
字号:
// ScrollDialog.cpp : implementation file
//
#include "stdafx.h"
//#include "resource.h"
#include "ScrollDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CScrollDialog dialog
#define HORZ_PTS 8
#define VERT_PTS 4

CScrollDialog::CScrollDialog(UINT nIDD,CWnd* pParent /*=NULL*/)
	: CDialog(nIDD, pParent)
{
	//{{AFX_DATA_INIT(CScrollDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CScrollDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScrollDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CScrollDialog, CDialog)
	//{{AFX_MSG_MAP(CScrollDialog)
	ON_WM_VSCROLL()
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScrollDialog message handlers

void CScrollDialog::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    int nInc;
    switch (nSBCode)
    {
        case SB_TOP:        nInc = -m_nVscrollPos;               break;
        case SB_BOTTOM:     nInc = m_nVscrollMax-m_nVscrollPos;  break;
        case SB_LINEUP:     nInc = -1;                           break;
        case SB_LINEDOWN:   nInc = 1;                            break;
        case SB_PAGEUP:     nInc = min(-1, -m_nVertInc);         break;
        case SB_PAGEDOWN:   nInc = max(1, m_nVertInc);           break;
        case SB_THUMBTRACK: nInc = nPos - m_nVscrollPos;         break;
        default:            nInc = 0;
    }

    nInc = max(-m_nVscrollPos, min(nInc, m_nVscrollMax - m_nVscrollPos));

    if (nInc)
    {
        m_nVscrollPos += nInc;
        int iMove = -VERT_PTS * nInc;
		ScrollWindowEx(0,iMove,
			NULL,NULL,NULL,NULL,
			SW_SCROLLCHILDREN|SW_INVALIDATE);
        SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);
    }
	InvalidateRect(NULL);

	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

BOOL CScrollDialog::OnInitDialog() 
{
	CRect m_WindowRect;
	GetWindowRect(&m_WindowRect);
	//Get Dialog temp's size,must be before CDialog::OnInitDialog() 
	CDialog::OnInitDialog();

	m_nHscrollPos = 0;
    m_nVscrollPos = 0;
	
	CRect tempRect;
    if(GetParent())
		GetParent()->GetClientRect(&tempRect);
	else
		GetClientRect(&tempRect);

    // Calculate how many scrolling increments for the client area
	int sx=GetSystemMetrics(SM_CXHSCROLL);
	int sy=GetSystemMetrics(SM_CYVSCROLL);
	int WinWidth = m_WindowRect.right+m_WindowRect.left;
	int WinHeight = m_WindowRect.bottom+m_WindowRect.top;

	if(WinWidth <= tempRect.Width())
		m_nHorzInc = (WinWidth - tempRect.Width())/HORZ_PTS;
	else
		m_nHorzInc = (WinWidth+sx - tempRect.Width())/HORZ_PTS;
	if(WinHeight <= tempRect.Height())
		m_nVertInc = (WinHeight+sy - tempRect.Height())/VERT_PTS;
	else
		m_nVertInc = (WinHeight - tempRect.Height())/VERT_PTS;

    // Set the vertical and horizontal scrolling info
    m_nHscrollMax = max(0, m_nHorzInc);
    m_nHscrollPos = min (m_nHscrollPos, m_nHscrollMax);
    SetScrollRange(SB_HORZ, 0, m_nHscrollMax, FALSE);
    SetScrollPos(SB_HORZ, m_nHscrollPos, TRUE);
    m_nVscrollMax = max(0, m_nVertInc);
    m_nVscrollPos = min(m_nVscrollPos, m_nVscrollMax);
    SetScrollRange(SB_VERT, 0, m_nVscrollMax, FALSE);
    SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CScrollDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    int nInc;
    switch (nSBCode)
    {
        case SB_TOP:        nInc = -m_nHscrollPos;               break;
        case SB_BOTTOM:     nInc = m_nHscrollMax-m_nHscrollPos;  break;
        case SB_LINEUP:     nInc = -1;                           break;
        case SB_LINEDOWN:   nInc = 1;                            break;
        case SB_PAGEUP:     nInc = -HORZ_PTS;                    break;
        case SB_PAGEDOWN:   nInc = HORZ_PTS;                     break;
        case SB_THUMBTRACK: nInc = nPos - m_nHscrollPos;         break;
        default:            nInc = 0;

    }

    nInc = max(-m_nHscrollPos, min(nInc, m_nHscrollMax - m_nHscrollPos));

    if (nInc)
    {
        m_nHscrollPos += nInc;
        int iMove = -HORZ_PTS * nInc;
        ScrollWindowEx(iMove,0,
			NULL,NULL,NULL,NULL,
			SW_SCROLLCHILDREN|SW_INVALIDATE);
		SetScrollPos(SB_HORZ, m_nHscrollPos, TRUE);
    }	
	InvalidateRect(NULL);
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
//This Function achieve scroll the compenents on dialog .
//It's written by MicroSoft in file named dlgcore.cpp
//The Function named ScrollWindow(...) can NOT work,But 
//ScrollWindowEx(...) work well
void CScrollDialog::ScrollContent(int xAmount, int yAmount)
{
	HWND hWndChild = ::GetWindow(m_hWnd, GW_CHILD);
	if (hWndChild != NULL)
	{
		for (; hWndChild != NULL;
			hWndChild = ::WCE_FCTN(GetNextWindow)(hWndChild, GW_HWNDNEXT))
		{
			CRect rect;
			::GetWindowRect(hWndChild, &rect);
			ScreenToClient(&rect);
			::SetWindowPos(hWndChild, NULL,
				rect.left+xAmount, rect.top+yAmount, 0, 0,
				SWP_NOSIZE|SWP_NOACTIVATE|SWP_NOZORDER);
		}
	}
}

⌨️ 快捷键说明

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