MFC 分页bar

来源:互联网 发布:职场女性知乎 编辑:程序博客网 时间:2024/06/09 18:37

bar的头文件

#ifndef __PAGERCTRLEXBAR_H__#define __PAGERCTRLEXBAR_H__#include "Sbartool.h"#include "CPagerCtrlEx.h"class CPagerCtrlExBar : public CToolBar{public:    static void SendGoto(CPagerCtrlEx* pCtrl, CWnd* pWnd);public:    CPagerCtrlExBar();    virtual ~CPagerCtrlExBar();    BOOL Init( CWnd* pParentWnd, FuncGotoIndex func, UINT nId );public:public:public:    virtual CSize CalcFixedLayout( BOOL bStretch, BOOL bHorz );    virtual CSize CalcDynamicLayout( int nLength, DWORD dwMode );public:    CPagerCtrlEx m_pagerCtrl;protected:    FuncGotoIndex m_funcGoto;    CWnd* m_pParent;    DECLARE_MESSAGE_MAP()public:    afx_msg void OnWindowPosChanged( WINDOWPOS* lpwndpos );};#endif //__PAGERCTRLEXBAR_H__

bar的实现文件

#include "StdAfx.h"#include "CPagerCtrlExBar.h"BEGIN_MESSAGE_MAP( CPagerCtrlExBar, CToolBar )    ON_WM_WINDOWPOSCHANGED()END_MESSAGE_MAP()void CPagerCtrlExBar::SendGoto( CPagerCtrlEx* pCtrl, CWnd* pWnd ){    CPagerCtrlExBar* pBar = static_cast<CPagerCtrlExBar*>(pWnd);    pBar->m_funcGoto( pCtrl, pBar->m_pParent );}CPagerCtrlExBar::CPagerCtrlExBar(){}CPagerCtrlExBar::~CPagerCtrlExBar(){}BOOL CPagerCtrlExBar::Init( CWnd* pParentWnd, FuncGotoIndex func, UINT nId ){    if ( pParentWnd == NULL || func == NULL )    {        assert( 0 );        return FALSE;    }    if ( CreateEx( pParentWnd,                   TBSTYLE_FLAT | TBSTYLE_TRANSPARENT,                   WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_FLYBY,                   CRect( 0, 0, 0, 0 ),                   nId ) == FALSE )        return FALSE;    m_pParent = pParentWnd;    m_funcGoto = func;    if ( m_pagerCtrl.Create( CPoint( 0, 0 ), this, 1000, CPagerCtrlExBar::SendGoto ) == FALSE )        return FALSE;    return TRUE;}CSize CPagerCtrlExBar::CalcFixedLayout( BOOL bStretch, BOOL bHorz ){    CRect rc;    m_pagerCtrl.GetWindowRect( rc );    return CSize( rc.Width() + 15, rc.Height() + 5 );}CSize CPagerCtrlExBar::CalcDynamicLayout( int nLength, DWORD dwMode ){    return CalcFixedLayout( dwMode & LM_STRETCH, dwMode & LM_HORZDOCK );}void CPagerCtrlExBar::OnWindowPosChanged( WINDOWPOS* lpwndpos ){    CToolBar::OnWindowPosChanged( lpwndpos );    if ( IsFloating() )    {        if ( m_pDockBar )        {            CWnd* pParent = m_pDockBar->GetParent();            if ( pParent == NULL )                return;            if ( pParent->IsKindOf( RUNTIME_CLASS( CMiniFrameWnd ) ) )            {                pParent->ModifyStyle( WS_SYSMENU, 0, 0 );            }        }    }}

分页控件的头文件

#ifndef __PAGERCTRLEX_H__#define __PAGERCTRLEX_H__#include "stdafx.h"class CPagerCtrlEx;typedef void( *FuncGotoIndex )(CPagerCtrlEx* pCtrl, CWnd* pParent);class CPagerCtrlEx : public CWnd{    DECLARE_DYNCREATE( CPagerCtrlEx )public:    enum PagerButtonItem    {        ITEM_PREV,        ITEM_NEXT,    };    CPagerCtrlEx(void);    ~CPagerCtrlEx(void);    virtual BOOL Create( const CPoint& pt, CWnd* pParentWnd, UINT nId, FuncGotoIndex func );    void SetProperty(int nPageTotal,int nCurrentIndex);    void SetBorderColor(COLORREF col);    UINT GetCurrentIndex();private:    void SetPageText();    void GotoPager(int nPage);protected:    virtual BOOL PreTranslateMessage(MSG* pMsg);    virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);    afx_msg void OnPaint();    DECLARE_MESSAGE_MAP()private:    CWnd*           m_pParentWnd;    CButton         m_btnFirst;    CButton         m_btnPrev;    CButton         m_btnNext;    CButton         m_btnLast;    CEdit           m_editGoto;    CStatic         m_staticText;    int             m_nPageTotal;           int             m_nCurrentIndex;        COLORREF        m_BorderColor;     FuncGotoIndex   m_func;};#endif //__PAGERCTRLEX_H__

分页控件的实现文件

#include "CPagerCtrlEx.h"#define PAGER_WIDTH         150#define PAGER_HEIGHT        20#define BUTTON_WIDTH        20#define PAGER_ID_BUTTON_PREV  (1001)#define PAGER_ID_BUTTON_NEXT  (1002)#define PAGER_ID_STATIC_PANEL (1004)#define PAGER_ID_EDIT_GOTO    (1005)#define PAGER_ID_STATIC_TEXT  (1006)CPagerCtrlEx::CPagerCtrlEx( ){    m_pParentWnd = NULL;    m_nPageTotal = 0;    m_nCurrentIndex = 0;    m_BorderColor = RGB( 41, 101, 153 );}CPagerCtrlEx::~CPagerCtrlEx( ){}IMPLEMENT_DYNCREATE( CPagerCtrlEx, CWnd )BEGIN_MESSAGE_MAP( CPagerCtrlEx, CWnd )    //{{AFX_MSG_MAP(COutlookMenu)    //}}AFX_MSG_MAP    ON_WM_PAINT()END_MESSAGE_MAP()BOOL CPagerCtrlEx::Create( const CPoint& pt, CWnd* pParentWnd, UINT nId, FuncGotoIndex func ){    VERIFY( m_pParentWnd = pParentWnd );    VERIFY( m_func = func );    LPCTSTR lpszClassName = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, AfxGetApp()->LoadStandardCursor( IDC_ARROW ), (HBRUSH)GetStockObject( WHITE_BRUSH ), NULL );    BOOL ret = CWnd::Create( lpszClassName, NULL, WS_CHILDWINDOW | WS_VISIBLE, CRect( pt.x, pt.y, pt.x + PAGER_WIDTH, pt.y + PAGER_HEIGHT ), pParentWnd, nId );    if ( ret == FALSE)    {        return FALSE;    }    CRect rcPrev( 0, 0, BUTTON_WIDTH, PAGER_HEIGHT );    CRect rcNext( PAGER_WIDTH - BUTTON_WIDTH, 0, PAGER_WIDTH, PAGER_HEIGHT );    CRect rcGoto( rcPrev.right + 1, 0, rcPrev.right + 50, PAGER_HEIGHT );    CRect rcText( rcGoto.right + 1, rcGoto.top, rcNext.left, PAGER_HEIGHT );    m_btnPrev.Create( _T( "←" ), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, rcPrev, this, PAGER_ID_BUTTON_PREV );    m_btnNext.Create( _T( "→" ), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, rcNext, this, PAGER_ID_BUTTON_NEXT );    m_editGoto.Create( WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_CENTER, rcGoto, this, PAGER_ID_EDIT_GOTO );    m_staticText.Create( NULL, WS_CHILDWINDOW | WS_VISIBLE | SS_CENTER, rcText, this, PAGER_ID_STATIC_TEXT );    if ( m_pParentWnd )    {        SetFont( m_pParentWnd->GetFont() );        m_editGoto.SetFont( m_pParentWnd->GetFont() );        m_staticText.SetFont( m_pParentWnd->GetFont() );    }    MoveWindow( 0, 0, PAGER_WIDTH, PAGER_HEIGHT );    return ret;}void CPagerCtrlEx::SetProperty( int nPageTotal, int nCurrentIndex ){    if ( nPageTotal <= 0 || nCurrentIndex <= 0 )    {        nPageTotal = 1;        nCurrentIndex = 1;    }    m_nPageTotal = nPageTotal - 1;    m_nCurrentIndex = nCurrentIndex - 1;    SetPageText();}void CPagerCtrlEx::SetBorderColor( COLORREF col ){    m_BorderColor = col;}UINT CPagerCtrlEx::GetCurrentIndex(){    return m_nCurrentIndex;}void CPagerCtrlEx::SetPageText(){    TCHAR szText[32];    _stprintf( szText, _T( "%d/%d" ), m_nCurrentIndex + 1, m_nPageTotal + 1 );    m_staticText.SetWindowText( szText );    _stprintf( szText, _T( "%d" ), m_nCurrentIndex + 1 );    m_editGoto.SetWindowText( szText );}void CPagerCtrlEx::GotoPager( int nPage ){    --nPage;    if ( m_nCurrentIndex == nPage )        return;    if ( nPage < 0 )        m_nCurrentIndex = 0;    else if ( nPage > m_nPageTotal )        m_nCurrentIndex = m_nPageTotal;    else        m_nCurrentIndex = nPage;    m_func( this, m_pParentWnd );    SetPageText();}BOOL CPagerCtrlEx::OnCommand( WPARAM wParam, LPARAM lParam ){    int nCtrlID = LOWORD( wParam );    switch ( nCtrlID )    {        case PAGER_ID_BUTTON_PREV:            if ( m_nCurrentIndex > 0 )            {                --m_nCurrentIndex;                m_func( this, m_pParentWnd );                SetPageText();            }            break;        case PAGER_ID_BUTTON_NEXT:            if ( m_nCurrentIndex < m_nPageTotal )            {                ++m_nCurrentIndex;                m_func( this, m_pParentWnd );                SetPageText();            }            break;    }    return CWnd::OnCommand( wParam, lParam );}void CPagerCtrlEx::OnPaint(){    CPaintDC dc( this );    if ( !m_pParentWnd )    {        return;    }}BOOL CPagerCtrlEx::PreTranslateMessage( MSG* pMsg ){    if ( pMsg->message == WM_KEYDOWN )    {        if ( pMsg->wParam == VK_RETURN )        {            if ( !(GetKeyState( VK_CONTROL ) & 0x8000) )            {                if ( m_editGoto.GetSafeHwnd() == pMsg->hwnd )                {                    CString strText = "";                    m_editGoto.GetWindowText( strText );                    if ( !strText.IsEmpty() )                    {                        GotoPager( _tcstoul( strText, NULL, 0 ) );                    }                    return TRUE;                }            }        }        if ( pMsg->wParam == 0x56 )        {            if ( GetKeyState( VK_CONTROL ) & 0x8000 )            {                if ( m_editGoto.GetSafeHwnd() == pMsg->hwnd )                {                    return TRUE;                }            }        }    }    return CWnd::PreTranslateMessage( pMsg );}

效果这里写图片描述