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

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. //}}AFX_MSG_MAP
  86. END_MESSAGE_MAP()
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CAddressAdminDlg message handlers
  89. BOOL CAddressAdminDlg::OnInitDialog()
  90. {
  91. CDialog::OnInitDialog();
  92. // Add "About..." menu item to system menu.
  93. // IDM_ABOUTBOX must be in the system command range.
  94. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  95. ASSERT(IDM_ABOUTBOX < 0xF000);
  96. CMenu* pSysMenu = GetSystemMenu(FALSE);
  97. if (pSysMenu != NULL)
  98. {
  99. CString strAboutMenu;
  100. strAboutMenu.LoadString(IDS_ABOUTBOX);
  101. if (!strAboutMenu.IsEmpty())
  102. {
  103. pSysMenu->AppendMenu(MF_SEPARATOR);
  104. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  105. }
  106. }
  107. // Set the icon for this dialog.  The framework does this automatically
  108. //  when the application's main window is not a dialog
  109. SetIcon(m_hIcon, TRUE); // Set big icon
  110. SetIcon(m_hIcon, FALSE); // Set small icon
  111. // TODO: Add extra initialization here
  112. ::CoInitialize(NULL);
  113. conn.CreateInstance(__uuidof(Connection));
  114. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena"),_bstr_t(""),_bstr_t("tiger"),-1);
  115. rs.CreateInstance(__uuidof(Recordset));
  116. rs->Open(_variant_t("select * from MyAddress_ocy"),conn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);
  117. this->ShowData();
  118. return TRUE;  // return TRUE  unless you set the focus to a control
  119. }
  120. void CAddressAdminDlg::OnSysCommand(UINT nID, LPARAM lParam)
  121. {
  122. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  123. {
  124. CAboutDlg dlgAbout;
  125. dlgAbout.DoModal();
  126. }
  127. else
  128. {
  129. CDialog::OnSysCommand(nID, lParam);
  130. }
  131. }
  132. // If you add a minimize button to your dialog, you will need the code below
  133. //  to draw the icon.  For MFC applications using the document/view model,
  134. //  this is automatically done for you by the framework.
  135. void CAddressAdminDlg::OnPaint() 
  136. {
  137. if (IsIconic())
  138. {
  139. CPaintDC dc(this); // device context for painting
  140. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  141. // Center icon in client rectangle
  142. int cxIcon = GetSystemMetrics(SM_CXICON);
  143. int cyIcon = GetSystemMetrics(SM_CYICON);
  144. CRect rect;
  145. GetClientRect(&rect);
  146. int x = (rect.Width() - cxIcon + 1) / 2;
  147. int y = (rect.Height() - cyIcon + 1) / 2;
  148. // Draw the icon
  149. dc.DrawIcon(x, y, m_hIcon);
  150. }
  151. else
  152. {
  153. this->show();
  154. CDialog::OnPaint();
  155. }
  156. }
  157. // The system calls this to obtain the cursor to display while the user drags
  158. //  the minimized window.
  159. HCURSOR CAddressAdminDlg::OnQueryDragIcon()
  160. {
  161. return (HCURSOR) m_hIcon;
  162. }
  163. void CAddressAdminDlg::OnButton8() 
  164. {
  165. // TODO: Add your control notification handler code here
  166. CFileDialog dlg(TRUE);
  167. dlg.DoModal();
  168. CString strFilePath=dlg.GetPathName();
  169. if(strFilePath.GetLength()>0) 
  170. {
  171. CFile fsource;
  172. CFileException fex;
  173. fsource.Open(strFilePath,CFile::modeRead,&fex);
  174. this->iSize=fsource.GetLength();
  175. if(this->pBuffer!=NULL)
  176. {
  177. delete this->pBuffer;
  178. this->pBuffer=NULL;
  179. }
  180. this->pBuffer=new CHAR[iSize];
  181. fsource.Read(this->pBuffer,iSize);
  182. }
  183. //显示图片;
  184. this->p_CurrentImag=this->ShowImage();
  185. CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
  186. pDc->Invalidate();
  187. }
  188. HBITMAP CAddressAdminDlg::ShowImage()
  189. {
  190. LPSTR pTempBuffer=this->pBuffer;
  191. BITMAPFILEHEADER bf;
  192. int leng=sizeof(bf);
  193. LPSTR pInfor=pTempBuffer+leng;
  194. BITMAPINFOHEADER& pInfHeader=*(LPBITMAPINFOHEADER)(pInfor);
  195. BITMAPINFO&       pInfo     =*(LPBITMAPINFO)(pInfor);
  196. LPVOID            pRGBData  =(LPVOID)(pInfo.bmiColors);
  197. CClientDC dc(this);
  198. HBITMAP hResult=CreateDIBitmap(dc.m_hDC,&pInfHeader,CBM_INIT,pRGBData,&pInfo,DIB_RGB_COLORS);
  199. return hResult;
  200. }
  201. void CAddressAdminDlg::show()
  202. {
  203. if(this->p_CurrentImag!=NULL)
  204. {
  205. CStatic* pStc=(CStatic*)this->GetDlgItem(IDC_STATICimg);
  206. CPaintDC dc(pStc);
  207. RECT rt;
  208. pStc->GetClientRect(&rt);
  209. CDC MemDc;
  210. MemDc.CreateCompatibleDC(&dc);
  211. HBITMAP pTemp=(HBITMAP)MemDc.SelectObject(this->p_CurrentImag);
  212. dc.BitBlt(0,0,rt.right,rt.bottom,&MemDc,0,0,SRCCOPY);
  213. MemDc.SelectObject(pTemp);
  214. }
  215. }
  216. void CAddressAdminDlg::OnButton5() 
  217. {
  218. // TODO: Add your control notification handler code here
  219. this->m_address="";
  220. this->m_name="";
  221. this->m_sex="";
  222. this->m_age=0;
  223. this->UpdateData(FALSE);
  224. }
  225. void CAddressAdminDlg::OnButton6() 
  226. {
  227. // TODO: Add your control notification handler code here
  228. this->UpdateData();
  229. rs->AddNew();
  230. rs->GetFields()->GetItem(_variant_t("addr_name"))->Value=_variant_t(this->m_name.AllocSysString());
  231. rs->GetFields()->GetItem(_variant_t("addr_sex"))->Value=_variant_t(this->m_sex.AllocSysString());
  232. rs->GetFields()->GetItem(_variant_t("addr_address"))->Value=_variant_t(this->m_address.AllocSysString());
  233. rs->GetFields()->GetItem(_variant_t("addr_age"))->Value=_variant_t(this->m_age);
  234. //写b字段;
  235. SAFEARRAYBOUND bd[1];
  236. bd[0].lLbound=0;
  237. bd[0].cElements=this->iSize;
  238. SAFEARRAY* pArray=SafeArrayCreate(VT_UI1,1,bd);
  239. char* pTemp=(char*)(this->pBuffer);
  240. for(long i=0;i<iSize;i++)
  241. {
  242. SafeArrayPutElement(pArray,&i,pTemp++);
  243. }
  244. VARIANT abc;
  245. abc.vt=VT_ARRAY | VT_UI1;
  246. abc.parray=pArray;
  247. try{
  248. FieldPtr fld=rs->Fields->GetItem("addr_Picture");
  249. fld->AppendChunk(abc);
  250. }
  251. catch(_com_error ex)
  252. {
  253. int i=0;
  254. i++;
  255. }
  256. rs->Update();
  257. rs->Close();
  258. conn->Close();
  259. }
  260. void CAddressAdminDlg::ShowData()
  261. {
  262.     //提取第一条记录;
  263. if(!rs->adEOF && !rs->BOF)
  264. {
  265. _variant_t vtname=rs->Fields->GetItem("addr_name")->Value;
  266. _variant_t vtage=rs->Fields->GetItem("addr_age")->Value;
  267. _variant_t vtsex=rs->Fields->GetItem("addr_sex")->Value;
  268. _variant_t vtaddress=rs->Fields->GetItem("addr_address")->Value;
  269. this->m_age=vtage.lVal;
  270. this->m_name=CString(vtname.bstrVal);
  271. this->m_sex=CString(vtsex.bstrVal);
  272. this->m_address=CString(vtaddress.bstrVal);
  273. this->UpdateData(FALSE);
  274. this->iSize=rs->Fields->GetItem("addr_picture")->ActualSize;
  275. if(this->pBuffer!=NULL)
  276. {
  277. delete this->pBuffer;
  278. this->pBuffer=NULL;
  279. }
  280. this->pBuffer=new CHAR[this->iSize];
  281. _variant_t vtdata=rs->Fields->GetItem("addr_picture")->GetChunk(this->iSize);
  282. BYTE* pTemp=NULL;
  283. SafeArrayAccessData(vtdata.parray,(void **)&pTemp);
  284. memcpy(this->pBuffer,pTemp,this->iSize);
  285. SafeArrayUnaccessData(vtdata.parray);
  286. //显示图片;
  287. this->p_CurrentImag=this->ShowImage();
  288. //刷新屏幕;
  289. CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
  290. pDc->Invalidate();
  291. this->Invalidate(FALSE);
  292. }
  293. }
  294. //显示下一条数据信息
  295. void CAddressAdminDlg::OnButton3() 
  296. {
  297. // TODO: Add your control notification handler code here
  298. if(!this->rs->adEOF)
  299. {
  300. this->rs->MoveNext();
  301. this->ShowData();
  302. }
  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. }