自己封装的翻页控件

概要:

CTurnPageCtrl

|---CTurnPageStatic

|---CTurnPageCombo


代码:

TurnPageCtr.h

// TurnPageCtrl.h: interface for the CTurnPageCtrl class.
//
//

#if !defined(AFX_TURNPAGECTRL_H__7092386C_B27F_4050_A11C_864E2BE02E8D__INCLUDED_)
#define AFX_TURNPAGECTRL_H__7092386C_B27F_4050_A11C_864E2BE02E8D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "TurnPageStatic.h"
#include "TurnPageCombo.h"

class CTurnPageCtrl  
{
public:
	CTurnPageCtrl();
	virtual ~CTurnPageCtrl();
public:
	CTurnPageStatic m_staticBegin;
	CTurnPageStatic m_staticUp;
	CTurnPageStatic m_staticDown;
	CTurnPageStatic m_staticEnd;
	CTurnPageCombo	m_combPage;

	CWnd *m_pParentWnd;
	int m_iIndex;	// 本控件与父窗口的通信约定:用于同一父窗口中使用了多个本控件的时候
					// 比如某对话框用了两组分页按钮,第一组的m_iIndex为0,第二组的m_iIndex
					// 为1,那么当给对话框发消息的时候,携带此m_iIndex,则父窗口就可以分辨出
					// 是哪组分页按钮发来的消息
	BOOL m_bCreated;// 本控件已创建, 防止重复创建
	void CreateChildCtrl(CWnd *pParentWnd, int iIndex =  0);
	void ShowChildCtrl(BOOL bShow);
};

#endif // !defined(AFX_TURNPAGECTRL_H__7092386C_B27F_4050_A11C_864E2BE02E8D__INCLUDED_)

TurnPageCtrl.cpp

// TurnPageCtrl.cpp: implementation of the CTurnPageCtrl class.
//
//

#include "stdafx.h"
#include "hrinetnsm_con.h"
#include "TurnPageCtrl.h"

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

//
// Construction/Destruction
//

CTurnPageCtrl::CTurnPageCtrl()
{
	m_bCreated = FALSE;
}

CTurnPageCtrl::~CTurnPageCtrl()
{

}

/** 
 * 函数名称: CreateChildCtrl
 *
 * 函数功能: 提供给外界,创建本控件
 *
 * 函数参数: pParentWnd 父窗口
 *			 iIndex 本控件为父窗口的第几组分页按钮
 */
void CTurnPageCtrl::CreateChildCtrl(CWnd *pParentWnd, int iIndex)
{
	if (m_bCreated)
		return;

	m_pParentWnd = pParentWnd;
	m_iIndex = iIndex;

	m_staticBegin.m_pParentWnd = m_staticUp.m_pParentWnd = m_staticDown.m_pParentWnd = m_staticEnd.m_pParentWnd = m_combPage.m_pParentWnd = m_pParentWnd;
	
	m_staticBegin.m_iIndex = m_staticUp.m_iIndex = m_staticDown.m_iIndex = m_staticEnd.m_iIndex = iIndex;

	m_staticBegin.m_pageType = PageBegin;
	m_staticUp.m_pageType = PageUp;
	m_staticDown.m_pageType = PageDown;
	m_staticEnd.m_pageType = PageEnd;
	
	m_staticBegin.Create(_T("首页"), WS_CHILD|WS_VISIBLE|SS_NOTIFY, CRect(0, 0, 30, 20), m_pParentWnd);
	m_staticUp.Create(_T("上一页"), WS_CHILD|WS_VISIBLE|SS_NOTIFY, CRect(40, 0, 90, 20), m_pParentWnd);
	m_staticDown.Create(_T("下一页"), WS_CHILD|WS_VISIBLE|SS_NOTIFY, CRect(100, 0, 150, 20), m_pParentWnd);
	m_staticEnd.Create(_T("末页"), WS_CHILD|WS_VISIBLE|SS_NOTIFY, CRect(160, 0, 190, 20), m_pParentWnd);

	m_combPage.Create(WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWN|CBS_AUTOHSCROLL, CRect(200, 0, 240, 100), m_pParentWnd, IDC_COMBO_PAGE);
	
	m_combPage.m_iIndex = iIndex;

	m_bCreated = TRUE;
}

/** 
 * 函数名称: ShowChildCtrl
 *
 * 函数功能: 显示或隐藏详细信息的控件
 *
 * 函数参数: bShow 
 *				TRUE 显示
 *				FALSE 隐藏
 */
void CTurnPageCtrl::ShowChildCtrl(BOOL bShow)
{
	if (m_bCreated)
	{
		m_staticBegin.ShowWindow(bShow);
		m_staticUp.ShowWindow(bShow);
		m_staticDown.ShowWindow(bShow);
		m_staticEnd.ShowWindow(bShow);

		m_combPage.ShowWindow(bShow);
	}
}

TurnPageStatic.h

#if !defined(AFX_TURNPAGESTATIC_H__4A0C0EBB_8E31_490B_81DE_D235B6446520__INCLUDED_)
#define AFX_TURNPAGESTATIC_H__4A0C0EBB_8E31_490B_81DE_D235B6446520__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// TurnPageStatic.h : header file
//
#include "Static/HandStatic.h"

typedef enum _EM_PAGE 
{
	PageBegin = 0,
	PageUp = 1,
	PageDown = 2,
	PageEnd = 3

}EM_PAGE;

#define WM_TURNPAGE_CLICK WM_USER + 101
/
// CTurnPageStatic window

class CTurnPageStatic : public CStatic
{
// Construction
public:
	CTurnPageStatic();

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CTurnPageStatic)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CTurnPageStatic();

	// Generated message map functions
protected:
	//{{AFX_MSG(CTurnPageStatic)
	afx_msg void OnClicked();
	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
	afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
public:
	EM_PAGE m_pageType;
	CWnd *m_pParentWnd;
	int m_iIndex;		// 本控件与父窗口的通信约定:用于同一父窗口中使用了多个本控件的时候
						// 比如某对话框用了两组分页按钮,第一组的m_iIndex为0,第二组的m_iIndex
						// 为1,那么当给对话框发消息的时候,携带此m_iIndex,则父窗口就可以分辨出
						// 是哪组分页按钮发来的消息

private:
	CFont m_font;
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TURNPAGESTATIC_H__4A0C0EBB_8E31_490B_81DE_D235B6446520__INCLUDED_)

TurnPageStatic.cpp

// TurnPageStatic.cpp : implementation file
//

#include "stdafx.h"
#include "hrinetnsm_con.h"
#include "TurnPageStatic.h"

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

/
// CTurnPageStatic

CTurnPageStatic::CTurnPageStatic()
{

}

CTurnPageStatic::~CTurnPageStatic()
{
}


BEGIN_MESSAGE_MAP(CTurnPageStatic, CStatic)
	//{{AFX_MSG_MAP(CTurnPageStatic)
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_SETCURSOR()
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CTurnPageStatic message handlers

void CTurnPageStatic::OnClicked() 
{
	// TODO: Add your control notification handler code here
	if (m_pParentWnd->GetSafeHwnd() != NULL)
	{
		m_pParentWnd->SendMessage(WM_TURNPAGE_CLICK, (WPARAM)m_pageType, m_iIndex);
	}
}


BOOL CTurnPageStatic::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	POINT   point;
	GetCursorPos(&point); 
	
	if (GetSafeHwnd() != NULL)
	{
		CRect rect;
		
		GetWindowRect(&rect);
		
		if (rect.PtInRect(point))
		{
			::SetCursor(::LoadCursor(NULL, IDC_HAND));
			
			return true;
		}
	}
	
	return CStatic::OnSetCursor(pWnd, nHitTest, message);
}

HBRUSH CTurnPageStatic::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	// TODO: Change any attributes of the DC here
	if (!(HFONT)m_font) {
		// first time init: create font
		LOGFONT lf;
		::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(lf),&lf);
		lf.lfUnderline = TRUE;//具有下划线的文字
		m_font.CreateFontIndirect(&lf);
	}
	
	// use underline font and visited/unvisited colors
	pDC->SelectObject(&m_font);
	
	pDC->SetBkMode(TRANSPARENT);
	
	// return hollow brush to preserve parent background color

	// TODO: Return a non-NULL brush if the parent's handler should not be called
	return NULL;
}

TurnPageCombo.h

#if !defined(AFX_TURNPAGECOMBO_H__0D622184_382A_4A32_AB4A_DC50C878ACAB__INCLUDED_)
#define AFX_TURNPAGECOMBO_H__0D622184_382A_4A32_AB4A_DC50C878ACAB__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// TurnPageCombo.h : header file
//
#define WM_TURNPAGE_COMBO WM_USER + 102

/
// CTurnPageCombo window

class CTurnPageCombo : public CComboBox
{
// Construction
public:
	CTurnPageCombo();

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CTurnPageCombo)
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CTurnPageCombo();

	// Generated message map functions
protected:
	//{{AFX_MSG(CTurnPageCombo)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnSelchange();
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
private:
	CFont m_font;
public:
	CWnd *m_pParentWnd;
	int m_iIndex;		// 本控件与父窗口的通信约定:用于同一父窗口中使用了多个本控件的时候
						// 比如某对话框用了两组分页按钮,第一组的m_iIndex为0,第二组的m_iIndex
						// 为1,那么当给对话框发消息的时候,携带此m_iIndex,则父窗口就可以分辨出
						// 是哪组分页按钮发来的消息
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TURNPAGECOMBO_H__0D622184_382A_4A32_AB4A_DC50C878ACAB__INCLUDED_)

TurnPageCombo.cpp

// TurnPageCombo.cpp : implementation file
//

#include "stdafx.h"
#include "hrinetnsm_con.h"
#include "TurnPageCombo.h"

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

/
// CTurnPageCombo

CTurnPageCombo::CTurnPageCombo()
{
}

CTurnPageCombo::~CTurnPageCombo()
{
}


BEGIN_MESSAGE_MAP(CTurnPageCombo, CComboBox)
	//{{AFX_MSG_MAP(CTurnPageCombo)
	ON_WM_CREATE()
	ON_CONTROL_REFLECT(CBN_SELCHANGE, OnSelchange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CTurnPageCombo message handlers


int CTurnPageCombo::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CComboBox::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	LOGFONT lf;
	::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(lf),&lf);
	m_font.CreateFontIndirect(&lf);

	SetFont(&m_font);

	return 0;
}

void CTurnPageCombo::OnSelchange() 
{
	// TODO: Add your control notification handler code here
	int iIndex = GetCurSel();

	if (iIndex != CB_ERR)
	{
		if (m_pParentWnd->GetSafeHwnd() != NULL)
		{
			m_pParentWnd->SendMessage(WM_TURNPAGE_COMBO, iIndex + 1, m_iIndex);// +1 页码从1开始
		}
	}
}



DataGridView分页控件封装是为了实现对大量数据的分页显示和管理。当数据量较大时,一次性加载所有数据到DataGridView中会占用大量内存,影响程序的运行速度和用户体验。通过分页控件封装,可以将数据进行分页加载,提高程序的运行效率。 封装一个DataGridView分页控件,需要考虑以下几个方面: 1. 数据源管理:封装控件需要支持设置数据源,可以从数据库、列表等来源获取数据,并进行分页处理。同时,还需要支持数据的添加、删除、修改等操作。 2. 分页显示:封装控件需要实现对数据的分页显示,每页显示指定数量的数据,并提供上一页、下一页、首页、末页等操作按钮,方便用户切换页数。 3. 数据排序:封装控件应该支持对数据进行排序,用户可以根据某一列的值进行升序或降序排列。 4. 数据筛选:封装控件应该支持对数据进行筛选,用户可以根据某一列的值进行筛选显示符合条件的数据。 5. 显示页面信息:封装控件需要在界面上显示当前页数、总页数、总记录数等信息,让用户清楚了解当前显示的数据范围。 6. 事件处理:封装控件需要提供相应的事件,让用户可以对操作进行自定义处理,如点击某一行数据触发事件等。 通过以上功能的封装,可以方便开发人员在各种项目中使用DataGridView分页控件,实现数据的分页显示和管理,提高程序的可用性和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值