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

📄 lobtestdlg.cpp

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

#include "stdafx.h"
#include "LOBTest.h"
#include "LOBTestDlg.h"

#include "BLOBShowWnd.h"

//自己定义的其他头文件
#include "ImageSet.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()

/////////////////////////////////////////////////////////////////////////////
// CLOBTestDlg dialog

CLOBTestDlg::CLOBTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLOBTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLOBTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLOBTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLOBTestDlg)
	DDX_Control(pDX, IDC_LIST_BLOB, m_BLOBList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLOBTestDlg, CDialog)
	//{{AFX_MSG_MAP(CLOBTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
	ON_BN_CLICKED(IDC_BUTTON_SHOW, OnButtonShow)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_BLOB, OnDblclkListBlob)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLOBTestDlg message handlers

BOOL CLOBTestDlg::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_BLOBList.SetExtendedStyle(LVS_EX_FLATSB | LVS_EX_FULLROWSELECT |LVS_EX_GRIDLINES);
    m_BLOBList.InsertColumn(0,"数据库中的图像",LVCFMT_LEFT,200);
	m_BLOBList.InsertColumn(1,"图像的大小",LVCFMT_LEFT,213);
 
	//填充数据
	int         nIndex = 0;
    CImageSet   rs(&((CLOBTestApp*)AfxGetApp())->m_DB);
    try
	{
		rs.Open();
		while   (!rs.IsEOF())
		{
			m_BLOBList.InsertItem(nIndex,"");
			m_BLOBList.SetItem(nIndex,0,LVIF_TEXT,rs.m_BLOBName,NULL,NULL,NULL,NULL);   
			CString strLength=" ";
			strLength.Format("%d bytes",rs.m_BLOBLength);
			m_BLOBList.SetItem(nIndex,1,LVIF_TEXT,strLength,NULL,NULL,NULL,NULL);   

			nIndex++;
			rs.MoveNext();
		}

		rs.Close();
	}
	catch(CException* pE)
    {
        pE->ReportError();
        pE->Delete();
        return 0;
    }
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLOBTestDlg::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 CLOBTestDlg::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 CLOBTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
////////////////////////////////////////////////////////////////////////////////
//函数名				OnButtonAdd
//函数输入参数			无
//函数返回值			无
//函数功能				添加按钮对应的消息函数
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::OnButtonAdd() 
{
	// TODO: Add your control notification handler code here
	StoreBLOBInDB();
}
//	OnButtonAdd
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//函数名				OnButtonDelete
//函数输入参数			无
//函数返回值			无
//函数功能				删除按钮对应的消息函数
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::OnButtonDelete() 
{
	// TODO: Add your control notification handler code here
	DeleteBLOBInDB();
}
//	OnButtonDelete
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//函数名				OnButtonExit
//函数输入参数			无
//函数返回值			无
//函数功能				退出按钮对应的消息函数
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::OnButtonExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}
//	OnButtonExit
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//函数名				OnButtonShow
//函数输入参数			无
//函数返回值			无
//函数功能				显示按钮对应的消息函数
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::OnButtonShow() 
{
	// TODO: Add your control notification handler code here
	ShowBLOBInDB();
}
//	OnButtonShow
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//函数名				StoreBLOBInDB
//函数输入参数			无
//函数返回值			无
//函数功能				将位图文件存到数据库中
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::StoreBLOBInDB()
{
	static char BASED_CODE szFilter[] = "Bitmap Files (*.bmp)|*.bmp||";
 
	//将位图文件存入数据库中
	CImageSet       rs(&((CLOBTestApp*)AfxGetApp())->m_DB);
	CFileDialog     fileDlg(TRUE,NULL,NULL,0,szFilter,this);

    if  (IDOK != fileDlg.DoModal())
        return;

    try
    {
		//加入数据
        rs.Open();
        rs.AddNew();

        CFile		fileImage;
        CFileStatus	fileStatus;

        fileImage.Open(fileDlg.GetPathName(), CFile::modeRead);
        fileImage.GetStatus(fileStatus);

        rs.m_BLOBName = fileImage.GetFileTitle();
        rs.m_BLOBImage.m_dwDataLength = fileStatus.m_size;
		rs.m_BLOBLength = fileStatus.m_size;

        HGLOBAL hGlobal		= GlobalAlloc(GPTR,fileStatus.m_size);
        rs.m_BLOBImage.m_hData = GlobalLock(hGlobal);

        fileImage.ReadHuge(rs.m_BLOBImage.m_hData,fileStatus.m_size);

        rs.SetFieldDirty(&rs.m_BLOBImage);
        rs.SetFieldNull(&rs.m_BLOBImage,FALSE);
        rs.Update();

        GlobalUnlock(hGlobal);

        rs.Close();

		//将新放入数据库中的图象数据加入List控件中
		m_BLOBList.InsertItem(0,"");
 		m_BLOBList.SetItem(0,0,LVIF_TEXT,fileImage.GetFileTitle(),NULL,NULL,NULL,NULL);   
		CString strLength=" ";
		strLength.Format("%d bytes",fileStatus.m_size);
		m_BLOBList.SetItem(0,1,LVIF_TEXT,strLength,NULL,NULL,NULL,NULL);   
	}
    catch(CException* pE)
    {
        pE->ReportError();
        pE->Delete();
        return;
    }

}
//	StoreBLOBInDB
///////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//函数名				DeleteBLOBInDB
//函数输入参数			无
//函数返回值			无
//函数功能				将位图文件从数据库中删除
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::DeleteBLOBInDB()
{

	//得到当前选中的行
    int nIndex = m_BLOBList.GetNextItem(-1,LVNI_SELECTED);
    if  (nIndex < 0)return;
   
	CString     strFileName = m_BLOBList.GetItemText(nIndex,0);


	CImageSet   rs(&((CLOBTestApp*)AfxGetApp())->m_DB);
    rs.m_strFilter.Format("BLOBName = '%s'",strFileName);
    try
    {
		//进行删除操作
        rs.Open();
        if  (rs.IsEOF() || !rs.CanUpdate())
            AfxMessageBox("Unable to delete image from db");
        else
        {
			//删除在数据库中的信息
			((CLOBTestApp*)AfxGetApp())->m_DB.BeginTrans() ;
            rs.Delete();
			((CLOBTestApp*)AfxGetApp())->m_DB.CommitTrans();
			//删除在List控件中的信息
			m_BLOBList.DeleteItem(nIndex);
        }

        rs.Close();
 
    }
    catch(CException* pE)
    {
        pE->ReportError();
        pE->Delete();
        return;
    }

}
//	DeleteBLOBInDB
/////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//函数名				ShowBLOBInDB
//函数输入参数			无
//函数返回值			无
//函数功能				将数据库中的位图文件进行显示
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::ShowBLOBInDB()
{
    int nIndex = m_BLOBList.GetNextItem(-1,LVNI_SELECTED);
    if  (nIndex < 0)return;

	//将Lob数据从数据库中读出来到文件
	CImageSet   rs(&((CLOBTestApp*)AfxGetApp())->m_DB);
    CString     strFileName = m_BLOBList.GetItemText(nIndex,0);

    rs.m_strFilter.Format("BLOBName = '%s'",strFileName);
	
	CString strTitle ="图像显示: "+strFileName;
    
	try
    {
        rs.Open();
        if  (rs.IsEOF())
            AfxMessageBox("Unable to get image from db");
        else
        {
			//读到临时文件中
            char    tmpPath[_MAX_PATH+1];
            GetTempPath(_MAX_PATH,tmpPath);

            strFileName.Insert(0,tmpPath);
            
            CFile	outFile(strFileName,CFile::modeCreate|CFile::modeWrite);
            LPSTR	buffer = (LPSTR)GlobalLock(rs.m_BLOBImage.m_hData);
            outFile.WriteHuge(buffer,rs.m_BLOBImage.m_dwDataLength);
            GlobalUnlock(rs.m_BLOBImage.m_hData);
            outFile.Close();

			//把文件放到一个新的窗口中显示
			CBLOBShowWnd *pWnd = new CBLOBShowWnd();
			pWnd->SetImage(strFileName);
			pWnd->Create(NULL,strTitle,WS_OVERLAPPEDWINDOW,CRect(300,300,600,600),this,NULL);
			pWnd->ShowWindow(SW_SHOW);
        }

        rs.Close();
 
    }
    catch(CException* pE)
    {
        pE->ReportError();
        pE->Delete();
        return;
    }

}
//	ShowBLOBInDB
///////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//函数名				ShowBLOBInDB
//函数输入参数			NMHDR* pNMHDR		wizard产生
//						LRESULT* pResult	wizard产生
//函数返回值			无
//函数功能				将数据库中的位图文件进行显示
//函数开发人			林晨
//函数开发时间			2002年4月8日
//函数修改历史			...
//函数测试时间			2002年4月8日

void CLOBTestDlg::OnDblclkListBlob(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here

	ShowBLOBInDB();

	*pResult = 0;
}
//	OnDblclickListBlob
////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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