www.pudn.com > ArxDatabase.rar > ArxDatabaseDlg.cpp


// ArxDatabaseDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "ArxDatabase.h" 
#include "ArxDatabaseDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
extern CArxDatabaseApp theApp; 
 
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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CArxDatabaseDlg dialog 
 
CArxDatabaseDlg::CArxDatabaseDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CArxDatabaseDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CArxDatabaseDlg) 
	m_CompKind = _T(""); 
	m_CompName = _T(""); 
	m_CompStyle = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CArxDatabaseDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CArxDatabaseDlg) 
	DDX_Control(pDX, IDC_LIST, m_List); 
	DDX_Text(pDX, IDC_COMPKIND, m_CompKind); 
	DDX_Text(pDX, IDC_COMPNAME, m_CompName); 
	DDX_Text(pDX, IDC_COMPSTYLE, m_CompStyle); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CArxDatabaseDlg, CDialog) 
	//{{AFX_MSG_MAP(CArxDatabaseDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_READCOMP, OnReadcomp) 
	ON_BN_CLICKED(IDC_WRITECOMP, OnWritecomp) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CArxDatabaseDlg message handlers 
 
BOOL CArxDatabaseDlg::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 
	    m_pRecordset.CreateInstance(__uuidof(Recordset)); 
 
	// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息, 
	// 因为它有时会经常出现一些想不到的错误。 
	try 
	{ 
		m_pRecordset->Open("SELECT * FROM Component",                // 查询DemoTable表中所有字段 
							theApp.m_pConnection.GetInterfacePtr(),	 // 获取库接库的IDispatch指针 
							adOpenDynamic, 
							adLockOptimistic, 
							adCmdText); 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	} 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CArxDatabaseDlg::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 CArxDatabaseDlg::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 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CArxDatabaseDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
 
void CArxDatabaseDlg::OnReadcomp()  
{ 
	// TODO: Add your control notification handler code here 
	_variant_t var; 
	CString strCompID,strCompName,strCompKind,strCompStyle,strCompPic,strCompDwg; 
 
	// 清空列表框 
	m_List.ResetContent(); 
	strCompID=strCompName=strCompKind=strCompStyle=strCompPic=strCompDwg=""; 
 
	try 
	{ 
		if(!m_pRecordset->BOF) 
			m_pRecordset->MoveFirst(); 
		else 
		{ 
			AfxMessageBox("表内数据为空"); 
			return; 
		} 
 
		// 读入库中各字段并加入列表框中 
		while(!m_pRecordset->adoEOF) 
		{ 
			var = m_pRecordset->GetCollect("CompName"); 
			if(var.vt != VT_NULL) 
				strCompName = (LPCSTR)_bstr_t(var); 
			var = m_pRecordset->GetCollect("CompKind"); 
			if(var.vt != VT_NULL) 
				strCompKind = (LPCSTR)_bstr_t(var); 
			var=m_pRecordset->GetCollect("CompStyle"); 
			if(var.vt!=VT_NULL) 
				strCompKind=(LPCSTR)_bstr_t(var); 
			var=m_pRecordset->GetCollect("CompID"); 
			if(var.vt!=VT_NULL) 
				strCompID=(LPCSTR)_bstr_t(var); 
			 
			//读取dwg文件 
			try 
			{ 
				CString strFileNameDwg; 
                strFileNameDwg.Format("%s.dwg",strCompName); 
				char tmpPath1[_MAX_PATH+1]; 
				GetCurrentDirectory(_MAX_PATH,tmpPath1); 
				strFileNameDwg.Insert(0,tmpPath1); 
				CFile outFile1(strFileNameDwg,CFile::modeCreate|CFile::modeWrite); 
				long nSize1 = m_pRecordset->GetFields()->GetItem("DWG")->ActualSize; 
				if(nSize1 > 0) 
				{ 
					_variant_t	varBLOB1; 
					varBLOB1 = m_pRecordset->GetFields()->GetItem("DWG")->GetChunk(nSize1); 
					if(varBLOB1.vt == (VT_ARRAY | VT_UI1)) 
					{ 
						if(BYTE *pBuffer1 = new BYTE [nSize1+1])		///重新申请必要的存储空间 
						{	 
							char *pBuf1 = NULL; 
							SafeArrayAccessData(varBLOB1.parray,(void **)&pBuf1); 
							memcpy(pBuffer1,pBuf1,nSize1);				 
							 
							///复制数据到缓冲区m_pBMPBuffer 
							SafeArrayUnaccessData (varBLOB1.parray); 
							//	int nSize = lDataSize; 
							//(m_Pic.LoadPictureData(pBuffer, nSize)); 
							outFile1.WriteHuge(pBuffer1,nSize1); 
							 
							delete [] pBuffer1; 
							pBuf1=0; 
							 
						} 
					} 
				} 
			} 
			catch (_com_error e) { 
			} 
			 
			//读取jpg文件 
			try 
			{ 
				CString strFileNameJpg; 
                strFileNameJpg.Format("%s.jpg",strCompName); 
				char tmpPath[_MAX_PATH+1]; 
				GetCurrentDirectory(_MAX_PATH,tmpPath); 
				strFileNameJpg.Insert(0,tmpPath); 
				CFile outFile(strFileNameJpg,CFile::modeCreate|CFile::modeWrite); 
				long nSize = m_pRecordset->GetFields()->GetItem("Picture")->ActualSize; 
				if(nSize > 0) 
				{ 
					_variant_t	varBLOB; 
					varBLOB = m_pRecordset->GetFields()->GetItem("Picture")->GetChunk(nSize); 
					if(varBLOB.vt == (VT_ARRAY | VT_UI1)) 
					{ 
						if(BYTE *pBuffer = new BYTE [nSize+1])		///重新申请必要的存储空间 
						{	 
							char *pBuf = NULL; 
							SafeArrayAccessData(varBLOB.parray,(void **)&pBuf); 
							memcpy(pBuffer,pBuf,nSize);				 
							 
							///复制数据到缓冲区m_pBMPBuffer 
							SafeArrayUnaccessData (varBLOB.parray); 
							//	int nSize = lDataSize; 
							//(m_Pic.LoadPictureData(pBuffer, nSize)); 
							outFile.WriteHuge(pBuffer,nSize); 
							 
							delete [] pBuffer; 
							pBuf=0; 
							 
						} 
					} 
				} 
			} 
			catch (_com_error e) { 
			} 
 
 
 
			m_List.AddString(strCompID+"->"+strCompName+"->"+strCompKind+"->"+strCompStyle); 
 
			m_pRecordset->MoveNext(); 
		} 
 
		// 默认列表指向第一项,同时移动记录指针并显示 
		m_List.SetCurSel(0); 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	} 
} 
	 
 
 
void CArxDatabaseDlg::OnWritecomp()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(); 
	if(m_CompName=="") 
	{ 
		AfxMessageBox("部件名称不能为空!"); 
		return; 
	} 
	try 
	{ 
		//写入字段值 
		m_pRecordset->AddNew(); 
		m_pRecordset->PutCollect("CompName",_variant_t(m_CompName)); 
		m_pRecordset->PutCollect("CompKind",_variant_t(m_CompKind)); 
		m_pRecordset->PutCollect("CompStyle",_variant_t(m_CompStyle)); 
		 
		//在数据库中加入dwg文件 
        CFile f1; 
		CString  FilePathName1; 
		CFileException e1; 
		 
		CFileDialog dlg1(TRUE,NULL,NULL,0,"dwg Files (*.dwg)|*.dwg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 
		if(dlg1.DoModal()==IDOK) 
		{FilePathName1=dlg1.GetPathName(); 
		 
		if(m_Pic1.m_IPicture != NULL) m_Pic1.FreePictureData(); // Important - Avoid Leaks... 
		 
		if(f1.Open(FilePathName1, CFile::modeRead | CFile::typeBinary, &e1)) //打开了一个dwg文件 
		{	 
			 
			int nSize1 = f1.GetLength();          //先得到dwg文件长度 
			BYTE * pBuffer1 = new BYTE [nSize1];  //按文件的大小在堆上申请一块内存 
			 
			if (f1.Read(pBuffer1, nSize1) > 0 )    //把dwg文件读到pBuffer(堆上申请一块内存) 
			{   // +---------------------------------------------- 
				BYTE *pBuf1 = pBuffer1;     ///下面这一大段是把pBuffer里的DWG数据放到库中 
				VARIANT			varBLOB1; 
				SAFEARRAY		*psa1; 
				SAFEARRAYBOUND	rgsabound1[1]; 
				 
				if(pBuf1) 
				{     
					rgsabound1[0].lLbound = 0; 
					rgsabound1[0].cElements = nSize1; 
					psa1 = SafeArrayCreate(VT_UI1, 1, rgsabound1); 
					for (long i = 0; i < (long)nSize1; i++) 
						SafeArrayPutElement (psa1, &i, pBuf1++); 
					varBLOB1.vt = VT_ARRAY | VT_UI1; 
					varBLOB1.parray = psa1; 
					m_pRecordset->GetFields()->GetItem("DWG")->AppendChunk(varBLOB1); 
				}				 
				 
				(m_Pic1.LoadPictureData(pBuffer1, nSize1));//接作调用函数读pBuffer的jpg数据准备显示 
				delete [] pBuffer1;     //删掉堆上申请的那一块内存 
				pBuf1=0;                //以防二次乱用 
			} 
			f1.Close();	 
		} 
		 
		} 
		 
		 
		//在数据库中加入jpg文件 
		CFile f; 
		CString  FilePathName; 
		CFileException e; 
		 
		CFileDialog dlg(TRUE,NULL,NULL,0,"jpg Files (*.jpg)|*.jpg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框 
		if(dlg.DoModal()==IDOK) 
		{FilePathName=dlg.GetPathName(); 
		 
		if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData(); // Important - Avoid Leaks... 
		 
		if(f.Open(FilePathName, CFile::modeRead | CFile::typeBinary, &e)) //打开了一个jpg文件 
		{	 
			 
			int nSize = f.GetLength();          //先得到jpg文件长度 
			BYTE * pBuffer = new BYTE [nSize];  //按文件的大小在堆上申请一块内存 
			 
			if (f.Read(pBuffer, nSize) > 0 )    //把jpg文件读到pBuffer(堆上申请一块内存) 
			{   // +---------------------------------------------- 
				BYTE *pBuf = pBuffer;     ///下面这一大段是把pBuffer里的jpg数据放到库中 
				VARIANT			varBLOB; 
				SAFEARRAY		*psa; 
				SAFEARRAYBOUND	rgsabound[1]; 
				 
				if(pBuf) 
				{     
					rgsabound[0].lLbound = 0; 
					rgsabound[0].cElements = nSize; 
					psa = SafeArrayCreate(VT_UI1, 1, rgsabound); 
					for (long i = 0; i < (long)nSize; i++) 
						SafeArrayPutElement (psa, &i, pBuf++); 
					varBLOB.vt = VT_ARRAY | VT_UI1; 
					varBLOB.parray = psa; 
					m_pRecordset->GetFields()->GetItem("Picture")->AppendChunk(varBLOB); 
				}				 
				 
				(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的jpg数据准备显示 
				delete [] pBuffer;     //删掉堆上申请的那一块内存 
				pBuf=0;                //以防二次乱用 
			} 
			f.Close();	 
		} 
		 
		} 
		 
		 
		 
		m_pRecordset->Update(); 
		 
		AfxMessageBox("插入成功!"); 
		//更新显示其库内容 
		int nCurSel=m_List.GetCurSel(); 
		OnReadcomp(); 
		m_List.SetCurSel(nCurSel); 
 
		 
	} 
	catch(_com_error *e) 
	{ 
		AfxMessageBox(e->ErrorMessage()); 
	} 
	 
} 
  
	 
 
BOOL CArxDatabaseDlg::DestroyWindow()  
{ 
	// 关闭记录集 
	m_pRecordset->Close(); 
	m_pRecordset = NULL; 
	 
	return CDialog::DestroyWindow(); 
}