AddressAdminDlg.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:8k
源码类别:
ActiveX/DCOM/ATL
开发平台:
Visual C++
- // AddressAdminDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AddressAdmin.h"
- #include "AddressAdminDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAddressAdminDlg dialog
- CAddressAdminDlg::CAddressAdminDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CAddressAdminDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CAddressAdminDlg)
- m_age = 0;
- iSize=0;
- m_name = _T("");
- m_address = _T("");
- m_sex = _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- this->pBuffer=NULL;
- p_CurrentImag=NULL;
- }
- void CAddressAdminDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAddressAdminDlg)
- DDX_Text(pDX, IDC_AGE, m_age);
- DDX_Text(pDX, IDC_Name, m_name);
- DDX_Text(pDX, IDC_Address, m_address);
- DDX_Text(pDX, IDC_SEX, m_sex);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAddressAdminDlg, CDialog)
- //{{AFX_MSG_MAP(CAddressAdminDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
- ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
- ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAddressAdminDlg message handlers
- BOOL CAddressAdminDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- ::CoInitialize(NULL);//数据库环境初始化
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CAddressAdminDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CAddressAdminDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- this->show();
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CAddressAdminDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CAddressAdminDlg::OnButton8()
- {
- // TODO: Add your control notification handler code here
- CFileDialog dlg(TRUE);
- dlg.DoModal();
- CString strFilePath=dlg.GetPathName();//获取该图片的路径(空则为0)
- if(strFilePath.GetLength()>0) //地址有效的时候
- {
- CFile fsource;
- CFileException fex;
- fsource.Open(strFilePath,CFile::modeRead,&fex);//读取的方式打开
- this->iSize=fsource.GetLength();//获取图片大小
- if(this->pBuffer!=NULL)//pBuffer在头文件中已定义并在构造函数中初始化了
- {
- delete this->pBuffer;
- this->pBuffer=NULL;
- }
- this->pBuffer=new CHAR[iSize];//连续的字符内存空间
- fsource.Read(this->pBuffer,iSize);//读到缓冲区
- }
- //显示图片;
- this->p_CurrentImag=this->ShowImage();
- CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
- pDc->Invalidate();
- }
- HBITMAP CAddressAdminDlg::ShowImage()
- {
- LPSTR pTempBuffer=this->pBuffer;
- BITMAPFILEHEADER bf;
- int leng=sizeof(bf);
- LPSTR pInfor=pTempBuffer+leng;
- BITMAPINFOHEADER& pInfHeader=*(LPBITMAPINFOHEADER)(pInfor);
- BITMAPINFO& pInfo =*(LPBITMAPINFO)(pInfor);
- LPVOID pRGBData =(LPVOID)(pInfo.bmiColors);
- CClientDC dc(this);
- HBITMAP hResult=CreateDIBitmap(dc.m_hDC,&pInfHeader,CBM_INIT,pRGBData,&pInfo,DIB_RGB_COLORS);
- return hResult;
- }
- void CAddressAdminDlg::show()
- {
- if(this->p_CurrentImag!=NULL)
- {
- CStatic* pStc=(CStatic*)this->GetDlgItem(IDC_STATICimg);
- CPaintDC dc(pStc);
- RECT rt;
- pStc->GetClientRect(&rt);
- CDC MemDc;
- MemDc.CreateCompatibleDC(&dc);
- HBITMAP pTemp=(HBITMAP)MemDc.SelectObject(this->p_CurrentImag);
- dc.BitBlt(0,0,rt.right,rt.bottom,&MemDc,0,0,SRCCOPY);
- MemDc.SelectObject(pTemp);
- }
- }
- //清空文本框
- void CAddressAdminDlg::OnButton5()
- {
- // TODO: Add your control notification handler code here
- this->m_address="";
- this->m_name="";
- this->m_sex="";
- this->m_age=0;
- this->UpdateData(FALSE);//提交到文本框
- }
- //update上传数据到服务器数据库
- void CAddressAdminDlg::OnButton6()
- {
- // TODO: Add your control notification handler code here
- this->UpdateData(TRUE);//获取文本框数据
- _ConnectionPtr conn;
- conn.CreateInstance(__uuidof(Connection));
- conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena"),_bstr_t(""),_bstr_t("tiger"),-1);
- _RecordsetPtr rs;
- rs.CreateInstance(__uuidof(Recordset));
- rs->Open(_variant_t("select * from MyAddress_ocy"),conn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);
- rs->AddNew();//添加一个新节点
- //添加非B数据
- rs->GetFields()->GetItem(_variant_t("addr_name"))->Value=_variant_t(this->m_name.AllocSysString());
- rs->GetFields()->GetItem(_variant_t("addr_sex"))->Value=_variant_t(this->m_sex.AllocSysString());
- rs->GetFields()->GetItem(_variant_t("addr_address"))->Value=_variant_t(this->m_address.AllocSysString());
- rs->GetFields()->GetItem(_variant_t("addr_age"))->Value=_variant_t(this->m_age);
- //写b字段;
- SAFEARRAYBOUND bd[1];
- bd[0].lLbound=0;
- bd[0].cElements=this->iSize;//安全数组大小声明为long类型
- SAFEARRAY* pArray=SafeArrayCreate(VT_UI1,1,bd);//初级安全数组
- char* pTemp=(char*)(this->pBuffer);//指向含有图片数据的缓冲区
- for(long i=0;i<iSize;i++)
- {
- SafeArrayPutElement(pArray,&i,pTemp++);//拷贝给安全数组
- }
- VARIANT abc;
- abc.vt=VT_ARRAY | VT_UI1;
- abc.parray=pArray;
- try{
- FieldPtr fld=rs->Fields->GetItem("addr_Picture");
- fld->AppendChunk(abc);//安全数组的数据添加到服务器
- }
- catch(_com_error ex)
- {
- int i=0;
- i++;
- }
- rs->Update();
- rs->Close();
- conn->Close();
- }