AddressAdminDlg.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:9k
源码类别:
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)
- ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- //}}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);
- //连接数据库
- conn.CreateInstance(__uuidof(Connection));
- conn->Open(_bstr_t("Provider=OraOLEDB.Oracle.1;User Id=scott;Data Source=tarena"),_bstr_t(""),_bstr_t("tiger"),-1);
- rs.CreateInstance(__uuidof(Recordset));
- rs->Open(_variant_t("select * from ocyAddress_ocy"),conn.GetInterfacePtr(),adOpenDynamic,adLockPessimistic,adCmdText);
- //显示数据用的比较频繁,所以自建一个函数
- this->ShowData();
- 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();
- if(strFilePath.GetLength()>0)
- {
- CFile fsource;
- CFileException fex;
- fsource.Open(strFilePath,CFile::modeRead,&fex);
- this->iSize=fsource.GetLength();
- if(this->pBuffer!=NULL)
- {
- 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);
- }
- void CAddressAdminDlg::OnButton6()
- {
- // TODO: Add your control notification handler code here
- this->UpdateData();
- rs->AddNew();//添加新节点
- 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;
- 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();
- }
- void CAddressAdminDlg::ShowData()
- {
- //提取第一条记录;
- if(!rs->adEOF && !rs->BOF)
- {//根据属性名赋值
- _variant_t vtname=rs->Fields->GetItem("addr_name")->Value;
- _variant_t vtage=rs->Fields->GetItem("addr_age")->Value;
- _variant_t vtsex=rs->Fields->GetItem("addr_sex")->Value;
- _variant_t vtaddress=rs->Fields->GetItem("addr_address")->Value;
- //万能型转成所需的内存数据类型
- this->m_age=vtage.lVal;
- this->m_name=CString(vtname.bstrVal);
- this->m_sex=CString(vtsex.bstrVal);
- this->m_address=CString(vtaddress.bstrVal);
- this->UpdateData(FALSE);//上传内存数据到对话框
- this->iSize=rs->Fields->GetItem("addr_picture")->ActualSize;//获取图片大小
- if(this->pBuffer!=NULL)//目的是在下一步前确保>pBuffer为空
- {
- delete this->pBuffer;
- this->pBuffer=NULL;
- }
- this->pBuffer=new CHAR[this->iSize];//开辟和图片大小一致的内存
- //复制图片二进制数据到万能指针中
- _variant_t vtdata=rs->Fields->GetItem("addr_picture")->GetChunk(this->iSize);
- BYTE* pTemp=NULL;
- //万能指针类型转成一般类型
- SafeArrayAccessData(vtdata.parray,(void **)&pTemp);
- //内存复制
- memcpy(this->pBuffer,pTemp,this->iSize);
- //解开安全数组的锁
- SafeArrayUnaccessData(vtdata.parray);
- //显示图片;
- this->p_CurrentImag=this->ShowImage();
- //刷新静态控件
- CStatic* pDc=(CStatic*)(this->GetDlgItem(IDC_STATICimg));
- pDc->Invalidate();
- //刷新窗体
- this->Invalidate(FALSE);
- }
- }
- void CAddressAdminDlg::OnButton3()
- {
- // TODO: Add your control notification handler code here
- if(!this->rs->adEOF)
- {
- this->rs->MoveNext();
- this->ShowData();
- }
- }
- void CAddressAdminDlg::OnButton2()
- {
- // TODO: Add your control notification handler code here
- if(!this->rs->BOF)
- {
- this->rs->MovePrevious();
- this->ShowData();
- }
- }