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

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. return TRUE;  // return TRUE  unless you set the focus to a control
  112. }
  113. void CAddressAdminDlg::OnSysCommand(UINT nID, LPARAM lParam)
  114. {
  115. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  116. {
  117. CAboutDlg dlgAbout;
  118. dlgAbout.DoModal();
  119. }
  120. else
  121. {
  122. CDialog::OnSysCommand(nID, lParam);
  123. }
  124. }
  125. // If you add a minimize button to your dialog, you will need the code below
  126. //  to draw the icon.  For MFC applications using the document/view model,
  127. //  this is automatically done for you by the framework.
  128. void CAddressAdminDlg::OnPaint() 
  129. {
  130. if (IsIconic())
  131. {
  132. CPaintDC dc(this); // device context for painting
  133. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  134. // Center icon in client rectangle
  135. int cxIcon = GetSystemMetrics(SM_CXICON);
  136. int cyIcon = GetSystemMetrics(SM_CYICON);
  137. CRect rect;
  138. GetClientRect(&rect);
  139. int x = (rect.Width() - cxIcon + 1) / 2;
  140. int y = (rect.Height() - cyIcon + 1) / 2;
  141. // Draw the icon
  142. dc.DrawIcon(x, y, m_hIcon);
  143. }
  144. else
  145. {
  146. this->show();
  147. CDialog::OnPaint();
  148. }
  149. }
  150. // The system calls this to obtain the cursor to display while the user drags
  151. //  the minimized window.
  152. HCURSOR CAddressAdminDlg::OnQueryDragIcon()
  153. {
  154. return (HCURSOR) m_hIcon;
  155. }
  156. void CAddressAdminDlg::OnButton8() 
  157. {
  158. // TODO: Add your control notification handler code here
  159. CFileDialog dlg(TRUE);
  160. dlg.DoModal();
  161. CString strFilePath=dlg.GetPathName();//获取该图片的路径(空则为0)
  162. if(strFilePath.GetLength()>0) //地址有效的时候
  163. {
  164. CFile fsource;
  165. CFileException fex;
  166. fsource.Open(strFilePath,CFile::modeRead,&fex);//读取的方式打开
  167. this->iSize=fsource.GetLength();//获取图片大小
  168. if(this->pBuffer!=NULL)//pBuffer在头文件中已定义并在构造函数中初始化了
  169. {
  170. delete this->pBuffer;
  171. this->pBuffer=NULL;
  172. }
  173. this->pBuffer=new CHAR[iSize];//连续的字符内存空间
  174. fsource.Read(this->pBuffer,iSize);//读到缓冲区
  175. }
  176. //显示图片;
  177. this->p_CurrentImag=this->ShowImage();
  178. CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
  179. pDc->Invalidate();
  180. }
  181. HBITMAP CAddressAdminDlg::ShowImage()
  182. {
  183. LPSTR pTempBuffer=this->pBuffer;
  184. BITMAPFILEHEADER bf;
  185. int leng=sizeof(bf);
  186. LPSTR pInfor=pTempBuffer+leng;
  187. BITMAPINFOHEADER& pInfHeader=*(LPBITMAPINFOHEADER)(pInfor);
  188. BITMAPINFO&       pInfo     =*(LPBITMAPINFO)(pInfor);
  189. LPVOID            pRGBData  =(LPVOID)(pInfo.bmiColors);
  190. CClientDC dc(this);
  191. HBITMAP hResult=CreateDIBitmap(dc.m_hDC,&pInfHeader,CBM_INIT,pRGBData,&pInfo,DIB_RGB_COLORS);
  192. return hResult;
  193. }
  194. void CAddressAdminDlg::show()
  195. {
  196. if(this->p_CurrentImag!=NULL)
  197. {
  198. CStatic* pStc=(CStatic*)this->GetDlgItem(IDC_STATICimg);
  199. CPaintDC dc(pStc);
  200. RECT rt;
  201. pStc->GetClientRect(&rt);
  202. CDC MemDc;
  203. MemDc.CreateCompatibleDC(&dc);
  204. HBITMAP pTemp=(HBITMAP)MemDc.SelectObject(this->p_CurrentImag);
  205. dc.BitBlt(0,0,rt.right,rt.bottom,&MemDc,0,0,SRCCOPY);
  206. MemDc.SelectObject(pTemp);
  207. }
  208. }
  209. //清空文本框
  210. void CAddressAdminDlg::OnButton5() 
  211. {
  212. // TODO: Add your control notification handler code here
  213. this->m_address="";
  214. this->m_name="";
  215. this->m_sex="";
  216. this->m_age=0;
  217. this->UpdateData(FALSE);//提交到文本框
  218. }
  219. //update上传数据到服务器数据库
  220. void CAddressAdminDlg::OnButton6() 
  221. {
  222. // TODO: Add your control notification handler code here
  223. this->UpdateData(TRUE);//获取文本框数据
  224. _ConnectionPtr conn;
  225. conn.CreateInstance(__uuidof(Connection));
  226. conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena"),_bstr_t(""),_bstr_t("tiger"),-1);
  227. _RecordsetPtr rs;
  228. rs.CreateInstance(__uuidof(Recordset));
  229. rs->Open(_variant_t("select * from MyAddress_ocy"),conn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);
  230. rs->AddNew();//添加一个新节点
  231. //添加非B数据
  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;//安全数组大小声明为long类型
  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. }