写了个MFC的分页控件,分享一下。使用方法:1、分页控件父窗体继承CPagerNotify类,并重写virtual void OnGotoPager(int nIndex)方法2、声明变量 CPagerCtrlEx m_Pager;3、在窗体的OnInitDialog()方法中写入下面代码设置图片: m_Pager.Create(CPoint(200,200),this); // 第一个参数为控件坐标 m_Pager.SetNotifyManager(this);
 m_Pager.SetProperty(20,1);
 m_Pager.SetBitmaps(ITEM_FIRST,IDB_PAGER_1_1, IDB_PAGER_1_2, IDB_PAGER_1_3, IDB_PAGER_1_1);
 m_Pager.SetBitmaps(ITEM_PREV,IDB_PAGER_2_1, IDB_PAGER_2_2, IDB_PAGER_2_3, IDB_PAGER_2_1);
 m_Pager.SetBitmaps(ITEM_NEXT,IDB_PAGER_3_1, IDB_PAGER_3_2, IDB_PAGER_3_3, IDB_PAGER_3_1);
 m_Pager.SetBitmaps(ITEM_LAST,IDB_PAGER_4_1, IDB_PAGER_4_2, IDB_PAGER_4_3, IDB_PAGER_4_1);4、效果图如下:5、例子下载:http://download.csdn.net/source/3025013本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yfqvip/archive/2011/02/16/6188451.aspx

解决方案 »

  1.   

    把源码贴一下:
    ImgButton.h
    /////////////////////////////////////////////////////////////////////////////
    // Name:        IMGBUTTON.H
    // Purpose:     图片按钮
    // Author:      [email protected]
    // Modified by: 
    // Created:     2011/2/16 16:13
    // RCS-ID:      
    // Copyright:   
    // Licence:     
    /////////////////////////////////////////////////////////////////////////////#ifndef __IMGBUTTON_H__
    #define __IMGBUTTON_H__class CImgButton : public CButton
    {
    DECLARE_DYNAMIC(CImgButton)
    public:
    CImgButton();
    virtual ~CImgButton();
    BOOL Create(const RECT& rect, CWnd* pParentWnd);
    void SetBitmaps(int nNormal = 0, int nHot = 0, int nPressed = 0, int nDisable = 0);
    protected:
    DECLARE_MESSAGE_MAP()
    private:
    CBitmap m_bmpNormal;
    CBitmap m_bmpHot;
    CBitmap m_bmpPressed;
    CBitmap m_bmpDisable;
    BOOL m_MouseHover;
    CWnd* m_pParentWnd;
    protected:
    virtual void PreSubclassWindow();
    virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
    };
    #endif //__IMGBUTTON_H__ImgButton.cpp#include "StdAfx.h"
    #include "ImgButton.h"IMPLEMENT_DYNAMIC(CImgButton, CButton)CImgButton::CImgButton()
    : m_MouseHover(FALSE)
    , m_pParentWnd(NULL)
    {
    }CImgButton::~CImgButton()
    {
    }BEGIN_MESSAGE_MAP(CImgButton, CButton)
    ON_WM_MOUSEMOVE()
    ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
    END_MESSAGE_MAP()BOOL CImgButton::Create(const RECT& rect, CWnd* pParentWnd)
    {
    m_pParentWnd = pParentWnd;
    static UINT nID = WM_USER + 200;
    BOOL ret = CButton::Create(NULL,WS_CHILDWINDOW | WS_VISIBLE | SS_CENTER |WS_VISIBLE | BS_PUSHBUTTON /*|WS_BORDER*/, rect, pParentWnd, ++nID);
    if(ret)
    {
    SetFont(pParentWnd->GetFont());
    }
    return ret;
    }void CImgButton::SetBitmaps(int nNormal, int nHot, int nPressed, int nDisable)
    {
    if (nNormal)
    {
    m_bmpNormal.LoadBitmap(nNormal);
    }
    if (nHot)
    {
    m_bmpHot.LoadBitmap(nHot);
    }
    if (nPressed)
    {
    m_bmpPressed.LoadBitmap(nPressed);
    }
    if (nDisable)
    {
    m_bmpDisable.LoadBitmap(nDisable);
    }
    }void CImgButton::PreSubclassWindow()
    {
    ModifyStyle(0, BS_OWNERDRAW); CButton::PreSubclassWindow();
    }void CImgButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
    HBITMAP bitmap = (HBITMAP)m_bmpNormal;
    if (lpDrawItemStruct->itemState & ODS_DISABLED)
    {
    bitmap = (HBITMAP)m_bmpDisable;
    }
    else if (lpDrawItemStruct->itemState & ODS_SELECTED)
    {
    bitmap = (HBITMAP)m_bmpPressed;
    }
    else if (m_MouseHover)
    {
    bitmap = (HBITMAP)m_bmpHot;
    } RECT rc;
    GetClientRect(&rc);
    HDC dc = CreateCompatibleDC(lpDrawItemStruct->hDC);
    HBITMAP oldBitmap = (HBITMAP)SelectObject(dc,bitmap);
    BitBlt(lpDrawItemStruct->hDC, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, dc, 0, 0, SRCCOPY);
    SelectObject(dc,oldBitmap);
    DeleteDC(dc); CString strText;
    GetWindowText(strText);
    SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
    DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
    }void CImgButton::OnMouseMove(UINT nFlags, CPoint point)
    {
    if (!m_MouseHover)
    {
    Invalidate();
    TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, m_hWnd, 0};
    m_MouseHover = TrackMouseEvent(&tme);
    } CButton::OnMouseMove(nFlags, point);
    }LRESULT CImgButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
    {
    Invalidate();
    m_MouseHover = FALSE;
    return 0;
    }
      

  2.   

    Label.h
    /////////////////////////////////////////////////////////////////////////////
    // Name:        LABEL.H
    // Purpose:     标签控件
    // Author:      [email protected]
    // Modified by: 
    // Created:     2010/12/23 14:27
    // RCS-ID:      
    // Copyright:   
    // Licence:     
    /////////////////////////////////////////////////////////////////////////////#ifndef __LABEL_H__
    #define __LABEL_H__class CLabel : public CStatic
    {
    DECLARE_DYNCREATE(CLabel)
    public:
    CLabel(void);
    ~CLabel(void);
    BOOL Create(const RECT& rect, CWnd* pParentWnd);
    private: 
    COLORREF m_BorderColor; 
    COLORREF m_TextColor; 
    COLORREF m_BackColor; 
    CBrush m_Brush;  //   Operations 
    public:
    void SetBorderColor(COLORREF col);
    void SetTextColor(COLORREF col);
    void SetBackColor(COLORREF col);
    void SetWindowText(LPCSTR text);
    void UpdateCtrl();
    private:
    CString m_strText;
    CWnd* m_pParentWnd;
    public:
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    afx_msg void OnPaint(); DECLARE_MESSAGE_MAP()};#endif //__LABEL_H__
    Label.cpp#include "StdAfx.h"
    #include "Label.h"
    #include "UCtrlID.h"#define TRANS_BACK -1CLabel::CLabel(void)
    {
    m_pParentWnd = NULL;
    m_strText = "";
    m_TextColor = RGB(225, 112, 5);
    m_BorderColor = RGB(0,100,200);
    m_BackColor = TRANS_BACK;
    }CLabel::~CLabel(void)
    {
    }IMPLEMENT_DYNCREATE(CLabel, CStatic)
    BEGIN_MESSAGE_MAP(CLabel, CStatic)
    ON_WM_CTLCOLOR_REFLECT()
    ON_WM_PAINT()
    END_MESSAGE_MAP()
    BOOL CLabel::Create(const RECT& rect, CWnd* pParentWnd)
    {
    m_pParentWnd = pParentWnd;
    static UINT nID = ID_LABEL_CTRL;
    BOOL ret = CStatic::Create(NULL,WS_CHILDWINDOW | WS_VISIBLE | SS_CENTER, rect, pParentWnd, ++nID);
    if(ret)
    {
    SetFont(pParentWnd->GetFont());
    }
    return ret;
    }
    void CLabel::OnPaint()
    {
    CPaintDC dc(this); // device context for painting
    if (!m_pParentWnd)
    {
    return;
    }
    CRect rect;
    GetClientRect(rect);
    CBrush brushFrame(m_BorderColor);
    dc.FrameRect(&rect,&brushFrame);
    dc.SelectObject(GetFont());
    dc.DrawText(m_strText,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE); 
    }
    HBRUSH CLabel::CtlColor(CDC* pDC, UINT nCtlColor)
    {
    m_Brush.DeleteObject();
    if(m_BackColor == TRANS_BACK)
    {
    m_Brush.CreateStockObject(HOLLOW_BRUSH);
    pDC->SetBkMode(TRANSPARENT);
    }
    else
    {
    m_Brush.CreateSolidBrush(m_BackColor);
    pDC->SetBkColor(m_BackColor);
    } SetTextColor(m_TextColor); return (HBRUSH)m_Brush;
    }void CLabel::UpdateCtrl()

    CWnd* pParent = GetParent();
    CRect rect; GetWindowRect(rect);
    pParent->ScreenToClient(rect);
    rect.DeflateRect(2,2); pParent->InvalidateRect(rect,FALSE);

    void CLabel::SetBorderColor(COLORREF col)
    {
    m_BorderColor = col;
    UpdateCtrl();
    CRect rect;
    GetClientRect(rect);
    InvalidateRect(rect,FALSE);
    }
    void CLabel::SetTextColor(COLORREF col)
    {
    m_TextColor = col;
    UpdateCtrl();
    }
    void CLabel::SetBackColor(COLORREF col)
    {
    m_BackColor = col;
    UpdateCtrl();
    }
    void CLabel::SetWindowText(LPCSTR text)
    {
    m_strText = text;
    CStatic::SetWindowText(text);
    CRect rect;
    GetClientRect(rect);
    InvalidateRect(rect,FALSE);
    }UCtrlID.h#ifndef __UCTRLID_H__
    #define __UCTRLID_H__#define ID_PAGER_CTRL WM_USER + 1000;
    #define ID_LABEL_CTRL WM_USER + 2000;#endif //__UCTRLID_H__PagerCtrlEx.h
    /////////////////////////////////////////////////////////////////////////////
    // Name:        PAGERCTRLEX.H
    // Purpose:     分页控件
    // Author:      [email protected]
    // Modified by: 
    // Created:     2011/2/15 16:16
    // RCS-ID:      
    // Copyright:   
    // Licence:     
    /////////////////////////////////////////////////////////////////////////////#ifndef __PAGERCTRLEX_H__
    #define __PAGERCTRLEX_H__
    #include "ImgButton.h"
    #include "Label.h"enum ButtonItem
    {
    ITEM_FIRST,
    ITEM_PREV,
    ITEM_NEXT,
    ITEM_LAST
    };class CPagerNotify
    {
    public:
    // 索引从0开始
    virtual void OnGotoPager(int nIndex){}
    };class CPagerCtrlEx : public CWnd
    {
    DECLARE_DYNCREATE(CPagerCtrlEx)
    public:
    CPagerCtrlEx(void);
    ~CPagerCtrlEx(void);
    virtual BOOL Create(const CPoint& pt, CWnd* pParentWnd);
    void SetNotifyManager(CPagerNotify *pNotify);
    // 索引从1开始
    void SetProperty(int nPageTotal,int nCurrentIndex);
    void SetBitmaps(ButtonItem eItem,int nNormal = 0, int nHot = 0, int nPressed = 0, int nDisable = 0);
    void SetBorderColor(COLORREF col);
    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;
    CImgButton m_btnFirst;
    CImgButton m_btnPrev;
    CImgButton m_btnNext;
    CImgButton m_btnLast;
    CLabel m_lblPanel; /*< 输入框父控件 */
    CEdit m_txtGoto;
    CLabel m_lblText;
    CPagerNotify* m_pNotify;
    int m_nPageTotal; /*< 索引从零开始 */
    int m_nCurrentIndex; /*< 索引从零开始 */
    COLORREF m_BorderColor; 
    };#endif //__PAGERCTRLEX_H__
      

  3.   

    PagerCtrlEx.cpp#include "StdAfx.h"
    #include "PagerCtrlEx.h"
    #include "UCtrlID.h"
    #include <assert.h>#define PAGER_WIDTH 170
    #define PAGER_HEIGHT 20
    #define BUTTON_WIDTH 20
    #define GAP_WIDTH 0 /*< 按钮之前的间隔 */
    #define LINE_WIDTH_HEIGHT 1 /*< 边线宽高 */#pragma warning(disable:4996)
    CPagerCtrlEx::CPagerCtrlEx(void)
    {
    m_pParentWnd = NULL;
    m_pNotify = NULL;
    m_nPageTotal = 0;
    m_nCurrentIndex = 0;
    m_BorderColor = RGB(41,101,153);
    }CPagerCtrlEx::~CPagerCtrlEx(void)
    {
    }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)
    {
    m_pParentWnd = pParentWnd; static UINT nID = ID_PAGER_CTRL; 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+LINE_WIDTH_HEIGHT*2,pt.y+PAGER_HEIGHT+LINE_WIDTH_HEIGHT*2), pParentWnd, ++nID);
    if(ret)
    {
    SetFont(pParentWnd->GetFont());
    } CRect rcFirst(LINE_WIDTH_HEIGHT,LINE_WIDTH_HEIGHT,BUTTON_WIDTH+LINE_WIDTH_HEIGHT,PAGER_HEIGHT+LINE_WIDTH_HEIGHT);
    CRect rcLast(PAGER_WIDTH-BUTTON_WIDTH*1-LINE_WIDTH_HEIGHT,LINE_WIDTH_HEIGHT,PAGER_WIDTH-LINE_WIDTH_HEIGHT,PAGER_HEIGHT+LINE_WIDTH_HEIGHT);
    CRect rcPrev(rcFirst.right,rcFirst.top,rcFirst.right+rcFirst.Width(),rcFirst.bottom);
    CRect rcNext(rcLast.left-rcLast.Width(),rcLast.top,rcLast.left,rcLast.bottom);
    CRect rcPanel(rcPrev.right,rcPrev.top,rcPrev.right+40,rcPrev.bottom);
    CRect rcGoto(1,3,rcPanel.Width()-1,rcPanel.Height()-1);
    CRect rcText(rcPanel.right,rcPanel.top,rcNext.left,rcPanel.bottom); m_btnFirst.Create(rcFirst, this);
    m_btnPrev.Create(rcPrev, this);
    m_btnNext.Create(rcNext, this);
    m_btnLast.Create(rcLast, this);
    m_lblPanel.Create(rcPanel,this);
    m_txtGoto.Create(WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|ES_NUMBER,rcGoto, &m_lblPanel, ++nID);
    m_lblText.Create(rcText,this); m_lblPanel.SetBorderColor(RGB(193,225,253));
    m_lblText.SetBorderColor(RGB(248,248,248));
    m_txtGoto.SetFont(pParentWnd->GetFont());
    m_lblText.SetFont(pParentWnd->GetFont()); return ret;
    }
    void CPagerCtrlEx::SetNotifyManager(CPagerNotify *pNotify)
    {
    m_pNotify = pNotify;
    }
    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::SetBitmaps(ButtonItem eItem,int nNormal, int nHot, int nPressed, int nDisable)
    {
    switch (eItem)
    {
    case ITEM_FIRST:
    m_btnFirst.SetBitmaps(nNormal, nHot, nPressed, nDisable);
    break;
    case ITEM_PREV:
    m_btnPrev.SetBitmaps(nNormal, nHot, nPressed, nDisable);
    break;
    case ITEM_NEXT:
    m_btnNext.SetBitmaps(nNormal, nHot, nPressed, nDisable);
    break;
    case ITEM_LAST:
    m_btnLast.SetBitmaps(nNormal, nHot, nPressed, nDisable);
    break;
    }}
    void CPagerCtrlEx::SetBorderColor(COLORREF col)
    {
    m_BorderColor = col;
    }
    void CPagerCtrlEx::SetPageText()
    {
    char strText[20] = {0};
    sprintf(strText,"%d/%d",m_nCurrentIndex+1,m_nPageTotal+1);
    m_lblText.SetWindowText(strText);
    memset(strText,0,20);
    sprintf(strText,"%d",m_nCurrentIndex+1);
    m_txtGoto.SetWindowText(strText);
    // 刷新控件
    CRect rect;
    m_lblText.GetClientRect(&rect);
    m_lblText.MapWindowPoints(this,rect);
    InvalidateRect(&rect);
    }
    void CPagerCtrlEx::GotoPager(int nPage)
    {
    if (m_nCurrentIndex != nPage-1)
    {
    if (nPage-1 < 0)
    {
    m_nCurrentIndex = 0;
    }
    else if (nPage-1 > m_nPageTotal)
    {
    m_nCurrentIndex = m_nPageTotal;
    }
    else
    {
    m_nCurrentIndex = nPage-1;
    }
    m_pNotify->OnGotoPager(m_nCurrentIndex);
    SetPageText();
    }
    }
    BOOL CPagerCtrlEx::OnCommand(WPARAM wParam, LPARAM lParam)
    {
    assert(m_pNotify);
    int nCtrlID = LOWORD(wParam);
    if (nCtrlID == m_btnFirst.GetDlgCtrlID())
    {
    if (m_nCurrentIndex > 0)
    {
    m_nCurrentIndex = 0;
    m_pNotify->OnGotoPager(m_nCurrentIndex);
    SetPageText();
    }
    }
    if (nCtrlID == m_btnPrev.GetDlgCtrlID())
    {
    if (m_nCurrentIndex > 0)
    {
    m_pNotify->OnGotoPager(--m_nCurrentIndex);
    SetPageText();
    }
    }
    if (nCtrlID == m_btnNext.GetDlgCtrlID())
    {
    if (m_nCurrentIndex < m_nPageTotal)
    {
    m_pNotify->OnGotoPager(++m_nCurrentIndex);
    SetPageText();
    }
    }
    if (nCtrlID == m_btnLast.GetDlgCtrlID())
    {
    if (m_nCurrentIndex < m_nPageTotal)
    {
    m_nCurrentIndex = m_nPageTotal;
    m_pNotify->OnGotoPager(m_nCurrentIndex);
    SetPageText();
    }
    }
    return CWnd::OnCommand(wParam, lParam);
    }
    void CPagerCtrlEx::OnPaint()
    {
    CPaintDC dc(this); // device context for painting
    if (!m_pParentWnd)
    {
    return;
    }
    CRect rect;
    GetClientRect(rect);
    CBrush brushFrame(m_BorderColor);
    dc.FrameRect(&rect,&brushFrame);
    }
    BOOL CPagerCtrlEx::PreTranslateMessage(MSG* pMsg)
    {
    if (pMsg->message == WM_KEYDOWN)
    {
    if (pMsg->wParam == VK_RETURN)
    {
    if (!(GetKeyState(VK_CONTROL) & 0x8000))
    {
    if (m_txtGoto.GetSafeHwnd() == pMsg->hwnd)
    {
    assert(m_pNotify);
    CString strText = "";
    m_txtGoto.GetWindowText(strText);
    if (!strText.IsEmpty())
    {
    GotoPager(atoi(strText));
    }
    return TRUE; //滤掉Enter
    }
    }
    }
    if (pMsg->wParam == 0x56 )
    {
    if (GetKeyState(VK_CONTROL) & 0x8000)
    {
    if (m_txtGoto.GetSafeHwnd() == pMsg->hwnd)
    {
    return TRUE; //滤掉ctrl+v
    }
    }
    }
    }
    return CWnd::PreTranslateMessage(pMsg);
    }
    #pragma warning(default:4996)
      

  4.   

    谢谢斑竹推荐,稍后我会把最新demo的下载地址更新在博客里~~
      

  5.   

    最新demo源码下载地址:http://download.csdn.net/source/3025947
      

  6.   

    csdn会删除技术区的灌水回复
    像这种推荐帖会被特别关注
    有些也不是我删的
      

  7.   

    在窗体的OnInitDialog()方法中写入下面代码设置图片: m_Pager.Create(CPoint(200,200),this); // 第一个参数为控件坐标 m_Pager.SetNotifyManager(this);
     m_Pager.SetProperty(20,1);
     m_Pager.SetBitmaps(ITEM_FIRST,IDB_PAGER_1_1, IDB_PAGER_1_2, IDB_PAGER_1_3, IDB_PAGER_1_1);
     m_Pager.SetBitmaps(ITEM_PREV,IDB_PAGER_2_1, IDB_PAGER_2_2, IDB_PAGER_2_3, IDB_PAGER_2_1);
     m_Pager.SetBitmaps(ITEM_NEXT,IDB_PAGER_3_1, IDB_PAGER_3_2, IDB_PAGER_3_3, IDB_PAGER_3_1);
     m_Pager.SetBitmaps(ITEM_LAST,IDB_PAGER_4_1, IDB_PAGER_4_2, IDB_PAGER_4_3, IDB_PAGER_4_1);