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

📄 pdtnavigation.cpp

📁 使用vc进行数据库编程的好例子
💻 CPP
字号:
// PdtNavigation.cpp : implementation file
//

#include "stdafx.h"
#include "ManageSystem.h"
#include "PdtNavigation.h"

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

#define RESULT_SHOW	0
#define RESULT_HIDE	1
/////////////////////////////////////////////////////////////////////////////
// CPdtNavigation dialog

IMPLEMENT_DYNCREATE(CPdtNavigation, CDialog)

CPdtNavigation::CPdtNavigation(CWnd* pParent /*=NULL*/)
	: CDialog(CPdtNavigation::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPdtNavigation)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CPdtNavigation::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPdtNavigation)
	DDX_Control(pDX, IDC_TRE_PDCT_CLASS, m_Tre_Pdct_Class);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPdtNavigation, CDialog)
	//{{AFX_MSG_MAP(CPdtNavigation)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TRE_PDCT_CLASS, OnSelchangedTrePdctClass)
	ON_WM_SIZE()
	ON_NOTIFY(NM_CLICK, IDC_TRE_PDCT_CLASS, OnClickTrePdctClass)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_UPDETASELDATA,UpdateSelData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPdtNavigation message handlers

void CPdtNavigation::OnOK() 
{
	// TODO: Add extra validation here
}

void CPdtNavigation::OnCancel() 
{
	// TODO: Add extra cleanup here
}

void CPdtNavigation::OnSelchangedTrePdctClass(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(bIsFirstSel==true)
	{
		*pResult = 0;
		return;
	}
	
	if(g_isPLocked==false)
	{
		if(g_pSet->IsOpen())
			g_pSet->Close();
	}
	else
	{
		*pResult=0;
		return;
	}

	CString sqlComm,classname;
	int classid=-1;
	m_hTreeCurrent=m_Tre_Pdct_Class.GetSelectedItem();
	classname=m_Tre_Pdct_Class.GetItemText(m_hTreeCurrent);

	try
	{
		if(classname=="产品信息库")
		{
			sqlComm="SELECT * FROM productinfo";
		}
		else
		{
			PRODUCTCLASS* ProductClass=g_ProductClass->next;
		
			while(ProductClass!=NULL)
			{
				if(ProductClass->classname==classname)
					break;
				else
					ProductClass=ProductClass->next;
			}
			classid=ProductClass->classid;
		
			sqlComm.Format("SELECT * FROM productinfo WHERE class_id=%d",classid);
		}

		g_pSet->Open(CRecordset::snapshot,sqlComm);
		g_isPLocked=true;
		::SendMessage(::AfxGetMainWnd()->m_hWnd,WM_SHOW_PRO_RESULT_SIMP,0,RESULT_SHOW);

	}
	catch(CDBException* pe)
	{
		AfxMessageBox("产品信息数据加载错误:\n"+pe->m_strError);
		if(g_pSet)
		{
			if(g_pSet->IsOpen())
				g_pSet->Close();
			delete g_pSet;
		}
		pe->Delete();
	}
	
	*pResult = 0;
}

void CPdtNavigation::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	OnDialogSize();
}

BOOL CPdtNavigation::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	bIsFirstSel=true;

	g_ProductClass=new PRODUCTCLASS;
	g_ProductClass->classid=-1;
	g_ProductClass->classname="";
	g_ProductClass->next=NULL;

	AddItems();

	m_Tre_Pdct_Class.Expand(products,TVE_EXPAND);
/*	
	CRect rect;
	GetClientRect(&rect);
	if(m_Tre_Pdct_Class.GetSafeHwnd())
		m_Tre_Pdct_Class.MoveWindow(&rect);
*/
	OnDialogSize();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPdtNavigation::OnDialogSize()
{
	if(m_Tre_Pdct_Class.GetSafeHwnd())
	{
		CRect rect;
		GetClientRect(&rect);
		m_Tre_Pdct_Class.MoveWindow(&rect);
	}

}


bool CPdtNavigation::AddItems()
{
	products = m_Tre_Pdct_Class.InsertItem(_T("产品信息库"));

	bool b_result=true;
	PRODUCTCLASS* temp=g_ProductClass;
	PRODUCTCLASS* newClass;
	CString className;	
	try
	{
		m_pcSet=new CProductClassSet();
		m_pcSet->Open(CRecordset::snapshot,"SELECT * FROM class");

		while(!m_pcSet->IsEOF())
		{
			newClass=new PRODUCTCLASS;
			className=m_pcSet->m_class_name;
			className.TrimRight();
			
			newClass->classid=m_pcSet->m_class_id;
			newClass->classname=className;
			newClass->next=temp->next;
			temp->next=newClass;
			
			m_Tre_Pdct_Class.InsertItem(className,products);
			m_pcSet->MoveNext();
		}
	}
	catch(CDBException* pe)
	{
		AfxMessageBox("产品信息数据加载错误:\n"+pe->m_strError);
		pe->Delete();
		b_result=false;
	}
	
	if(m_pcSet)
	{
		if(m_pcSet->IsOpen())
			m_pcSet->Close();
		delete m_pcSet;
	}

	return b_result;
}

void CPdtNavigation::OnClickTrePdctClass(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	bIsFirstSel=false;
	
	OnSelchangedTrePdctClass(pNMHDR,pResult);

	*pResult = 0;
}

void CPdtNavigation::UpdateSelData()
{
	if(g_isPLocked==false)
	{
		if(g_pSet->IsOpen())
			g_pSet->Close();
	}
	else
	{
		AfxMessageBox("locked,try again");
		return;
	}

	CString sqlComm,classname;
	int classid=-1;
	m_hTreeCurrent=m_Tre_Pdct_Class.GetSelectedItem();
	classname=m_Tre_Pdct_Class.GetItemText(m_hTreeCurrent);

	try
	{
		if(classname=="产品信息库")
		{
			sqlComm="SELECT * FROM productinfo";
		}
		else
		{
			PRODUCTCLASS* ProductClass=g_ProductClass->next;
		
			while(ProductClass!=NULL)
			{
				if(ProductClass->classname==classname)
					break;
				else
					ProductClass=ProductClass->next;
			}
			classid=ProductClass->classid;
		
			sqlComm.Format("SELECT * FROM productinfo WHERE class_id=%d",classid);
		}

		g_pSet->Open(CRecordset::snapshot,sqlComm);
		g_isPLocked=true;
		::SendMessage(::AfxGetMainWnd()->m_hWnd,WM_SHOW_PRO_RESULT_SIMP,0,RESULT_HIDE);

	}
	catch(CDBException* pe)
	{
		AfxMessageBox("产品信息数据加载错误:\n"+pe->m_strError);
		if(g_pSet)
		{
			if(g_pSet->IsOpen())
				g_pSet->Close();
			delete g_pSet;
		}
		pe->Delete();
	}
}

void CPdtNavigation::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
}

⌨️ 快捷键说明

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