📄 imagedlg.cpp
字号:
// ImageDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Image.h"
#include "ImageDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CImageDlg dialog
CImageDlg::CImageDlg(CWnd* pParent /*=NULL*/)
: CDialog(CImageDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CImageDlg)
// 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 CImageDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CImageDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CImageDlg, CDialog)
//{{AFX_MSG_MAP(CImageDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDB_ADDBUTTON, OnAddbutton)
ON_BN_CLICKED(IDB_SHOW, OnShow)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImageDlg message handlers
BOOL CImageDlg::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
InitDataBase();
return TRUE; // return TRUE unless you set the focus to a control
}
void CImageDlg::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 CImageDlg::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 CImageDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CImageDlg::InitDataBase()
{
CString strSql;
HRESULT hr;
hr = m_pConnection.CreateInstance("ADODB.Connection");
if (SUCCEEDED(hr))
{
strSql = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb";
m_pConnection->ConnectionTimeout = 5;
hr = m_pConnection->Open(_bstr_t(strSql),"","",adModeUnknown);
if (FAILED(hr))
{
MessageBox("Open database failed!!");
}
else
{
hr = m_pRecordset.CreateInstance("ADODB.RECORDSET");
if (SUCCEEDED(hr))
{
strSql = "Select * from Info";
hr = m_pRecordset->Open(_variant_t(strSql),_variant_t((IDispatch *)(m_pConnection)),
adOpenStatic,adLockOptimistic,adCmdText);
if (FAILED(hr))
{
MessageBox("recordset open failed");
return;
}
}
else
{
MessageBox("Recordset CreateInstance failed!!");
}
}
}
else
{
MessageBox("Connection CreateInstance failed!!");
}
}
void CImageDlg::OnAddbutton()
{
this->m_pRecordset->AddNew();
CTime time = CTime::GetCurrentTime();
CString strTemp;
strTemp = time.Format("%H:%M:%S");
m_pRecordset->PutCollect(_variant_t("No"),_variant_t(strTemp));
CFile file;
file.Open("E:\\photo\\11.bmp",CFile::modeRead);
BYTE* lp = (BYTE *) malloc(file.GetLength());
file.Read(lp,file.GetLength());
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = file.GetLength();
SAFEARRAY *sa = SafeArrayCreate(VT_UI1,1,rgsabound);
for (long i = 0;i< (long)file.GetLength();i++)
{
SafeArrayPutElement(sa,&i,lp++);
}
for ( i = 0;i<(long) file.GetLength();i++)
{
lp--;
}
VARIANT varBLOB;
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = sa;
m_pRecordset->GetFields()->GetItem("Data")->AppendChunk(varBLOB);
m_pRecordset->Update();
file.Close();
free(lp);
SafeArrayDestroy(sa);
}
void CImageDlg::OnShow()
{
m_pRecordset->MoveFirst();
long nn = 0;
_variant_t index = (_variant_t)nn ;
_variant_t vartemp = m_pRecordset->GetCollect(index);
CString strTemp;
strTemp = (const char *)(_bstr_t)vartemp;
MessageBox(strTemp);
long nSize = m_pRecordset->GetFields()->GetItem("Data")->ActualSize;
/* _variant_t varTemp = m_pRecordset->GetFields()->GetItem("Data")->GetChunk(nSize);
LPBYTE lp = new BYTE[nSize+1];
char* lpBuffer;
SafeArrayAccessData(varTemp.parray,(void **)&lpBuffer);
memcpy(lp,lpBuffer,nSize);
SafeArrayUnaccessData(varTemp.parray);
*/
_variant_t varBLOB;
varBLOB = m_pRecordset->GetFields()->GetItem("Data")->GetChunk(nSize);
BYTE *pBuffer = (BYTE *)malloc(nSize); ///重新申请必要的存储空间
memset(pBuffer,0,nSize);
// varBLOB.parray=(SAFEARRAY *)::GlobalAlloc(GPTR,nSize);
char *pBuf = (char *)(::GlobalLock(varBLOB.parray)) ;
// SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(pBuffer,pBuf,nSize); ///复制数据到缓冲区m_pBMPBuffer
// SafeArrayUnaccessData (varBLOB.parray);
CFile file;
file.Open("E:\\test.bmp",CFile::modeCreate|CFile::modeWrite);
file.Write(pBuffer,nSize);
file.Close();
free(pBuffer);
/* */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -