您的位置:首页 > 其它

CStaticPage类 页码控件类

2016-03-10 12:40 225 查看




头文件:

#pragma once

// CStaticPage

enum{

PAGE_CICK = 0,//点击

JUMP_NEXT_PAGE = 1,//跳转下一页

JUMP_UP_PAGE = 2,//跳转上一页

JUMP_SELECT_PAGE = 3,//跳转选中页码

DRAW_PAGE_WIDTH = 50//页码绘制宽度 当前这个宽度可以之车99999

};

//页码操作回调函数

typedef void(__stdcall *PFNPAGEOPERATION)(unsigned int nPage, unsigned int nOperation, void *pUserData );

class CStaticPage : public CStatic

{

DECLARE_DYNAMIC(CStaticPage)

DECLARE_MESSAGE_MAP()

public:

CStaticPage();

virtual ~CStaticPage();

public:

//初始化

void InitPage(__in unsigned int nSize=1, __in unsigned nCurPage=1, __in unsigned int nCurPageSize=6 );

//设置/获取页码

void SetPageSize(__in unsigned int nSize);

unsigned int GetPageSize();

//设置/获取当前页

void SetCurrentPage(__in unsigned int nPage);

unsigned int GetCurrentPage();

//设置/获取page颜色

void SetPageColorref(COLORREF clr);

COLORREF GetPageColorref();

void SetDefaultPageColorref();

//设置操作页码回调函数 如果设置了回调函数 又不需要了 可以继续调用次接口pfnPageOperation参数传NULL

void SetCallBackPageOperation(__in PFNPAGEOPERATION pfnPageOperation, __in void *pUserData);

protected:

virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

private:

unsigned int m_nPageSize;//页码总数

unsigned int m_nCurrentPage;//当前页

unsigned int m_nCurrentPageSize;//当前页显示总数

unsigned int m_nCurrentEndPage;//当前页码起始页

unsigned int m_nCurrentStartPage;//当前页码起始页

unsigned int m_nJumpPageFlog;//跳转页码标记

COLORREF m_clrPage;//页码颜色

PFNPAGEOPERATION m_pfnPageOperation;//操作回调函数指针

void *m_pUserData;//用户自定义数据

private:

unsigned int ClickPage(__in CPoint pt);//单击造成选中的页码

void DrawPage(__in CDC *pDC ,__in unsigned int nSizePage, __in unsigned int nCurPage);

void JumpPage( __in unsigned int nJumpFlog );

COLORREF AdjustBrightness(CONST COLORREF clrSrcPixel, CONST INT nPercent);//调整颜色亮度

virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);

};

源文件

// StaticPage.cpp : 实现文件

//

#include "stdafx.h"

#include "StaticPage.h"

#include

// CStaticPage

IMPLEMENT_DYNAMIC(CStaticPage, CStatic)

CStaticPage::CStaticPage()

{

m_nCurrentPage = 1;

m_nPageSize = 1;

m_nCurrentStartPage = 1;

m_nJumpPageFlog = JUMP_SELECT_PAGE;

m_pfnPageOperation = NULL;

m_pUserData = NULL;

m_clrPage = RGB(116, 184, 184);

}

CStaticPage::~CStaticPage()

{

}

BEGIN_MESSAGE_MAP(CStaticPage, CStatic)

END_MESSAGE_MAP()

// CStaticPage 消息处理程序

void CStaticPage::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

{

CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

pDC->FillSolidRect(&lpDrawItemStruct->rcItem,GetBkColor(pDC->m_hDC));

DrawPage(pDC, m_nPageSize, m_nCurrentPage);

}

void CStaticPage::DrawPage(__in CDC *pDC, __in unsigned int nSizePage, __in unsigned int nCurPage)

{

pDC->SetBkMode(TRANSPARENT);

CRect rc;

GetClientRect(rc);

int nWidth = rc.Width();

int nHeight = rc.Height();//高度做为页码的绘制高宽

//计算当前页的起始页和结束页

const int nDrawPageSize = nWidth / DRAW_PAGE_WIDTH - 2; //可以绘制的正方形区域总数

m_nCurrentPageSize = nDrawPageSize;

if ( nDrawPageSize > m_nPageSize ){

m_nCurrentPageSize = m_nPageSize;

}

m_nCurrentEndPage = m_nCurrentStartPage + m_nCurrentPageSize;

//绘制页码

CString strPage;

if( m_nCurrentEndPage > m_nPageSize+1 ){

m_nCurrentEndPage = m_nPageSize+1;

}

int nItem = 0;

for (int nRow = m_nCurrentStartPage-1; nRow <= m_nCurrentEndPage; nRow++, nItem++){

int nLeft = nItem * DRAW_PAGE_WIDTH;

int nRight = nLeft + DRAW_PAGE_WIDTH;

CRect rcPage(nLeft, 0, nRight, nHeight);

CRect rcFillSolid(rcPage.left + 2, rcPage.top, rcPage.right - 2, rcPage.bottom);

if (m_nCurrentPage == (nRow) && m_nCurrentPage != m_nCurrentEndPage){

pDC->FillSolidRect(rcFillSolid, AdjustBrightness(m_clrPage, 120));

}

else{

pDC->FillSolidRect(rcFillSolid, AdjustBrightness(m_clrPage, 100));

}

if( nRow == m_nCurrentStartPage-1){

strPage.Format("%s", "<上一页");

}

else if( nRow == m_nCurrentEndPage ){

strPage.Format("%s", "下一页>");

}

else{

strPage.Format("%d", nRow );

}

pDC->DrawText(strPage, rcPage, DT_VCENTER | DT_SINGLELINE | DT_CENTER);

}

JumpPage(m_nJumpPageFlog);

}

void CStaticPage::JumpPage( __in unsigned int nJumpFlog )

{

switch(nJumpFlog )

{

case JUMP_NEXT_PAGE:

{

if( m_nCurrentEndPage-1 != m_nPageSize ){

m_nCurrentStartPage = m_nCurrentEndPage;

}

m_nCurrentPage = -1;

Invalidate();

break;

}

case JUMP_UP_PAGE:

{

int nStartPage = m_nCurrentStartPage - m_nCurrentPageSize;

if( nStartPage <= 0 ){

m_nCurrentStartPage = 1;

}

else{

m_nCurrentStartPage = nStartPage;

}

m_nCurrentPage = -1;

Invalidate();

break;

}

}

m_nJumpPageFlog = JUMP_SELECT_PAGE;

}

COLORREF CStaticPage::AdjustBrightness(CONST COLORREF clrSrcPixel, CONST INT nPercent)

{

// My formula for calculating the transpareny is as

// follows(for each single color):

//

// percent

// destPixel = sourcePixel *( ------- )

// 100

//

// This is not real alpha blending, as it only modifies the brightness,

// but not the color(a real alpha blending had to mix the source and

// destination pixels, e.g. mixing green and red makes yellow).

// For our nice "menu" shadows its good enough.

return RGB(min(255, (GetRValue(clrSrcPixel) * nPercent) / 100),

min(255, (GetGValue(clrSrcPixel) * nPercent) / 100),

min(255, (GetBValue(clrSrcPixel) * nPercent) / 100));

}

void CStaticPage::InitPage(__in unsigned int nSize, __in unsigned nCurPage, __in unsigned int nCurPageSize )

{

m_nPageSize = nSize;

m_nCurrentPage = nCurPage;

m_nCurrentPageSize = nCurPageSize;

//重绘需要把风格修改成SS_OWNERDRAW

DWORD dwStyle = ::GetWindowLong(GetSafeHwnd(), GWL_STYLE);

dwStyle |= SS_OWNERDRAW | SS_NOTIFY;

::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle);

}

void CStaticPage::SetPageSize(__in unsigned int nSize)

{

if ( nSize < 0){

return;

}

m_nPageSize = nSize;

m_nCurrentStartPage = 1;

m_nCurrentPage = 1;

Invalidate();

}

unsigned int CStaticPage::GetPageSize()

{

return m_nPageSize;

}

void CStaticPage::SetCurrentPage(__in unsigned int nPage)

{

m_nCurrentPage = nPage;

Invalidate();

}

unsigned int CStaticPage::GetCurrentPage()

{

return m_nCurrentPage;

}

void CStaticPage::SetCallBackPageOperation(__in PFNPAGEOPERATION pfnPageOperation, __in void *pUserData)

{

m_pfnPageOperation = pfnPageOperation;

m_pUserData = pUserData;

}

LRESULT CStaticPage::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)

{

// TODO: 在此添加专用代码和/或调用基类

switch (message)

{

case WM_LBUTTONDOWN://单击

{

CPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));

ClickPage(pt);

break;

}

case WM_SIZE:

{

Invalidate();

break;

}

default:

break;

}

return CStatic::WindowProc(message, wParam, lParam);

}

unsigned int CStaticPage::ClickPage(__in CPoint pt)

{

m_nJumpPageFlog = JUMP_SELECT_PAGE;

int nItem = floor((float)pt.x / (DRAW_PAGE_WIDTH));

if( nItem == 0 ){

m_nJumpPageFlog = JUMP_UP_PAGE;

TRACE("Click up page\n" );

}

else if( nItem == m_nCurrentPageSize+1 ){

m_nJumpPageFlog = JUMP_NEXT_PAGE;

TRACE("Click next page\n");

}

int nPage = m_nCurrentStartPage + nItem - 1;

//单击的页码可能会大于当前最大页码数 所以需要做以下判断

if (nPage > m_nPageSize || nPage < 0){

return m_nCurrentPage;

}

m_nCurrentPage = nPage;

TRACE("Click %d\n", m_nCurrentPage);

Invalidate();

if (m_nJumpPageFlog == JUMP_SELECT_PAGE){

if ( m_pfnPageOperation != NULL ){

m_pfnPageOperation(m_nCurrentPage, JUMP_SELECT_PAGE, m_pUserData);

}

}

return m_nCurrentPage;

}

void CStaticPage::SetPageColorref(COLORREF clr)

{

m_clrPage = clr;

Invalidate();

}

COLORREF CStaticPage::GetPageColorref()

{

return m_clrPage;

}

void CStaticPage::SetDefaultPageColorref()

{

m_clrPage = RGB(116, 184, 184);

Invalidate();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: