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