📄 photoinsertdlg.cpp
字号:
// PHOTOINSERTDlg.cpp : implementation file
//
#include "stdafx.h"
#include "PHOTOINSERT.h"
#include "PHOTOINSERTDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CPHOTOINSERTDlg dialog
CPHOTOINSERTDlg::CPHOTOINSERTDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPHOTOINSERTDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPHOTOINSERTDlg)
m_old = _T("");
m_user = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hPhotoBitmap = NULL;
m_pBMPBuffer = NULL;
}
void CPHOTOINSERTDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPHOTOINSERTDlg)
DDX_Text(pDX, IDC_EDIT_AGE, m_old);
DDX_Text(pDX, IDC_EDIT_USER, m_user);
DDV_MaxChars(pDX, m_user, 20);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPHOTOINSERTDlg, CDialog)
//{{AFX_MSG_MAP(CPHOTOINSERTDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SelPic, OnBUTTONSelPic)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON_NEXT, OnButtonNext)
ON_BN_CLICKED(IDC_BUTTON_PRE, OnButtonPre)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPHOTOINSERTDlg message handlers
BOOL CPHOTOINSERTDlg::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_hPhotoBitmap = NULL;
m_pBMPBuffer = NULL;
if(CoInitialize(NULL)!=0)
{
AfxMessageBox("初始化COM支持库失败!");
return false;
}
myFun.ConnDB();
CString sqlString;
sqlString.Format("select * from FGBKZK.EXPERIMENTPICTURE");
myFun.m_pRecord.CreateInstance("ADODB.Recordset");
HRESULT hr = myFun.m_pRecord->Open(_variant_t(sqlString),_variant_t((IDispatch *)myFun.m_pConn,true),adOpenDynamic,adLockPessimistic,adCmdText);
if(SUCCEEDED(hr))
{
setData();
}
bAdd = FALSE;
return TRUE; // return TRUE unless you set the focus to a control
}
void CPHOTOINSERTDlg::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 CPHOTOINSERTDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
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(20,20,&dc);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPHOTOINSERTDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
HBITMAP CPHOTOINSERTDlg::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) ('M' << 8) | 'B')) return NULL;
hDIB = lpBuffer + bmfHeaderLen;
BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ;
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
int nColors = bmiHeader.biClrUsed ? bmiHeader.biClrUsed : 1 << bmiHeader.biBitCount;
if( bmInfo.bmiHeader.biBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
else
lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
CClientDC dc(this);
hBmp = CreateDIBitmap(dc.m_hDC,&bmiHeader,CBM_INIT,lpDIBBits,&bmInfo,DIB_RGB_COLORS);
return hBmp;
}
void CPHOTOINSERTDlg::OnBUTTONSelPic()
{
// TODO: Add your control notification handler code here
static char BASED_CODE szFilter[] = "BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*||";
CFileDialog dlg(TRUE,"BMP",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(dlg.DoModal() == IDOK)
{
CString pathname = dlg.GetPathName();
DestroyPhoto();
LoadBMPFile(pathname);
m_hPhotoBitmap = BufferToHBITMAP();
Invalidate();
}
}
void CPHOTOINSERTDlg::DestroyPhoto()
{
if(m_hPhotoBitmap)
{
DeleteObject(m_hPhotoBitmap);
m_hPhotoBitmap = NULL;
}
if(m_pBMPBuffer)
{
delete m_pBMPBuffer;
m_pBMPBuffer = NULL;
}
}
BOOL CPHOTOINSERTDlg::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 CPHOTOINSERTDlg::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,640,480,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(OldBitmap);
}
void CPHOTOINSERTDlg::setData()
{
DestroyPhoto(); ///清除原图像
if(myFun.m_pRecord->adoEOF||myFun.m_pRecord->BOF)
{
m_user.Empty();
m_old.Empty();
UpdateData(FALSE);
return;
}
long lDataSize = myFun.m_pRecord->GetFields()->GetItem("picture")->ActualSize;
if(lDataSize > 0)
{
_variant_t varBLOB;
varBLOB = myFun.m_pRecord->GetFields()->GetItem("picture")->GetChunk(lDataSize);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
if(m_pBMPBuffer = new char[lDataSize+1]) ///重新分配必要的存储空间
{
char *pBuf = NULL;
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(m_pBMPBuffer,pBuf,lDataSize); ///复制数据到缓冲区m_pBMPBuffer
SafeArrayUnaccessData (varBLOB.parray);
m_nFileLen = lDataSize;
m_hPhotoBitmap = BufferToHBITMAP(); ///生成BITMAP对象
}
}
}
CString user = myFun.m_pRecord->GetCollect(long(0)).bstrVal;
m_user = user;
CString strAge;
strAge.Format("%d",myFun.m_pRecord->GetCollect(long(1)).iVal);
m_old = strAge;
UpdateData(FALSE);
}
void CPHOTOINSERTDlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_user = "";
m_old = "";
UpdateData(FALSE);
DestroyPhoto();
Invalidate();
bAdd = TRUE;
}
void CPHOTOINSERTDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
//if(!m_bModify) return;
BeginWaitCursor();
UpdateData();
if(m_pBMPBuffer == NULL || m_hPhotoBitmap == NULL)
{
AfxMessageBox("您没有提供完整的用户信息。\r\n这些信息包括:用户名、年龄、照片");
return;
}
char *pBuf = m_pBMPBuffer;
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
if(bAdd)
myFun.m_pRecord->AddNew();
myFun.m_pRecord->PutCollect("PICTUREID",_variant_t("2007-10-08 20:00:00"));
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+i);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
_StreamPtr pStream = NULL;
pStream.CreateInstance(__uuidof(Stream));
variant_t varOptional(DISP_E_PARAMNOTFOUND,VT_ERROR);
pStream->PutType(adTypeBinary);
pStream->Open(varOptional, adModeUnknown, adOpenStreamUnspecified, _bstr_t(), _bstr_t());
pStream->Write(varBLOB);
//pStream->SaveToFile("d:\\temp.bmp", adSaveCreateNotExist);
/********************************************************************/
pStream->SaveToFile("d:\\temp.bmp",adSaveCreateOverWrite); //去掉这句,就不能成功地将数据插入到数据库!!!!!!
/********************************************************************/
myFun.m_pRecord->GetFields()->GetItem("PICTURE")->Value = pStream->Read(m_nFileLen);
}
myFun.m_pRecord->Update();
bAdd = FALSE;
EndWaitCursor();
}
void CPHOTOINSERTDlg::OnButtonNext()
{
// TODO: Add your control notification handler code here
myFun.m_pRecord->MoveNext();
if(myFun.m_pRecord->adoEOF)
myFun.m_pRecord->MoveLast();
setData();
Invalidate();
}
void CPHOTOINSERTDlg::OnButtonPre()
{
// TODO: Add your control notification handler code here
myFun.m_pRecord->MovePrevious();
if(myFun.m_pRecord->BOF)
myFun.m_pRecord->MoveFirst();
setData();
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -