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

📄 testgriddlg.cpp

📁 我从网上下载的一个关于DataGrid的程序。
💻 CPP
字号:
// TestGridDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestGrid.h"
#include "TestGridDlg.h"

#include "Columns.h"
#include "Column.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestGridDlg dialog

CTestGridDlg::CTestGridDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestGridDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestGridDlg)
	m_bCk1 = FALSE;
	m_bCk2 = FALSE;
	m_bCk3 = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestGridDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestGridDlg)
//	DDX_Control(pDX, IDC_EMAIL, m_staticEmail);
	DDX_Control(pDX, IDC_DATAGRID1, m_ctrlDG);
	DDX_Check(pDX, IDC_CHECK1, m_bCk1);
	DDX_Check(pDX, IDC_CHECK2, m_bCk2);
	DDX_Check(pDX, IDC_CHECK3, m_bCk3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestGridDlg, CDialog)
	//{{AFX_MSG_MAP(CTestGridDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
	ON_BN_CLICKED(IDC_CHECK2, OnCheck2)
	ON_BN_CLICKED(IDC_CHECK3, OnCheck3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestGridDlg message handlers

BOOL CTestGridDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
//	m_staticEmail.SetURL(_T("ChinaITBoy@163.com"));

	CString strConnection;
	strConnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;")_T("Data Source=note.mdb"));
	m_DBCn.Open((LPCTSTR)strConnection);	//打开程序数据库
	m_Rs.SetDatabase(&m_DBCn);
	m_Rs.Open(_T("select * from test;"));

	m_ctrlDG.SetCaption(_T("单击列头进行排序"));
	m_ctrlDG.SetRefDataSource((LPUNKNOWN)m_Rs.GetRecordset());
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestGridDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestGridDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestGridDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTestGridDlg::OnCheck1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CRect rc;
	m_ctrlDG.GetClientRect(rc);
	CColumns cols = m_ctrlDG.GetColumns();
	int iIndex = (int)cols.GetCount(); //取得列数
	float fWidth = (float)rc.Width()/iIndex-5;//求每列宽
	VARIANT vt;
	vt.vt = VT_INT; 
	if(m_bCk1)
	{
		for(int i=0;i<iIndex;i++)
		{//设置每列宽
			vt.intVal = i;
			CColumn  col  = cols.GetItem(vt);
			col.SetWidth(fWidth);//指定宽
		}
	}
	else
	{
		for(int i=0;i<iIndex;i++)
		{//设置每列宽
			vt.intVal = i;
			CColumn  col  = cols.GetItem(vt);
			col.SetWidth(140);//默认宽
		}
	}
}

void CTestGridDlg::OnCheck2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CColumns cols = m_ctrlDG.GetColumns();
	VARIANT vt;
	vt.vt = VT_INT;
	if(m_bCk2)
	{
		for(int i=4;i<7;i++)
		{
			vt.intVal = i;//包含小数的列
			CColumn  col  = cols.GetItem(vt);
			col.SetNumberFormat(_T("0.0%"));//百分数格式
		}
	}	
	else
	{
		for(int i=4;i<7;i++)
		{
			vt.intVal = i;//包含小数的列
			CColumn  col  = cols.GetItem(vt);
			col.SetNumberFormat(_T(""));//默认数据格式
		}	
	}
}

void CTestGridDlg::OnCheck3() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CColumns cols = m_ctrlDG.GetColumns();
	VARIANT vt;
	vt.vt = VT_INT;
	vt.intVal = 2;
	CColumn  col  = cols.GetItem(vt);
	col.SetWrapText(m_bCk3);	
}

BEGIN_EVENTSINK_MAP(CTestGridDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CTestGridDlg)
	ON_EVENT(CTestGridDlg, IDC_DATAGRID1, 216 /* HeadClick */, OnHeadClickDatagrid1, VTS_I2)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

static int ClkNum = 0; //单击次数
void CTestGridDlg::OnHeadClickDatagrid1(short ColIndex) 
{
	// TODO: Add your control notification handler code here
	ClkNum++;
	CString strSQL;
	CADOFieldInfo fi;
	m_Rs.GetFieldInfo(ColIndex,&fi);
	strSQL.Format(_T("select * from test order by %s %s;"),
		fi.m_strName,(ClkNum%2==0)?_T("asc"):_T("desc"));
	m_Rs.Open((LPCTSTR)strSQL);
	m_ctrlDG.SetRefDataSource((LPUNKNOWN)m_Rs.GetRecordset());
	OnCheck1();
	OnCheck2();
	OnCheck3();
}

⌨️ 快捷键说明

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