📄 bmpsysdlg.cpp
字号:
// BMPSysDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BMPSys.h"
#include "BMPSysDlg.h"
#include "picture.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()
/////////////////////////////////////////////////////////////////////////////
// CBMPSysDlg dialog
CBMPSysDlg::CBMPSysDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBMPSysDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBMPSysDlg)
m_strName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBMPSysDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBMPSysDlg)
DDX_Control(pDX, IDC_BUTTON_PRE, m_btnPre);
DDX_Control(pDX, IDC_BUTTON_NEXT, m_btnNext);
DDX_Text(pDX, IDC_STATIC_NAME, m_strName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBMPSysDlg, CDialog)
//{{AFX_MSG_MAP(CBMPSysDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_PRE, OnButtonPre)
ON_BN_CLICKED(IDC_BUTTON_NEXT, OnButtonNext)
ON_BN_CLICKED(IDC_BUTTON_FIRST, OnButtonFirst)
ON_BN_CLICKED(IDC_BUTTON_LAST, OnButtonLast)
ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBMPSysDlg message handlers
BOOL CBMPSysDlg::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
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->ConnectionString="File Name=my_data9.udl";
m_pConnection->Open("","","",adModeUnknown);
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(_variant_t("tbBmp"),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdTable);
}
catch (_com_error e)
{
AfxMessageBox("数据库连接失败!");
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CBMPSysDlg::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 CBMPSysDlg::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 CBMPSysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBMPSysDlg::OnButtonPre()
{
m_pRecordset->MovePrevious();
if(m_pRecordset->BOF)
{
m_btnPre.EnableWindow(FALSE);
m_pRecordset->MoveNext();
ShowPic();
return;
}
else {
ShowPic();
}
m_btnNext.EnableWindow(TRUE);
}
void CBMPSysDlg::OnButtonNext()
{
m_pRecordset->MoveNext();
if(m_pRecordset->adoEOF)
{
m_pRecordset->MovePrevious();
m_btnNext.EnableWindow(FALSE);
ShowPic();
}
else {
ShowPic();
}
m_btnPre.EnableWindow(TRUE);
}
void CBMPSysDlg::OnButtonFirst()
{
if(m_pRecordset!=NULL)
{
m_pRecordset->MoveFirst();
ShowPic();
}
m_btnPre.EnableWindow(FALSE);
}
void CBMPSysDlg::OnButtonLast()
{
if(m_pRecordset!=NULL)
{
m_pRecordset->MoveLast();
ShowPic();
}
m_btnNext.EnableWindow(FALSE);
}
/*
void CBMPSysDlg::OnButtonReset()
{
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 m_strPathName = dlg.GetPathName();
if (m_strPathName != "")
{ MessageBox(m_strPathName);
AppendChunkF(m_strPathName);
}
}
}*/
void CBMPSysDlg::OnButtonNew()
{
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 m_strPathName = dlg.GetPathName();
if (m_strPathName != "")
{
CFile file;
if (file.Open(m_strPathName, CFile::modeRead))
{
long length = (long)file.GetLength();
BYTE *pbuf = new BYTE[length];
if (pbuf != NULL && file.Read(pbuf, length) == (DWORD)length)
{ m_pRecordset->AddNew();
m_pRecordset->Fields->GetItem(long(1))->PutValue(_variant_t(dlg.GetFileName()));
MyAppendChunk(FieldPtr(m_pRecordset->GetFields()->GetItem(_bstr_t("Bmp"))), pbuf, length);
m_pRecordset->Update();
}
if (pbuf != NULL) delete[] pbuf;
file.Close();
}
}
}
}
void CBMPSysDlg::ShowPic()
{
if(m_pRecordset==NULL)
return;
_variant_t var;
CString strValue;
var=m_pRecordset->Fields->GetItem(_bstr_t("Name"))->GetValue();
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_strName=strValue;
UpdateData(FALSE);
//获得图片数据,显示图片
long size = m_pRecordset->Fields->GetItem(_bstr_t("Bmp"))->ActualSize;
if ( size > 0)
{
BYTE *lpData = new BYTE[size];
if (MyGetChunk(m_pRecordset->GetFields()->GetItem("Bmp"), (LPVOID)lpData))
{
CPicture m_Pic;
m_Pic.LoadPictureData(lpData,size);
CClientDC dc(this);
m_Pic.UpdateSizeOnDC(&dc);
m_Pic.Show(&dc, CRect(170,100,170+200,100+200) );
}
delete[] lpData;
lpData = NULL;
}
else if(size==0) MessageBox("该记录为空!");
}
BOOL CBMPSysDlg::MyAppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes)
{
SAFEARRAY FAR *pSafeArray = NULL;
SAFEARRAYBOUND rgsabound[1];
try
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nBytes;
pSafeArray = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)nBytes; i++)
{
UCHAR &chData = ((UCHAR*)lpData)[i];
HRESULT hr = SafeArrayPutElement(pSafeArray, &i, &chData);
if (FAILED(hr)) return FALSE;
}
_variant_t varChunk;
varChunk.vt = VT_ARRAY | VT_UI1;
varChunk.parray = pSafeArray;
return (pField->AppendChunk(varChunk) == S_OK);
}
catch (_com_error &e)
{
TRACE(_T("Warning: AppendChunk 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
return FALSE;
}
}
BOOL CBMPSysDlg::MyGetChunk(FieldPtr pField, LPVOID lpData)
{
ASSERT(pField != NULL);
ASSERT(lpData != NULL);
UCHAR chData;
long index = 0;
while (index < pField->ActualSize)
{
try
{
_variant_t varChunk = pField->GetChunk(pField->ActualSize);
if (varChunk.vt != (VT_ARRAY | VT_UI1))
{
return FALSE;
}
for (long i = 0; i < pField->ActualSize; i++)
{
if (SUCCEEDED( SafeArrayGetElement(varChunk.parray, &i, &chData) ))
{
((UCHAR*)lpData)[index] = chData;
index++;
}
else
{
break;
}
}
}
catch (_com_error e)
{
TRACE(_T("Warning: GetChunk 方法发生异常. 错误信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
return FALSE;
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -