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

📄 accesspicturedlg.cpp

📁 一个应用ADO使用ACCESS数据库的图像显示程序
💻 CPP
字号:
// AccessPictureDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AccessPicture.h"
#include "AccessPictureDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


extern CAccessPictureApp theApp;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CAccessPictureDlg dialog

CAccessPictureDlg::CAccessPictureDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAccessPictureDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAccessPictureDlg)
	m_Old = _T("");
	m_UserName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bModify = FALSE;
	m_hPhotoBitmap = NULL;
	m_pBMPBuffer = NULL;
}

void CAccessPictureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAccessPictureDlg)
	DDX_Control(pDX, IDC_USERNAME, m_editUserName);
	DDX_Control(pDX, IDC_OLD, m_editOld);
	DDX_Control(pDX, IDC_SELECTPHOTO, m_buttonSelectPhoto);
	DDX_Control(pDX, IDC_SAVEINFO, m_buttonSaveInfo);
	DDX_Control(pDX, IDC_PREVIOUSUSER, m_buttonPreviousUser);
	DDX_Control(pDX, IDC_NEXTUSER, m_buttonNextUser);
	DDX_Control(pDX, IDC_DELETEUSER, m_buttonDeleteUser);
	DDX_Text(pDX, IDC_OLD, m_Old);
	DDX_Text(pDX, IDC_USERNAME, m_UserName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAccessPictureDlg, CDialog)
	//{{AFX_MSG_MAP(CAccessPictureDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADDUSER, OnAdduser)
	ON_BN_CLICKED(IDC_PREVIOUSUSER, OnPrevioususer)
	ON_BN_CLICKED(IDC_NEXTUSER, OnNextuser)
	ON_BN_CLICKED(IDC_SELECTPHOTO, OnSelectphoto)
	ON_BN_CLICKED(IDC_DELETEUSER, OnDeleteuser)
	ON_BN_CLICKED(IDC_SAVEINFO, OnSaveinfo)
	ON_EN_CHANGE(IDC_USERNAME, OnChangeUsername)
	ON_EN_CHANGE(IDC_OLD, OnChangeOld)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAccessPictureDlg message handlers

BOOL CAccessPictureDlg::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_bNewUser = FALSE;
	m_pRecordset.CreateInstance("ADODB.Recordset");
	HRESULT hr = m_pRecordset->Open("SELECT * FROM userphoto",
		_variant_t((IDispatch *)theApp.m_pConnection,true),
		adOpenDynamic,adLockPessimistic,adCmdText);
    if(SUCCEEDED(hr))
	{
		ReadData();
	}
	ResetControls();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAccessPictureDlg::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 CAccessPictureDlg::OnPaint() 
{
	CPaintDC dc(this);
	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
	{
		DrawUserPhoto(100,100,&dc);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAccessPictureDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

VOID CAccessPictureDlg::ReadData()
{
	DestroyPhoto();						///清除原图像
	if(m_pRecordset->adoEOF||m_pRecordset->BOF) 
	{
		m_UserName.Empty();
		m_Old.Empty();
		UpdateData(FALSE);
		return;
	}
	long lDataSize = m_pRecordset->GetFields()->
		GetItem("photo")->ActualSize;
	if(lDataSize > 0)
	{
		_variant_t			varBLOB;
		varBLOB = m_pRecordset->GetFields()->
			GetItem("photo")->GetChunk(lDataSize);
		if(varBLOB.vt == (VT_ARRAY | VT_UI1))
		{
			///重新分配必要的存储空间
			if(m_pBMPBuffer = new char[lDataSize+1]) 
			{	
				char *pBuf = NULL;
				SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);

				///复制数据到缓冲区m_pBMPBuffer
				memcpy(m_pBMPBuffer,pBuf,lDataSize);			
				SafeArrayUnaccessData (varBLOB.parray);
				m_nFileLen = lDataSize;

				///生成BITMAP对象
				m_hPhotoBitmap = BufferToHBITMAP();					
			}
		}
	}
	m_UserName = m_pRecordset->GetCollect("username").bstrVal;
	m_Old.Format("%d",m_pRecordset->GetCollect("old").lVal);
	m_editUserName.EnableWindow();
	m_editOld.EnableWindow();
	m_buttonSelectPhoto.EnableWindow();
	m_buttonDeleteUser.EnableWindow();
	UpdateData(FALSE);

}

HBITMAP CAccessPictureDlg::BufferToHBITMAP()
{
	HBITMAP				hBmp;
	LPSTR				hDIB,lpBuffer = m_pBMPBuffer;
	LPVOID				lpDIBBits;
	BITMAPFILEHEADER	bmfHeader;
	DWORD				bmfHeaderLen;

	//获得位图的头信息
	bmfHeaderLen = sizeof(bmfHeader);
	strncpy((LPSTR)&bmfHeader,(LPSTR)lpBuffer,bmfHeaderLen);

	//根据获得的信息头判断是否是位图
	if (bmfHeader.bfType != (*(WORD*)"BM")) return NULL;

	//获取位图数据
	hDIB = lpBuffer + bmfHeaderLen;
	BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
	BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
	lpDIBBits=(lpBuffer)+((BITMAPFILEHEADER *)lpBuffer)->bfOffBits;

	//创建位图
	CClientDC dc(this);
	hBmp = CreateDIBitmap(dc.m_hDC,&bmiHeader,
		CBM_INIT,lpDIBBits,&bmInfo,DIB_RGB_COLORS);
	return hBmp;

}

VOID CAccessPictureDlg::DestroyPhoto()
{
	if(m_hPhotoBitmap)
	{
		DeleteObject(m_hPhotoBitmap);
		m_hPhotoBitmap = NULL;
	}
	if(m_pBMPBuffer)
	{
		delete m_pBMPBuffer;
		m_pBMPBuffer = NULL;
	}

}

BOOL CAccessPictureDlg::DestroyWindow()
{
	DestroyPhoto();
	m_pRecordset->Close();
	return CDialog::DestroyWindow();

}

VOID CAccessPictureDlg::DrawUserPhoto(int x, int y, CDC *pDC)
{
	if(!m_hPhotoBitmap) return;
	HBITMAP OldBitmap;
	CDC MemDC;
	MemDC.CreateCompatibleDC(pDC);
	OldBitmap=(HBITMAP)MemDC.SelectObject(m_hPhotoBitmap);
	pDC->BitBlt(x,y,100,130,&MemDC,0,0,SRCCOPY);
	MemDC.SelectObject(OldBitmap);

}



BOOL CAccessPictureDlg::FirstRecord()
{
	if(m_pRecordset->BOF)
	{
		m_editUserName.EnableWindow(FALSE);
		m_editOld.EnableWindow(FALSE);
		m_buttonSelectPhoto.EnableWindow(FALSE);
		return TRUE;
	}
	m_pRecordset->MovePrevious();
	if(m_pRecordset->BOF)
	{
		m_pRecordset->MoveFirst();
		return TRUE;
	}
	else
	{
		m_pRecordset->MoveNext();
		return FALSE;
	}

}

BOOL CAccessPictureDlg::LastRecord()
{
	if(m_pRecordset->adoEOF)
	{
		m_editUserName.EnableWindow(FALSE);
		m_editOld.EnableWindow(FALSE);
		m_buttonSelectPhoto.EnableWindow(FALSE);
		return TRUE;
	}
	m_pRecordset->MoveNext();
	if(m_pRecordset->adoEOF)
	{
		m_pRecordset->MovePrevious();
		return TRUE;
	}
	else
	{
		m_pRecordset->MovePrevious();
		return FALSE;
	}

}

BOOL CAccessPictureDlg::LoadBMPFile(const char *pBMPPathname)
{
	CFile file;
	if( !file.Open( pBMPPathname, CFile::modeRead) )
		return FALSE;
	m_nFileLen = file.GetLength();
	m_pBMPBuffer = new char[m_nFileLen + 1];
	if(!m_pBMPBuffer)
		return FALSE;
	if(file.ReadHuge(m_pBMPBuffer,m_nFileLen) != m_nFileLen)
		return FALSE;
	return TRUE;

}

VOID CAccessPictureDlg::ResetControls()
{
	BOOL IsFirstRecord = FirstRecord();
	BOOL IsLastRecord = LastRecord();
	m_buttonPreviousUser.EnableWindow(!IsFirstRecord);
	m_buttonNextUser.EnableWindow(!IsLastRecord);

}

void CAccessPictureDlg::OnAdduser() 
{
	// TODO: Add your control notification handler code here
	if(m_bModify)
	{
		int nRet = MessageBox("当前用户信息尚未保存,是否保存?","AccessPicture",MB_YESNOCANCEL);
		switch(nRet)
		{
		case IDCANCEL:
			return;
		case IDYES:
			OnSaveinfo();
			break;
		case IDNO:
		default:
			break;
		}
	}
	m_editUserName.EnableWindow();
	m_editOld.EnableWindow();
	m_buttonSelectPhoto.EnableWindow();
	m_buttonDeleteUser.EnableWindow(FALSE);
	m_Old = "";
	m_UserName = "";
	m_bModify = FALSE;
	m_bNewUser = TRUE;
	UpdateData(FALSE);
	DestroyPhoto();
	Invalidate();
	
}

void CAccessPictureDlg::OnPrevioususer() 
{
	// TODO: Add your control notification handler code here
	if(m_bModify)///假如当前记录改动过,则保存
		OnSaveinfo();
	m_pRecordset->MovePrevious();
	if(m_pRecordset->BOF)
		m_pRecordset->MoveFirst();
	ReadData();
	ResetControls();
	Invalidate();
	
}

void CAccessPictureDlg::OnNextuser() 
{
	// TODO: Add your control notification handler code here
	if(m_bModify)///假如当前记录改动过,则保存
		OnSaveinfo();
	m_pRecordset->MoveNext();
	if(m_pRecordset->adoEOF)
		m_pRecordset->MoveLast();
	ReadData();
	ResetControls();
	Invalidate();
	
}

void CAccessPictureDlg::OnSelectphoto() 
{
	// TODO: Add your control notification handler code here
	static char BASED_CODE szFilter[] = "BMP Files (*.bmp)|*.bmp||";
	CFileDialog dlg(TRUE,"BMP",NULL,  OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	if(dlg.DoModal() == IDOK)
	{
		CString pathname = dlg.GetPathName();
		DestroyPhoto();
		LoadBMPFile(pathname);
		m_hPhotoBitmap = BufferToHBITMAP();
		m_bModify = TRUE;
		m_buttonSaveInfo.EnableWindow();
		Invalidate();
	}
	
}

void CAccessPictureDlg::OnDeleteuser() 
{
	// TODO: Add your control notification handler code here
	m_pRecordset->Delete(adAffectCurrent);
	if(m_pRecordset->adoEOF)
		m_pRecordset->MoveLast();
	ResetControls();
	m_buttonDeleteUser.EnableWindow(FALSE);
	ReadData();
	Invalidate();
	
}

void CAccessPictureDlg::OnSaveinfo() 
{
	// TODO: Add your control notification handler code here
	if(!m_bModify) return;
	UpdateData();
	if(m_pBMPBuffer == NULL || m_hPhotoBitmap == NULL || m_UserName == "" || m_Old == "")
	{
		AfxMessageBox("您没有提供完整的用户信息。\r\n这些信息包括:用户名、年龄、照片");
		return;
	}
	m_buttonSaveInfo.EnableWindow(FALSE);
	char			*pBuf = m_pBMPBuffer;
	VARIANT			varBLOB;
	SAFEARRAY		*psa;
    SAFEARRAYBOUND	rgsabound[1];

	if(m_bNewUser)
		m_pRecordset->AddNew();
	m_pRecordset->PutCollect("username",_variant_t(m_UserName));
	m_pRecordset->PutCollect("old",atol(m_Old));
	if(pBuf)
	{    
		rgsabound[0].lLbound = 0;
		rgsabound[0].cElements = m_nFileLen;
		psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
		for (long i = 0; i < (long)m_nFileLen; i++)
			SafeArrayPutElement (psa, &i, pBuf++);
		varBLOB.vt = VT_ARRAY | VT_UI1;
		varBLOB.parray = psa;
		m_pRecordset->GetFields()->GetItem("photo")->AppendChunk(varBLOB);
	}
	m_pRecordset->Update();
	m_buttonDeleteUser.EnableWindow();
	ResetControls();
	m_bNewUser = FALSE;
	m_bModify = FALSE;
	
}

void CAccessPictureDlg::OnChangeUsername() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
		m_bModify = TRUE;
	m_buttonSaveInfo.EnableWindow();
	
}

void CAccessPictureDlg::OnChangeOld() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	m_bModify = TRUE;
	m_buttonSaveInfo.EnableWindow();
	
}

⌨️ 快捷键说明

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