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

📄 dtest3dlg.cpp

📁 VC6.0++ & MySQL demo program developing
💻 CPP
字号:
// DTest3Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "DTest3.h"
#include "DTest3Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CDTest3Dlg dialog

CDTest3Dlg::CDTest3Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDTest3Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDTest3Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_nIndex=-1;
	m_strStuID=_T("");
}

void CDTest3Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDTest3Dlg)
	DDX_Control(pDX, IDC_DATETIMEPICKER1, m_DataTime);
	DDX_Control(pDX, IDC_COMBO1, m_Combo);
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDTest3Dlg, CDialog)
	//{{AFX_MSG_MAP(CDTest3Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDTest3Dlg message handlers

BOOL CDTest3Dlg::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
	
	m_list.InsertColumn(0,"学号",LVCFMT_CENTER,40);
	m_list.InsertColumn(1,"姓名",LVCFMT_CENTER,60);
	m_list.InsertColumn(2,"性别",LVCFMT_CENTER,40);
	m_list.InsertColumn(3,"生日",LVCFMT_CENTER,100);
	m_list.InsertColumn(4,"联系",LVCFMT_CENTER,130);
	m_list.InsertColumn(5,"备注",LVCFMT_CENTER,130);
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES);

	m_Combo.InsertString(0,"男");
	m_Combo.InsertString(1,"女");
	m_Combo.SetCurSel(0);
	// TODO: Add extra initialization here
	mysql_init (&mysql);
	if(!mysql_real_connect(&mysql,"localhost","root",NULL,"mydb",3306,NULL,0))
	{ 
		AfxMessageBox("Can't connent to database"); // 输出错误信息
		return FALSE;
	}
	
	RefreshList();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDTest3Dlg::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 CDTest3Dlg::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 CDTest3Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDTest3Dlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	CString strName,strSex,strBirth,strTel,strMemo;

	GetDlgItem(IDC_EDIT_NAME)->GetWindowText(strName);

	CComboBox* pCombo=(CComboBox*)GetDlgItem(IDC_COMBO1);
	int nIndex=pCombo->GetCurSel();
	if(nIndex!=-1)
	{
		pCombo->GetLBText(nIndex,strSex);
	}

	COleDateTime t;
	m_DataTime.GetTime(t);
	strBirth=t.Format("%Y-%m-%d");
	
	GetDlgItem(IDC_EDIT_TEL)->GetWindowText(strTel);

	GetDlgItem(IDC_EDIT_MEMO)->GetWindowText(strMemo);

	CString strSQL;
	strSQL.Format("insert into class892(Name,sex,Birthday,Addrr,Memo) values(\'%s\',\'%s\',\'%s\',\'%s\',\'%s\')",strName,strSex,strBirth,strTel,strMemo);
	mysql_real_query(&mysql,(char*)(LPCTSTR)strSQL,(UINT)strSQL.GetLength());
	
	RefreshList();
}

void CDTest3Dlg::OnUpdate() 
{
	// TODO: Add your control notification handler code here

	CString strName,strSex,strBirth,strTel,strMemo;
	
	GetDlgItem(IDC_EDIT_NAME)->GetWindowText(strName);
	
	CComboBox* pCombo=(CComboBox*)GetDlgItem(IDC_COMBO1);
	int nIndex=pCombo->GetCurSel();
	if(nIndex!=-1)
	{
		pCombo->GetLBText(nIndex,strSex);
	}
	
	COleDateTime t;
	m_DataTime.GetTime(t);
	strBirth=t.Format("%Y-%m-%d");
	
	GetDlgItem(IDC_EDIT_TEL)->GetWindowText(strTel);
	
	GetDlgItem(IDC_EDIT_MEMO)->GetWindowText(strMemo);
	
	CString strSQL;
	strSQL.Format("update class892 set Name=\'%s\',sex=\'%s\',Birthday=\'%s\',Addrr=\'%s\',Memo=\'%s\' where StudentID=%s",strName,strSex,strBirth,strTel,strMemo,m_strStuID);
	mysql_real_query(&mysql,(char*)(LPCTSTR)strSQL,(UINT)strSQL.GetLength());
	
	RefreshList();
	
}

void CDTest3Dlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	CString strSQL;
	strSQL.Format("delete from class892 where StudentID=%s",m_strStuID);
	mysql_real_query(&mysql,(char*)(LPCTSTR)strSQL,(UINT)strSQL.GetLength());
	
	for(POSITION pos=m_list.GetFirstSelectedItemPosition();pos!=NULL;)
	{
		int nIndex=m_list.GetNextSelectedItem(pos);
		AfxMessageBox(m_list.GetItemText(nIndex,0));
	}
	RefreshList();
	
}

void CDTest3Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	POSITION pos=m_list.GetFirstSelectedItemPosition();
	if(pos!=NULL)
	{
		CString strName,strSex,strBirth,strAdd,strMemo;

		m_nIndex=m_list.GetNextSelectedItem(pos);

		m_strStuID=m_list.GetItemText(m_nIndex,0);

		strName=m_list.GetItemText(m_nIndex,1);
		GetDlgItem(IDC_EDIT_NAME)->SetWindowText(strName);

		strSex=m_list.GetItemText(m_nIndex,2);
		int nSel=m_Combo.SelectString(0,strSex);
		if(nSel!=-1)
		{
			m_Combo.SetCurSel(nSel);
		}

		strBirth=m_list.GetItemText(m_nIndex,3);
		COleDateTime t(atoi(strBirth.Left(4)),atoi(strBirth.Mid(5,2)),atoi(strBirth.Right(2)),0,0,0);
		if(t.GetStatus()==COleDateTime::valid)
		{
			m_DataTime.SetTime(t);
		}
		else
		{
			m_DataTime.SetTime();
		}

		strAdd=m_list.GetItemText(m_nIndex,4);
		GetDlgItem(IDC_EDIT_TEL)->SetWindowText(strAdd);

		strMemo=m_list.GetItemText(m_nIndex,5);
		GetDlgItem(IDC_EDIT_MEMO)->SetWindowText(strMemo);
		
	}
	*pResult = 0;
}

void CDTest3Dlg::RefreshList()
{
	m_list.DeleteAllItems();
	
	char *query;
	query="select * from Class892";
	mysql_real_query(&mysql,query,(UINT)strlen(query));
	
	CString str;
	MYSQL_RES *res;
	MYSQL_ROW row;
	int i=0;
	res=mysql_use_result(&mysql);
//	int nCount=(int)mysql_num_rows(res);
	while(row=mysql_fetch_row(res))
//	for(int i=0;r<nCount;r++)
	{
//		row=mysql_fetch_row(res);
//		if(row<0) break;
		str.Format("%s",row[0]);
		m_list.InsertItem(i,str);
		
		str.Format("%s",row[1]);
		m_list.SetItemText(i,1,str);
		
		str.Format("%s",row[2]);
		m_list.SetItemText(i,2,str);
		
		str.Format("%s",row[3]);
		m_list.SetItemText(i,3,str);
		
		str.Format("%s",row[4]);
		m_list.SetItemText(i,4,str);

		str.Format("%s",row[5]);
		m_list.SetItemText(i,5,str);
		
		i++;
	}
	mysql_free_result(res);
}

void CDTest3Dlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	mysql_close(&mysql);

}

⌨️ 快捷键说明

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