📄 picmanage.cpp
字号:
// PicManage.cpp : implementation file
//
#include "stdafx.h"
#include "PIM.h"
#include "PicManage.h"
#include <olestd.h>
#include <sys/stat.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPicManage dialog
CPicManage::CPicManage(CWnd* pParent /*=NULL*/)
: CDialog(CPicManage::IDD, pParent)
{
//{{AFX_DATA_INIT(CPicManage)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CPicManage::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPicManage)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPicManage, CDialog)
//{{AFX_MSG_MAP(CPicManage)
ON_BN_CLICKED(IDC_INPUTPIC, OnInputpic)
ON_BN_CLICKED(IDC_BROWSEPIC, OnBrowsepic)
ON_BN_CLICKED(IDC_PREVIOUPIC, OnPrevioupic)
ON_BN_CLICKED(IDC_NEXTPIC, OnNextpic)
ON_BN_CLICKED(IDC_DELETEPIC, OnDeletepic)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPicManage message handlers
void CPicManage::OnInputpic()
{
// TODO: Add your control notification handler code here
isBrowse = FALSE;
m_pFilename==_T("");
CFileDialog dlg(TRUE,NULL, NULL, OFN_HIDEREADONLY,\
"jpg Files (*.jpg)|*.jpg",NULL);
if(dlg.DoModal()!=IDOK)
{
AfxMessageBox("请选择所要录入的图像文件");
return;
}
m_pFilename=dlg.GetPathName();
if(m_pFilename==_T(""))
{
AfxMessageBox("请选择所要录入的图像文件");
return;
}
FILE *fp = NULL;
unsigned char *f_buf;
FieldPtr imfield;
char *path;
path=(LPSTR)(LPCTSTR)m_pFilename;
//open the file of the picture that you want to save to the database
fp = fopen(path,"rb");
if(NULL == fp)
{
return ;
}
//obtain the information of the picture
struct stat file_stat;
if(-1 == fstat(fileno(fp), &file_stat))
{
fclose(fp);
return ;
}
f_buf = (unsigned char *)malloc(sizeof(unsigned char)*file_stat.st_size);
if(NULL == f_buf)
{
fclose(fp);
return;
}
//read the file to the memory
long file_size = fread(f_buf,1,file_stat.st_size,fp);
fclose(fp);
if(file_size != file_stat.st_size)
{
delete f_buf;
return;
}
_variant_t strQuery;
// open the table of the datebase
if (theApp.m_pADOSet->State != adStateClosed)
{
theApp.m_pADOSet->Close();
}
strQuery="select * from 相片";
theApp.ADOExecute(theApp.m_pADOSet,strQuery);
ASSERT(theApp.m_pADOSet != NULL);
//add a new record to the picture table
try
{
if (theApp.m_pADOSet!= NULL)
{
theApp.m_pADOSet->AddNew();
// AfxMessageBox("aaa");
}
}
catch (_com_error e)
{
TRACE(_T("Warning: AddNew 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
// return FALSE;
}
// get the field of the record
try
{
imfield=theApp.m_pADOSet->GetFields()->GetItem("相片");
if(imfield==NULL)
{
AfxMessageBox("fail");
}
}
catch (_com_error e)
{
TRACE(_T("Warning: GetField发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
return;
}
//put the data of the momery into the SAFEARRAY defined for a safe array varies
SAFEARRAY FAR *pSafeArray = NULL;
SAFEARRAYBOUND rgsabound[1];
try
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = file_size;
pSafeArray = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)file_size; i++)
{
UCHAR &chData = ((UCHAR*)f_buf)[i];
HRESULT hr = SafeArrayPutElement(pSafeArray, &i, &chData);
if (FAILED(hr)) return ;
}
_variant_t varChunk;
varChunk.vt = VT_ARRAY | VT_UI1;
varChunk.parray = pSafeArray;
//append picture data to the picture table
imfield->AppendChunk(varChunk);
}
catch (_com_error &e)
{
TRACE(_T("Warning: AppendChunk 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
// return FALSE;
}
// theApp.m_pADOSet->PutCollect("主题名称", "aa");
// update the record to succeed in modifying the table
AfxMessageBox("录入成功");
HWND m_StaticHwnd;
m_Picture.RemovePictures();
//HWND m_StaticHwnd = (CStatic*)GetDlgItem(IDC_SHOWPIC)->m_hWnd;
GetDlgItem(IDC_SHOWPIC,&m_StaticHwnd);
m_Picture.AddPicture(m_StaticHwnd,f_buf,file_size,0,0);
RECT rect;
GetDlgItem(IDC_SHOWPIC)->GetClientRect(&rect);
m_Picture.RepaintPictures(rect.right-rect.left,rect.bottom-rect.top);
theApp.m_pADOSet->Update();
theApp.m_pADOSet->Close();
}
void CPicManage::OnBrowsepic()
{
// TODO: Add your control notification handler code here
_variant_t strQuery;
// open the table of the datebase
int ilistrow;
if (theApp.m_pADOSet->State != adStateClosed)
{
theApp.m_pADOSet->Close();
}
// strQuery = "select * from 资料";
// theApp.ADOExecute(theApp.m_pADOSet, strQuery);
if (theApp.m_pADOSet->State != adStateClosed)
{
theApp.m_pADOSet->Close();
}
strQuery="select * from 相片";
theApp.ADOExecute(theApp.m_pADOSet,strQuery);
ASSERT(theApp.m_pADOSet != NULL);
int iCount=theApp.m_pADOSet->GetRecordCount();
if(iCount<=0)
{
//theApp.m_pADOSet->MoveFirst();
AfxMessageBox("数据库里没有图片");
return;
}
theApp.m_pADOSet->MoveFirst();
m_Picture.RemovePictures();
isBrowse = TRUE;
// AfxMessageBox("aaa");
try
{
long nSize = theApp.m_pADOSet->GetFields()->GetItem("相片")->ActualSize;
if(nSize > 0)
{
_variant_t varBLOB;
varBLOB = theApp.m_pADOSet->GetFields()->GetItem("相片")->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);
HWND m_StaticHwnd;
GetDlgItem(IDC_SHOWPIC,&m_StaticHwnd);
m_Picture.AddPicture(m_StaticHwnd,pBuffer,nSize,0,0);
RECT rect;
GetDlgItem(IDC_SHOWPIC)->GetClientRect(&rect);
m_Picture.RepaintPictures(rect.right-rect.left,rect.bottom-rect.top);
delete [] pBuffer;
pBuf=0;
}
}
}
}
catch (_com_error e) {
}
}
void CPicManage::OnPrevioupic()
{
// TODO: Add your control notification handler code here
if(isBrowse == FALSE)
{
return;
}
if(theApp.m_pADOSet->adoBOF)
return;
theApp.m_pADOSet->MovePrevious();
m_Picture.RemovePictures();
// AfxMessageBox("aaa");
try
{
long nSize = theApp.m_pADOSet->GetFields()->GetItem("相片")->ActualSize;
if(nSize > 0)
{
_variant_t varBLOB;
varBLOB = theApp.m_pADOSet->GetFields()->GetItem("相片")->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));
HWND m_StaticHwnd;
GetDlgItem(IDC_SHOWPIC,&m_StaticHwnd);
m_Picture.AddPicture(m_StaticHwnd,pBuffer,nSize,0,0);
RECT rect;
GetDlgItem(IDC_SHOWPIC)->GetClientRect(&rect);
m_Picture.RepaintPictures(rect.right-rect.left,rect.bottom-rect.top);
delete [] pBuffer;
pBuf=0;
///生成BITMAP对象
// CClientDC dc(this);
// m_Pic.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels
// m_Pic.Show(&dc, CRect(0,50,m_Pic.m_Width,50+m_Pic.m_Height) );
}
}
}
}
catch (_com_error e) {
}
}
void CPicManage::OnNextpic()
{
// TODO: Add your control notification handler code here
if(isBrowse == FALSE)
{
return;
}
if(theApp.m_pADOSet->adoEOF)
return;
theApp.m_pADOSet->MoveNext();
m_Picture.RemovePictures();
// AfxMessageBox("aaa");
try
{
long nSize = theApp.m_pADOSet->GetFields()->GetItem("相片")->ActualSize;
if(nSize > 0)
{
_variant_t varBLOB;
varBLOB = theApp.m_pADOSet->GetFields()->GetItem("相片")->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);
HWND m_StaticHwnd;
GetDlgItem(IDC_SHOWPIC,&m_StaticHwnd);
m_Picture.AddPicture(m_StaticHwnd,pBuffer,nSize,0,0);
RECT rect;
GetDlgItem(IDC_SHOWPIC)->GetClientRect(&rect);
m_Picture.RepaintPictures(rect.right-rect.left,rect.bottom-rect.top);
delete [] pBuffer;
pBuf=0;
}
}
}
}
catch (_com_error e) {
}
}
void CPicManage::OnDeletepic()
{
// TODO: Add your control notification handler code here
if(isBrowse == FALSE)
{
return;
}
if(theApp.m_pADOSet != NULL)
{
if(AfxMessageBox("你真要删除该图片吗?",MB_YESNO|MB_ICONQUESTION)!=IDYES)
{
return;
}
if(theApp.m_pADOSet->Delete(adAffectCurrent) == S_OK)
{
m_Picture.RemovePictures();
RECT rect;
GetDlgItem(IDC_SHOWPIC)->GetClientRect(&rect);
m_Picture.RepaintPictures(rect.right-rect.left,rect.bottom-rect.top);
AfxMessageBox("已经删除");
}
}
}
BOOL CPicManage::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
isBrowse = FALSE;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -