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. //连接数据库
  114. conn.CreateInstance(__uuidof(Connection));
  115. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena"),_bstr_t(""),_bstr_t("tiger"),-1);
  116. rs.CreateInstance(__uuidof(Recordset));
  117. rs->Open(_variant_t("select * from ocyAddress_ocy"),conn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);
  118. //显示数据用的比较频繁,所以自建一个函数
  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. //万能型转成所需的内存数据类型
  272. this->m_age=vtage.lVal;
  273. this->m_name=CString(vtname.bstrVal);
  274. this->m_sex=CString(vtsex.bstrVal);
  275. this->m_address=CString(vtaddress.bstrVal);
  276. this->UpdateData(FALSE);//上传内存数据到对话框
  277. this->iSize=rs->Fields->GetItem("addr_picture")->ActualSize;//获取图片大小
  278. if(this->pBuffer!=NULL)//目的是在下一步前确保>pBuffer为空
  279. {
  280. delete this->pBuffer;
  281. this->pBuffer=NULL;
  282. }
  283. this->pBuffer=new CHAR[this->iSize];//开辟和图片大小一致的内存
  284. //复制图片二进制数据到万能指针中
  285. _variant_t vtdata=rs->Fields->GetItem("addr_picture")->GetChunk(this->iSize);
  286. BYTE* pTemp=NULL;
  287. //万能指针类型转成一般类型
  288. SafeArrayAccessData(vtdata.parray,(void **)&pTemp);
  289. //内存复制
  290. memcpy(this->pBuffer,pTemp,this->iSize);
  291. //解开安全数组的锁
  292. SafeArrayUnaccessData(vtdata.parray);
  293. //显示图片;
  294. this->p_CurrentImag=this->ShowImage();
  295. //刷新静态控件
  296. CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
  297. pDc->Invalidate();
  298. //刷新窗体
  299. this->Invalidate(FALSE);
  300. }
  301. }
  302. void CAddressAdminDlg::OnButton3() 
  303. {
  304. // TODO: Add your control notification handler code here
  305. if(!this->rs->adEOF)
  306. {
  307. this->rs->MoveNext();
  308. this->ShowData();
  309. }
  310. }
  311. void CAddressAdminDlg::OnButton2() 
  312. {
  313. // TODO: Add your control notification handler code here
  314. if(!this->rs->BOF)
  315. {
  316. this->rs->MovePrevious();
  317. this->ShowData();
  318. }
  319. }