1:新建一个基于对话框的mfc应用程序,添加个列表框,并对其进行报告顶端的设置,

添加个菜单子菜单为降序升序, 并为其添加响应函数

void CMyPopMenuDlg::OnMenuitemasc()
{
 // TODO: Add your command handler code here
 LoadBookInfo(false);
}

void CMyPopMenuDlg::OnMenuitemdesc()
{
 // TODO: Add your command handler code here
 LoadBookInfo(TRUE);
}

增加消息响应函数:(响应鼠标右键)

void CMyPopMenuDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 CMenu m_popmenu;
 m_popmenu.LoadMenu(IDR_POPMENU);
 CMenu* m_submenu = m_popmenu.GetSubMenu(0);
 
 m_submenu->TrackPopupMenu(TPM_LEFTBUTTON |TPM_LEFTALIGN ,point.x,point.y,this);
 
 m_popmenu.DestroyMenu();
 CDialog::OnRButtonDown(nFlags, point);
}

添加函数LoADBookInfo();

void CMyPopMenuDlg::LoadBookInfo(BOOL IsDesc)
{
 m_list.DeleteAllItems();
 CString sql;
 if(!IsDesc)
  sql="Select *from tb_bookinfo ORDER BY PRICE ASC";
 else
  sql="Select *from tb_bookinfo ORDER BY PRICE desc";
 CString fieldvalue;
 m_pRecord=m_pCon->Execute((_bstr_t)sql,NULL,adCmdText);
 int i=0;
 while( ! m_pRecord->ADOEOF && ! m_pRecord->BOF)
 {
  m_list.InsertItem(100,"");
  fieldvalue = (TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("bookname")->Value;

 m_list.SetItemText(i,0,fieldvalue);
  fieldvalue = (TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("barcode")->Value;
  m_list.SetItemText(i,1,fieldvalue);
  
  fieldvalue =(TCHAR*) (_bstr_t)m_pRecord->GetFields()->GetItem("author")->Value;
  m_list.SetItemText(i,2,fieldvalue);
  
  fieldvalue = (TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("bookconcern")->Value;
  m_list.SetItemText(i,3,fieldvalue);
  
  fieldvalue =(TCHAR*) (_bstr_t)m_pRecord->GetFields()->GetItem("price")->Value;
  m_list.SetItemText(i,4,fieldvalue);
  i+=1;
  m_pRecord->MoveNext();
 }
 
}

在OnInitDialog()中进行相应的初始化如列表框的初始化,关联变量

//添加外部变量:

extern _ConnectionPtr m_pCon;
extern _RecordsetPtr m_pRecord;

 m_list.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FLATSB|LVS_EX_FULLROWSELECT);
 m_list.InsertColumn(0,"书籍名称",LVCFMT_LEFT,150);
 m_list.InsertColumn(1,"条形码",LVCFMT_LEFT,120);
 m_list.InsertColumn(2,"作者",LVCFMT_LEFT,80);
 m_list.InsertColumn(3,"出版社",LVCFMT_LEFT,80);
 m_list.InsertColumn(4,"价格",LVCFMT_LEFT,80);
 在APP 中添加智能指针:

_ConnectionPtr m_pCon;
_RecordsetPtr m_pRecord;

并在InitInstance进行必要的初始化:

AfxOleInit();
 m_pCon.CreateInstance(__uuidof(Connection));
 m_pRecord.CreateInstance(__uuidof(Recordset));
 
 m_pCon->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; \
  Data Source=./Menu.mdb;Persist Security Info=False";
 try
 {
  m_pCon->Open("","","",-1);
 }
 catch(...)
 {
  AfxMessageBox("数据库连接错误",0,0);
  exit;
 }

 

 

 

 

 

 

posted on 2011-07-29 09:18  role  阅读(446)  评论(0编辑  收藏  举报