⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mychunkdlg.cpp

📁 这是书上的代码
💻 CPP
字号:
// MyChunkDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyChunk.h"
#include "MyChunkDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMyChunkDlg dialog

CMyChunkDlg::CMyChunkDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyChunkDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyChunkDlg)
	m_edtConnectStr = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyChunkDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyChunkDlg)
	DDX_Text(pDX, IDC_EDIT1, m_edtConnectStr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyChunkDlg, CDialog)
	//{{AFX_MSG_MAP(CMyChunkDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_LINKDB, OnLinkdb)
	ON_BN_CLICKED(IDC_FILETODB, OnFiletodb)
	ON_BN_CLICKED(IDC_DBTOFILE, OnDbtofile)
	ON_BN_CLICKED(IDC_STREAMFILETODB, OnStreamfiletodb)
	ON_BN_CLICKED(IDC_STREAMDBTOFILE, OnStreamdbtofile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyChunkDlg message handlers

BOOL CMyChunkDlg::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
	CButton* pButton=NULL;
	pButton=(CButton*)GetDlgItem(IDC_FILETODB);
	if(pButton!=NULL)
	{
		pButton->EnableWindow(FALSE);
		pButton=NULL;
	}

	pButton=(CButton*)GetDlgItem(IDC_DBTOFILE);
	if(pButton!=NULL)
	{
		pButton->EnableWindow(FALSE);
		pButton=NULL;
	}

	pButton=(CButton*)GetDlgItem(IDC_STREAMFILETODB);
	if(pButton!=NULL)
	{
		pButton->EnableWindow(FALSE);
		pButton=NULL;
	}
	pButton=(CButton*)GetDlgItem(IDC_STREAMDBTOFILE);
	if(pButton!=NULL)
	{
		pButton->EnableWindow(FALSE);
		pButton=NULL;
	}

	::CoInitialize(NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyChunkDlg::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 CMyChunkDlg::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 CMyChunkDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyChunkDlg::OnLinkdb() 
{
	// TODO: Add your control notification handler code here
	using namespace dl25;
	//using namespace ado25;
	HRESULT hr;
	IDataSourceLocatorPtr dlPrompt=NULL;
	_ConnectionPtr conn=NULL;

	//::CoInitialize(NULL);
	hr=dlPrompt.CreateInstance(__uuidof(DataLinks));
	conn=dlPrompt->PromptNew();
	if(conn!=NULL)
	{
		m_edtConnectStr.Format("%s",(char*)conn->ConnectionString);
		UpdateData(FALSE);
	}


	CButton* pButton=NULL;
	pButton=(CButton*)GetDlgItem(IDC_FILETODB);
	if(pButton!=NULL)
	{
		pButton->EnableWindow();
		pButton=NULL;
	}

	pButton=(CButton*)GetDlgItem(IDC_DBTOFILE);
	if(pButton!=NULL)
	{
		pButton->EnableWindow();
		pButton=NULL;
	}
	pButton=(CButton*)GetDlgItem(IDC_STREAMDBTOFILE);
	if(pButton!=NULL)
	{
		pButton->EnableWindow();
		pButton=NULL;
	}
	pButton=(CButton*)GetDlgItem(IDC_STREAMFILETODB);
	if(pButton!=NULL)
	{
		pButton->EnableWindow();
		pButton=NULL;
	}


}

void CMyChunkDlg::OnFiletodb() 
{
	// TODO: Add your control notification handler code here
	CString filepath="";
	long filelength=0;
	_RecordsetPtr pRs=NULL;
	_ConnectionPtr pConn=NULL;
	HRESULT hr;
    BYTE* pBuf;
	BYTE* pBufEx;
	static char szFilter[]={"bmp files(*.bmp)|*.bmp|jpg files(*.jpg)|*.jpg|gif files(*.gif)|*.gif|All picture files(*.*)|*.*||"};
	CFileDialog fd(TRUE,"bmp",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	if(fd.DoModal()==IDOK)
	{
		filepath=fd.GetPathName();
	}

	CFile file;
	if(filepath!="")
	{
		if(file.Open(filepath,CFile::modeRead)==0)
		{
			MessageBox("Open file error!");
			return;
		}
		filelength=file.GetLength();
        pBuf=new BYTE[filelength];
		memset(pBuf,0,filelength);

		if(pBuf==NULL)
		{
			MessageBox("Can not allocate memory!");
			return;
		}
		file.ReadHuge(pBuf,filelength);

		pBufEx=new BYTE[filelength];
		memset(pBufEx,0,filelength);
	
        
		SAFEARRAY* psa;
		SAFEARRAYBOUND	rsb[1];
		rsb[0].lLbound=0;
		rsb[0].cElements=filelength;

		psa=SafeArrayCreate(VT_UI1,1,rsb);
	
		if(SafeArrayAccessData(psa,(void HUGEP**)&pBufEx)==NOERROR)
			memcpy((void*)pBufEx,(void*)pBuf,filelength);
		SafeArrayUnaccessData(psa);

		_variant_t varBlob;
		varBlob.vt=VT_ARRAY|VT_UI1;
		varBlob.parray=psa;

		pConn.CreateInstance(__uuidof(Connection));
		pConn->ConnectionString=(_bstr_t)m_edtConnectStr;
		pConn->ConnectionTimeout=20;
		hr=pConn->Open("","","",NULL);
        if(FAILED(hr))
		{
			MessageBox("Open connection error!");
			return;
		}
		pRs.CreateInstance(__uuidof(Recordset));
		hr=pRs->Open(_variant_t("bb"),_variant_t((IDispatch*)pConn,TRUE),adOpenKeyset,adLockOptimistic,adCmdTable);
        if(FAILED(hr))
		{
			MessageBox("Open Recordset error!");
			return;
		}
		pRs->AddNew();
		hr=pRs->Fields->GetItem("blob")->AppendChunk(varBlob);
		if(FAILED(hr))
			MessageBox("AppendChunk failed!");
		pRs->Update();
		pRs->Close();
		pConn->Close();
		VariantClear(&varBlob);
      
	
	}
	else
	{
		MessageBox("no file selected!");
		return;
	}

	file.Close();
//	delete pBuf;
//	delete pBufEx;

}

void CMyChunkDlg::OnDbtofile() 
{
	// TODO: Add your control notification handler code here
	_RecordsetPtr pRs=NULL;
    HRESULT hr;

	pRs.CreateInstance(__uuidof(Recordset));
	hr=pRs->Open(_variant_t("bb"),_variant_t(m_edtConnectStr),adOpenKeyset,adLockOptimistic,adCmdTable);
    if(FAILED(hr))
	{
		MessageBox("Recordset Open Failed!");
	}
    
	_variant_t varBlob;
	long nfilelength=0;
	VariantInit(&varBlob);
	pRs->MoveLast();
	if(pRs->EndOfFile)
	{
		MessageBox("Have no record!");
		return;
	}
	nfilelength=pRs->Fields->Item[_bstr_t("blob")]->ActualSize;
	varBlob=pRs->Fields->GetItem("blob")->GetChunk(nfilelength);

	if(varBlob.vt==(VT_ARRAY|VT_UI1))
	{
		BYTE* pBuf=NULL;
		void* pBufEx=NULL;

	
	   SafeArrayAccessData(varBlob.parray,(void **)&pBuf);
	   if(FAILED(hr))
			MessageBox("SafeArrayAccessData Failed!");
	   else
	   {

		   	static char szFilter[]={"bmp files(*.bmp)|*.bmp|jpg files(*.jpg)|*.jpg|gif files(*.gif)|*.gif||"};
	    	CFileDialog fd(FALSE,"bmp",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
			if(fd.DoModal()==IDOK)
			{
				CString filepath(fd.GetPathName());
    		    CFile myFile;
			    myFile.Open(_T(filepath),CFile::modeCreate|CFile::modeWrite);
			    myFile.Write(pBuf,nfilelength);
			    myFile.Close();
			}
			
	   }
		SafeArrayUnaccessData(varBlob.parray);
	}
	else
		MessageBox("varBlob.vt!=VT_ARRAY|VT_UI1");

  VariantClear(&varBlob);
  pRs->Close();

}

void CMyChunkDlg::OnStreamfiletodb() 
{
	// TODO: Add your control notification handler code here
	_StreamPtr      pStream=NULL;
	_RecordsetPtr   pRs=NULL;
	HRESULT hr;

	pRs.CreateInstance(__uuidof(Recordset));
	pRs->Open(_bstr_t("bb"),_bstr_t(m_edtConnectStr),adOpenKeyset,adLockOptimistic,adCmdTable);
    pStream.CreateInstance(__uuidof(Stream));
	
	_variant_t vtSource;
    vtSource.vt=VT_ERROR;
    vtSource.scode=DISP_E_PARAMNOTFOUND;
  	_bstr_t bstrUser(L"");
	_bstr_t bstrPass(L"");
	pStream->Type=adTypeBinary;
	try
	{
		hr=pStream->Open(vtSource,adModeUnknown,adOpenStreamUnspecified,bstrUser,bstrPass);
		if(FAILED(hr))
			MessageBox("Stream open failed!");
		static char szFilter[]={"bmp files(*.bmp)|*.bmp|jpg files(*.jpg)|*.jpg|gif files(*.gif)|*.gif|All picture files(*.*)|*.*||"};
	    CFileDialog fd(TRUE,"bmp",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
		CString filepath="";
		if(fd.DoModal()==IDOK)
		{
			filepath=fd.GetPathName();
			pStream->LoadFromFile(_bstr_t(filepath));
			pRs->AddNew();
			pRs->Fields->GetItem(_variant_t("blob"))->put_Value(_variant_t(pStream->Read(adReadAll)));
			pRs->Update();

		}
		pStream->Close();
		pRs->Close();

	   
	}
	catch(_com_error &e)
	{
		CString error("Message:");
		error+=e.ErrorMessage();
		error+="   Discription:";
		error+=e.Description();
		MessageBox(error);
	}
  

	
}

void CMyChunkDlg::OnStreamdbtofile() 
{
	// TODO: Add your control notification handler code here
	_StreamPtr      pStream=NULL;
	_RecordsetPtr   pRs=NULL;
	HRESULT hr;

	pRs.CreateInstance(__uuidof(Recordset));
	pRs->Open(_bstr_t("bb"),_bstr_t(m_edtConnectStr),adOpenKeyset,adLockOptimistic,adCmdTable);
    pStream.CreateInstance(__uuidof(Stream));

	_variant_t vtSource;
    vtSource.vt=VT_ERROR;
    vtSource.scode=DISP_E_PARAMNOTFOUND;
  	_bstr_t bstrUser(L"");
	_bstr_t bstrPass(L"");
	pStream->Type=adTypeBinary;

	try
	{
		hr=pStream->Open(vtSource,adModeUnknown,adOpenStreamUnspecified,bstrUser,bstrPass);
		if(FAILED(hr))
			MessageBox("Stream open failed!");
		pRs->MoveLast();
		if(pRs->EndOfFile)
		{
			MessageBox("Have no record!");
			return;
		}
		static char szFilter[]={"bmp files(*.bmp)|*.bmp|jpg files(*.jpg)|*.jpg|gif files(*.gif)|*.gif||"};
		CFileDialog fd(FALSE,"bmp",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
		if(fd.DoModal()==IDOK)
		{
			CString filepath;
			filepath=fd.GetPathName();
			pStream->Write(_variant_t(pRs->Fields->GetItem(_variant_t("blob"))->GetValue()));
		    pStream->SaveToFile(_bstr_t(filepath),adSaveCreateOverWrite);
		}
		pStream->Close();
		pRs->Close();
	}

	catch(_com_error &e)
	{
		CString error("Message:");
		error+=e.ErrorMessage();
		error+="   Discription:";
		error+=e.Description();

		MessageBox(error);
	}
  

}

BOOL CMyChunkDlg::DestroyWindow() 
{
	
	// TODO: Add your specialized code here and/or call the base class
	::CoUninitialize();
	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -