AddressAdminDlg.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:10k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // AddressAdminDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "AddressAdmin.h"
  5. #include "AddressAdminDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CAddressAdminDlg dialog
  50. CAddressAdminDlg::CAddressAdminDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CAddressAdminDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CAddressAdminDlg)
  54. m_age = 0;
  55. iSize=0;
  56. m_name = _T("");
  57. m_address = _T("");
  58. m_sex = _T("");
  59. //}}AFX_DATA_INIT
  60. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  61. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  62. this->pBuffer=NULL;
  63. p_CurrentImag=NULL;
  64. }
  65. void CAddressAdminDlg::DoDataExchange(CDataExchange* pDX)
  66. {
  67. CDialog::DoDataExchange(pDX);
  68. //{{AFX_DATA_MAP(CAddressAdminDlg)
  69. DDX_Text(pDX, IDC_AGE, m_age);
  70. DDX_Text(pDX, IDC_Name, m_name);
  71. DDX_Text(pDX, IDC_Address, m_address);
  72. DDX_Text(pDX, IDC_SEX, m_sex);
  73. //}}AFX_DATA_MAP
  74. }
  75. BEGIN_MESSAGE_MAP(CAddressAdminDlg, CDialog)
  76. //{{AFX_MSG_MAP(CAddressAdminDlg)
  77. ON_WM_SYSCOMMAND()
  78. ON_WM_PAINT()
  79. ON_WM_QUERYDRAGICON()
  80. ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
  81. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  82. ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
  83. ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  84. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  85. ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
  86. ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
  87. //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CAddressAdminDlg message handlers
  91. BOOL CAddressAdminDlg::OnInitDialog()
  92. {
  93. CDialog::OnInitDialog();
  94. // Add "About..." menu item to system menu.
  95. // IDM_ABOUTBOX must be in the system command range.
  96. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  97. ASSERT(IDM_ABOUTBOX < 0xF000);
  98. CMenu* pSysMenu = GetSystemMenu(FALSE);
  99. if (pSysMenu != NULL)
  100. {
  101. CString strAboutMenu;
  102. strAboutMenu.LoadString(IDS_ABOUTBOX);
  103. if (!strAboutMenu.IsEmpty())
  104. {
  105. pSysMenu->AppendMenu(MF_SEPARATOR);
  106. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  107. }
  108. }
  109. // Set the icon for this dialog.  The framework does this automatically
  110. //  when the application's main window is not a dialog
  111. SetIcon(m_hIcon, TRUE); // Set big icon
  112. SetIcon(m_hIcon, FALSE); // Set small icon
  113. // TODO: Add extra initialization here
  114. ::CoInitialize(NULL);
  115. conn.CreateInstance(__uuidof(Connection));
  116. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena"),_bstr_t(""),_bstr_t("tiger"),-1);
  117. rs.CreateInstance(__uuidof(Recordset));
  118. rs->Open(_variant_t("select * from MyAddress_ocy"),conn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);
  119. this->ShowData();
  120. return TRUE;  // return TRUE  unless you set the focus to a control
  121. }
  122. void CAddressAdminDlg::OnSysCommand(UINT nID, LPARAM lParam)
  123. {
  124. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  125. {
  126. CAboutDlg dlgAbout;
  127. dlgAbout.DoModal();
  128. }
  129. else
  130. {
  131. CDialog::OnSysCommand(nID, lParam);
  132. }
  133. }
  134. // If you add a minimize button to your dialog, you will need the code below
  135. //  to draw the icon.  For MFC applications using the document/view model,
  136. //  this is automatically done for you by the framework.
  137. void CAddressAdminDlg::OnPaint() 
  138. {
  139. if (IsIconic())
  140. {
  141. CPaintDC dc(this); // device context for painting
  142. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  143. // Center icon in client rectangle
  144. int cxIcon = GetSystemMetrics(SM_CXICON);
  145. int cyIcon = GetSystemMetrics(SM_CYICON);
  146. CRect rect;
  147. GetClientRect(&rect);
  148. int x = (rect.Width() - cxIcon + 1) / 2;
  149. int y = (rect.Height() - cyIcon + 1) / 2;
  150. // Draw the icon
  151. dc.DrawIcon(x, y, m_hIcon);
  152. }
  153. else
  154. {
  155. this->show();
  156. CDialog::OnPaint();
  157. }
  158. }
  159. // The system calls this to obtain the cursor to display while the user drags
  160. //  the minimized window.
  161. HCURSOR CAddressAdminDlg::OnQueryDragIcon()
  162. {
  163. return (HCURSOR) m_hIcon;
  164. }
  165. void CAddressAdminDlg::OnButton8() 
  166. {
  167. // TODO: Add your control notification handler code here
  168. CFileDialog dlg(TRUE);
  169. dlg.DoModal();
  170. CString strFilePath=dlg.GetPathName();
  171. if(strFilePath.GetLength()>0) 
  172. {
  173. CFile fsource;
  174. CFileException fex;
  175. fsource.Open(strFilePath,CFile::modeRead,&fex);
  176. this->iSize=fsource.GetLength();
  177. if(this->pBuffer!=NULL)
  178. {
  179. delete this->pBuffer;
  180. this->pBuffer=NULL;
  181. }
  182. this->pBuffer=new CHAR[iSize];
  183. fsource.Read(this->pBuffer,iSize);
  184. }
  185. //显示图片;
  186. this->p_CurrentImag=this->ShowImage();
  187. CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
  188. pDc->Invalidate();
  189. }
  190. HBITMAP CAddressAdminDlg::ShowImage()
  191. {
  192. LPSTR pTempBuffer=this->pBuffer;
  193. BITMAPFILEHEADER bf;
  194. int leng=sizeof(bf);
  195. LPSTR pInfor=pTempBuffer+leng;
  196. BITMAPINFOHEADER& pInfHeader=*(LPBITMAPINFOHEADER)(pInfor);
  197. BITMAPINFO&       pInfo     =*(LPBITMAPINFO)(pInfor);
  198. LPVOID            pRGBData  =(LPVOID)(pInfo.bmiColors);
  199. CClientDC dc(this);
  200. HBITMAP hResult=CreateDIBitmap(dc.m_hDC,&pInfHeader,CBM_INIT,pRGBData,&pInfo,DIB_RGB_COLORS);
  201. return hResult;
  202. }
  203. void CAddressAdminDlg::show()
  204. {
  205. if(this->p_CurrentImag!=NULL)
  206. {
  207. CStatic* pStc=(CStatic*)this->GetDlgItem(IDC_STATICimg);
  208. CPaintDC dc(pStc);
  209. RECT rt;
  210. pStc->GetClientRect(&rt);
  211. CDC MemDc;
  212. MemDc.CreateCompatibleDC(&dc);
  213. HBITMAP pTemp=(HBITMAP)MemDc.SelectObject(this->p_CurrentImag);
  214. dc.BitBlt(0,0,rt.right,rt.bottom,&MemDc,0,0,SRCCOPY);
  215. MemDc.SelectObject(pTemp);
  216. }
  217. }
  218. void CAddressAdminDlg::OnButton5() 
  219. {
  220. // TODO: Add your control notification handler code here
  221. this->m_address="";
  222. this->m_name="";
  223. this->m_sex="";
  224. this->m_age=0;
  225. this->UpdateData(FALSE);
  226. }
  227. void CAddressAdminDlg::OnButton6() 
  228. {
  229. // TODO: Add your control notification handler code here
  230. this->UpdateData();
  231. rs->AddNew();
  232. rs->GetFields()->GetItem(_variant_t("addr_name"))->Value=_variant_t(this->m_name.AllocSysString());
  233. rs->GetFields()->GetItem(_variant_t("addr_sex"))->Value=_variant_t(this->m_sex.AllocSysString());
  234. rs->GetFields()->GetItem(_variant_t("addr_address"))->Value=_variant_t(this->m_address.AllocSysString());
  235. rs->GetFields()->GetItem(_variant_t("addr_age"))->Value=_variant_t(this->m_age);
  236. //写b字段;
  237. SAFEARRAYBOUND bd[1];
  238. bd[0].lLbound=0;
  239. bd[0].cElements=this->iSize;
  240. SAFEARRAY* pArray=SafeArrayCreate(VT_UI1,1,bd);
  241. char* pTemp=(char*)(this->pBuffer);
  242. for(long i=0;i<iSize;i++)
  243. {
  244. SafeArrayPutElement(pArray,&i,pTemp++);
  245. }
  246. VARIANT abc;
  247. abc.vt=VT_ARRAY | VT_UI1;
  248. abc.parray=pArray;
  249. try{
  250. FieldPtr fld=rs->Fields->GetItem("addr_Picture");
  251. fld->AppendChunk(abc);
  252. }
  253. catch(_com_error ex)
  254. {
  255. int i=0;
  256. i++;
  257. }
  258. rs->Update();
  259. rs->Close();
  260. conn->Close();
  261. }
  262. void CAddressAdminDlg::ShowData()
  263. {
  264.     //提取第一条记录;
  265. if(!rs->adEOF && !rs->BOF)
  266. {
  267. _variant_t vtname=rs->Fields->GetItem("addr_name")->Value;
  268. _variant_t vtage=rs->Fields->GetItem("addr_age")->Value;
  269. _variant_t vtsex=rs->Fields->GetItem("addr_sex")->Value;
  270. _variant_t vtaddress=rs->Fields->GetItem("addr_address")->Value;
  271. this->m_age=vtage.lVal;
  272. this->m_name=CString(vtname.bstrVal);
  273. this->m_sex=CString(vtsex.bstrVal);
  274. this->m_address=CString(vtaddress.bstrVal);
  275. this->UpdateData(FALSE);
  276. this->iSize=rs->Fields->GetItem("addr_picture")->ActualSize;
  277. if(this->pBuffer!=NULL)
  278. {
  279. delete this->pBuffer;
  280. this->pBuffer=NULL;
  281. }
  282. this->pBuffer=new CHAR[this->iSize];
  283. _variant_t vtdata=rs->Fields->GetItem("addr_picture")->GetChunk(this->iSize);
  284. BYTE* pTemp=NULL;
  285. SafeArrayAccessData(vtdata.parray,(void **)&pTemp);
  286. memcpy(this->pBuffer,pTemp,this->iSize);
  287. SafeArrayUnaccessData(vtdata.parray);
  288. //显示图片;
  289. this->p_CurrentImag=this->ShowImage();
  290. //刷新屏幕;
  291. CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
  292. pDc->Invalidate();
  293. this->Invalidate(FALSE);
  294. }
  295. }
  296. void CAddressAdminDlg::OnButton3() 
  297. {
  298. // TODO: Add your control notification handler code here
  299. if(!this->rs->adEOF)
  300. {
  301. this->rs->MoveNext();
  302. this->ShowData();
  303. }
  304. }
  305. void CAddressAdminDlg::OnButton2() 
  306. {
  307. // TODO: Add your control notification handler code here
  308. if(!this->rs->BOF)
  309. {
  310. this->rs->MovePrevious();
  311. this->ShowData();
  312. }
  313. }
  314. //删除数据
  315. void CAddressAdminDlg::OnButton7() 
  316. {
  317. // TODO: Add your control notification handler code here
  318. if(!this->rs->adEOF && !this->rs->BOF)
  319. {
  320. this->rs->Delete(adAffectCurrent);
  321. this->rs->Update();
  322. this->rs->MoveNext();
  323. this->ShowData();
  324. }
  325. }
  326. //更改数据
  327. void CAddressAdminDlg::OnButton9() 
  328. {
  329. // TODO: Add your control notification handler code here
  330. this->UpdateData();
  331. rs->GetFields()->GetItem(_variant_t("addr_name"))->Value=_variant_t(this->m_name.AllocSysString());
  332. rs->GetFields()->GetItem(_variant_t("addr_sex"))->Value=_variant_t(this->m_sex.AllocSysString());
  333. rs->GetFields()->GetItem(_variant_t("addr_address"))->Value=_variant_t(this->m_address.AllocSysString());
  334. rs->GetFields()->GetItem(_variant_t("addr_age"))->Value=_variant_t(this->m_age);
  335. rs->Update();
  336. }