SelectDlg.cpp
上传用户:sztwq510
上传日期:2013-06-26
资源大小:4045k
文件大小:8k
源码类别:

酒店行业

开发平台:

Java

  1. // SelectDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyPos.h"
  5. #include "SelectDlg.h"
  6. #include "ClassDlg.h"
  7. #include "PosDlg.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CSelectDlg dialog
  15. extern CMyPosApp theApp;
  16. CSelectDlg::CSelectDlg(CWnd* pParent /*=NULL*/)
  17. : CDialog(CSelectDlg::IDD, pParent)
  18. {
  19. //{{AFX_DATA_INIT(CSelectDlg)
  20. // NOTE: the ClassWizard will add member initialization here
  21. //}}AFX_DATA_INIT
  22. }
  23. void CSelectDlg::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(CSelectDlg)
  27. DDX_Control(pDX, IDC_LIST_SELECT, m_oListsmateriel);
  28. DDX_Control(pDX, IDC_TREE_SCLASS, m_oTreesclass);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CSelectDlg, CDialog)
  32. //{{AFX_MSG_MAP(CSelectDlg)
  33. ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_SCLASS, OnSelchangedTreeSclass)
  34. ON_NOTIFY(NM_CLICK, IDC_LIST_SELECT, OnClickListSelect)
  35. ON_BN_CLICKED(IDC_BUTTON_SADD, OnButtonSadd)
  36. //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CSelectDlg message handlers
  40. BOOL CSelectDlg::OnInitDialog() 
  41. {
  42. CDialog::OnInitDialog();
  43. AddTree();
  44. //设置list控件的文字和背景颜色
  45. m_oListsmateriel.SetBkColor(RGB(255,255,255));
  46. m_oListsmateriel.SetTextBkColor(RGB(161,223, 212));
  47. //清空list控件的数据
  48. for(int delcolumn=100;delcolumn>=0;delcolumn--)
  49. m_oListsmateriel.DeleteColumn(delcolumn);
  50. //设置list对话框的列
  51. DWORD dwStyle;
  52. RECT rect;
  53. LV_COLUMN lvc;
  54. dwStyle = m_oListsmateriel.GetStyle();
  55. dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ;
  56. m_oListsmateriel.SetExtendedStyle(dwStyle);
  57. m_oListsmateriel.GetClientRect(&rect);
  58. lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH |LVCF_FMT;
  59. lvc.fmt=LVCFMT_LEFT;
  60. lvc.iSubItem = 0;
  61. lvc.pszText = _T("名称");
  62. lvc.cx = 140;
  63. m_oListsmateriel.InsertColumn(1,&lvc);
  64. lvc.iSubItem = 1;
  65. lvc.pszText = _T("价格");
  66. lvc.cx = 80;
  67. m_oListsmateriel.InsertColumn(2,&lvc);
  68. lvc.iSubItem = 2;
  69. lvc.pszText = _T("ID");
  70. lvc.cx = 0;
  71. m_oListsmateriel.InsertColumn(3,&lvc);
  72. return TRUE;  // return TRUE unless you set the focus to a control
  73.               // EXCEPTION: OCX Property Pages should return FALSE
  74. }
  75. void CSelectDlg::AddTree()
  76. {
  77. TVINSERTSTRUCT tvInsert;
  78. HTREEITEM hParent;
  79. _RecordsetPtr m_pTRecordset; //Must define it in function!!!Becourse this is a RECURSION function.
  80. CString sroot,sql;
  81. tvInsert.hParent = NULL;
  82. tvInsert.hInsertAfter = NULL;
  83. tvInsert.item.mask = TVIF_TEXT;
  84. // tvInsert.item.pszText = _T("root");
  85. hParent = m_oTreesclass.InsertItem(&tvInsert);//HTREEITEM of root.
  86. sql="SELECT * FROM MATERIELCLASS where ID=1";
  87. try
  88. {
  89. m_pTRecordset.CreateInstance("ADODB.Recordset");
  90. m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  91. if(!m_pTRecordset->adoEOF)
  92. {
  93. sroot=(LPTSTR)(_bstr_t)m_pTRecordset->GetCollect("NAME");//Insert an item into current parent.
  94. }
  95. m_pTRecordset->Close();
  96. }
  97. catch(_com_error e)///捕捉异常
  98. {
  99. CString errormessage;
  100. errormessage.Format("[商品资料] Addtree Class error:%s",e.ErrorMessage());
  101. AfxMessageBox(errormessage);
  102. }
  103. // tvInsert.item.pszText = _T("root");
  104. if(sroot!="")
  105. {
  106. // tvInsert.item.pszText =_T("");
  107. hParent = m_oTreesclass.InsertItem(&tvInsert);//HTREEITEM of root.
  108. AddSubTree("RootClass",hParent);//Here strChildTree just must not equal "root",can be anyother string.
  109. }
  110. // AddSubTree("1",hParent);//Here strChildTree just must not equal "root",can be anyother string.
  111. m_oTreesclass.Expand(hParent,TVE_EXPAND);
  112. }
  113. void CSelectDlg::AddSubTree(CString ParTree, HTREEITEM hPartItem)
  114. {
  115. HTREEITEM hCurrent;
  116. CString sql;
  117. CString curID;
  118. _RecordsetPtr m_pTRecordset; //Must define it in function!!!Becourse this is a RECURSION function.
  119. sql="SELECT * FROM MATERIELCLASS where PARENT='";
  120. sql=sql+ParTree+"'";//The 1st time Partree="1".
  121. try
  122. {
  123. m_pTRecordset.CreateInstance("ADODB.Recordset");
  124. m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  125. m_pTRecordset->MoveFirst();
  126. if(!m_pTRecordset->adoEOF)
  127. {
  128. while(!m_pTRecordset->adoEOF)
  129. {
  130. hCurrent = m_oTreesclass.InsertItem((LPCTSTR)(_bstr_t)(m_pTRecordset->GetCollect("NAME")), hPartItem, NULL);//Insert an item into current parent.
  131. curID=CClassDlg::VariantToCString(m_pTRecordset->GetCollect("ID"));
  132. if (CClassDlg::TreeSumRecordCount(curID)>0)
  133. AddSubTree(CClassDlg::VariantToCString(m_pTRecordset->GetCollect("ID")),hCurrent);//Recursion.
  134. if (!m_pTRecordset->adoEOF)
  135. m_pTRecordset->MoveNext();
  136. }
  137. }
  138. m_pTRecordset->Close();
  139. }
  140. catch(_com_error e)///捕捉异常
  141. {
  142. CString temp;
  143. temp.Format("[商品资料] Read sub tree error:%s",e.ErrorMessage());
  144. AfxMessageBox(temp);
  145. return;
  146. }
  147. }
  148. void CSelectDlg::OnSelchangedTreeSclass(NMHDR* pNMHDR, LRESULT* pResult) 
  149. {
  150. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  151. CString sql,sql1;
  152. long lclassid;
  153. _RecordsetPtr m_pRecordset; //Must define it in function!!!
  154. HTREEITEM hCurItem;
  155. hCurItem=m_oTreesclass.GetSelectedItem ();//Get Current item and its sub items.
  156. curtext=m_oTreesclass.GetItemText(hCurItem);//Read current item text into edit control.
  157. sql="SELECT * FROM MATERIELCLASS where NAME='"+curtext+"'";
  158. try
  159. {
  160. m_pRecordset.CreateInstance("ADODB.Recordset");
  161. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  162. if(!m_pRecordset->adoEOF)
  163. {
  164. lclassid=(long)(m_pRecordset->GetCollect("ID"));
  165. }
  166. m_pRecordset->Close();
  167. }
  168. catch(_com_error e)///捕捉异常
  169. {
  170. CString temp;
  171. temp.Format("获取分类名称出错:%s",e.ErrorMessage());
  172. AfxMessageBox(temp);
  173. return;
  174. }
  175. sclassid.Format("%d",lclassid);
  176. sql1="Select * from MATERIEL where CLASSID="+sclassid+" and SUPPLY=1";
  177. ReadtoList(sql1);
  178. UpdateData(false);
  179. *pResult = 0;
  180. }
  181. void CSelectDlg::ReadtoList(CString sql)
  182. {
  183. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  184. //删除所有list中的数据。
  185. m_oListsmateriel.DeleteAllItems();
  186. int numline=0;
  187. try
  188. {
  189. m_pRecordset.CreateInstance("ADODB.Recordset");
  190. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  191. LV_ITEM lvitem;
  192. lvitem.pszText="";
  193. lvitem.mask=LVIF_TEXT;
  194. lvitem.iSubItem=0;
  195. while(!m_pRecordset->adoEOF)
  196. {
  197. lvitem.iItem=numline;
  198. m_oListsmateriel.InsertItem(&lvitem);
  199. //读出数据写入到list中
  200. m_oListsmateriel.SetItemText(numline,0,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME"));
  201. float fprice=(float)m_pRecordset->GetCollect("PRICE");
  202. CString sprice;
  203. sprice.Format("%.2f",fprice);
  204. m_oListsmateriel.SetItemText(numline,1,sprice);
  205. long lmid=(long)m_pRecordset->GetCollect("ID");
  206. CString smid;
  207. smid.Format("%d",lmid);
  208. m_oListsmateriel.SetItemText(numline,2,smid);
  209. numline++;
  210. m_pRecordset->MoveNext();
  211. }
  212. m_pRecordset->Close();
  213. }
  214. catch(_com_error e)///捕捉异常
  215. {
  216. CString temp;
  217. temp.Format("列表读入商品出错:%s",e.ErrorMessage());
  218. AfxMessageBox(temp);
  219. return;
  220. }
  221. }
  222. void CSelectDlg::OnClickListSelect(NMHDR* pNMHDR, LRESULT* pResult) 
  223. {
  224. //得到当前选中的行
  225. POSITION pos = m_oListsmateriel.GetFirstSelectedItemPosition();
  226. //如果选中一行
  227. if(pos)
  228. {
  229. int nItem = m_oListsmateriel.GetNextSelectedItem(pos);
  230. theApp.smID=m_oListsmateriel.GetItemText(nItem,2);
  231. theApp.sprice=m_oListsmateriel.GetItemText(nItem,1);
  232. theApp.smname=m_oListsmateriel.GetItemText(nItem,0);
  233. }
  234. *pResult = 0;
  235. }
  236. void CSelectDlg::OnButtonSadd() 
  237. {
  238. //When the OK button is click down,send a message(WM_APPLY) to the CMyDBgridDlgDlg.
  239. if(theApp.smname=="")
  240. {
  241. AfxMessageBox("请选择商品!");
  242. return;
  243. }
  244. else
  245. LRESULT Res=::SendMessage(theApp.pWnd, WM_APPLY, 0, 0);
  246. CDialog::OnOK();
  247. }