📄 picturedlg.cpp
字号:
// pictureDlg.cpp : implementation file
//
#include "stdafx.h"
#include "picture.h"
#include "pictureDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
static CString m_connect="";
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()
/////////////////////////////////////////////////////////////////////////////
// CPictureDlg dialog
CPictureDlg::CPictureDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPictureDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPictureDlg)
m_Sql = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_hPictureBitmap=NULL;
m_pBMPBuffer = NULL;
pado=NULL;
strPicturePath="";
bFlag = FALSE;
bStretchToFit=TRUE;
}
void CPictureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPictureDlg)
DDX_Text(pDX, IDC_EDIT_SQL, m_Sql);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPictureDlg, CDialog)
//{{AFX_MSG_MAP(CPictureDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_UploadPicture, OnUploadPicture)
ON_BN_CLICKED(ID_ChoosePicture, OnChoosePicture)
ON_BN_CLICKED(ID_DownloadPrePicture, OnDownloadPrePicture)
ON_BN_CLICKED(ID_DownloadNextPicture, OnDownloadNextPicture)
ON_BN_CLICKED(ID_ShowPicture, OnShowPicture)
ON_BN_CLICKED(IDC_RADIO_Stretch, OnRADIOStretch)
ON_BN_CLICKED(IDC_RADIO_Copy, OnRADIOCopy)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPictureDlg message handlers
BOOL CPictureDlg::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
SetWindowText("~~Visual C++--SQL--Picture~~");
CheckRadioButton(IDC_RADIO_Stretch, IDC_RADIO_Copy, IDC_RADIO_Stretch);
if(m_connect.GetLength()<=0)
{
m_connect="Provider=SQLOLEDB.1;Password=386153;Persist Security Info=True;User ID=sa;Initial Catalog=student;Data Source=(local)";
}
::CoInitialize(NULL);
Open();
if(m_pSet.CreateInstance("ADODB.Recordset")!=S_OK)
return false;
HRESULT hr = m_pSet->Open("SELECT * FROM picture ",pado.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
if(SUCCEEDED(hr))
{
ReadData();
}
ResetControls();
return TRUE; // return TRUE unless you set the focus to a control
}
void CPictureDlg::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 CPictureDlg::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
{
if(bFlag)
DrawPicture(30,30,&dc);
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPictureDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPictureDlg::OnUploadPicture()
{
// TODO: Add your control notification handler code here
UpdateData(true);
CFile fp;
if(!fp.Open(strPicturePath,CFile::modeRead))
{
m_pSet->Close();
m_pSet->Release();
return;
}
DWORD len=fp.GetLength();
BYTE* bt=new BYTE[len];
if(bt==NULL)
{
fp.Close();
m_pSet->Close();
m_pSet->Release();
return;
}
fp.ReadHuge(bt,len);//可从文件的当前位置读取大于64K数据
fp.Close();
m_pSet->AddNew();
m_pSet->PutCollect("serial",_variant_t(m_Sql));
//SAFEARRAY 结构类型
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = len;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long k = 0; k < (long)len; k++)
SafeArrayPutElement (psa, &k, bt++);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
//BLOB数据的读取:
//对应于保存数据时我们所使用的AppendChunk函数,读取数据应该使用GetChunk函数
m_pSet->GetFields()->GetItem("pic")->AppendChunk(varBLOB);
m_pSet->Update();
SafeArrayDestroy(psa);
UpdateData(false);
AfxMessageBox("上传图片至SQL成功");
return;
}
void CPictureDlg::Open()
{
try
{
pado.CreateInstance(__uuidof(Connection));
pado->Open((const char *)m_connect,"","",-1);
}
catch(...)
{
AfxMessageBox("打开数据库失败");
return;
}
return;
}
void CPictureDlg::OnChoosePicture()
{
// TODO: Add your control notification handler code here
UpdateData(true);
static char BASED_CODE szFilter[]="bmp style(*.bmp)|*.bmp||";
CFileDialog fd(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter,NULL);
if(fd.DoModal()==IDOK)
strPicturePath=fd.GetPathName();
else
{
MessageBox("请选择图片的正确地址","选择失败",MB_OK);
return;
}
UpdateData(false);
return;
}
//在屏幕上显示图像
void CPictureDlg::DrawPicture(int x, int y, CDC *pDC)
{
if(!m_hPictureBitmap) return;
HBITMAP OldBitmap;
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
OldBitmap=(HBITMAP)MemDC.SelectObject(m_hPictureBitmap);
//拉伸显示
if(bStretchToFit)
{
//取得BMP图像的Width与Height
CBitmap bmpImage;
bmpImage.Attach (m_hPictureBitmap);
BITMAP bmpInfo;
bmpImage.GetBitmap(&bmpInfo);
CSize pSize = CSize(bmpInfo.bmWidth, bmpInfo.bmHeight );
pDC->StretchBlt(x,y,100,130,&MemDC,0,0,pSize.cx,pSize.cy,SRCCOPY);
MemDC.SelectObject(OldBitmap);
}
else
{
pDC->BitBlt(x,y,100,130,&MemDC,0,0,SRCCOPY);
}
}
//清除分配的对象
void CPictureDlg::DestroyPicture()
{
if(m_hPictureBitmap)
{
DeleteObject(m_hPictureBitmap);
m_hPictureBitmap = NULL;
}
if(m_pBMPBuffer)
{
delete m_pBMPBuffer;
m_pBMPBuffer = NULL;
}
}
void CPictureDlg::OnDownloadPrePicture()
{
// TODO: Add your control notification handler code here
m_pSet->MovePrevious();
if(m_pSet->BOF)
m_pSet->MoveFirst();
ReadData();
ResetControls();
Invalidate(true);
}
void CPictureDlg::OnDownloadNextPicture()
{
// TODO: Add your control notification handler code here
m_pSet->MoveNext();
if(m_pSet->adoEOF)
m_pSet->MoveLast();
ReadData();
ResetControls();
Invalidate(true);
}
//读取记录集当前记录中数据,包括图像数据
void CPictureDlg::ReadData()
{
UpdateData(true);
DestroyPicture();//清除原图像
if(m_pSet->adoEOF||m_pSet->BOF)
{
AfxMessageBox("无图片");
return;
}
long lDataSize = m_pSet->GetFields()->GetItem("pic")->ActualSize;
if(lDataSize > 0)
{
_variant_t varBLOB;
varBLOB = m_pSet->GetFields()->GetItem("pic")->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_hPictureBitmap = BufferToHBITMAP(); ///生成BITMAP对象
}
}
}
m_Sql = m_pSet->GetCollect("serial").bstrVal;
UpdateData(false);
}
//将内存中的BMP文件内容转换到HBITMAP
HBITMAP CPictureDlg::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;
}
BOOL CPictureDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
m_pSet->Close();
bFlag = FALSE;
return CDialog::DestroyWindow();
}
void CPictureDlg::OnShowPicture()
{
// TODO: Add your control notification handler code here
UpdateData(true);
bFlag=true;
Invalidate(true);
GetDlgItem(ID_ShowPicture)->EnableWindow(false);
GetDlgItem(IDC_RADIO_Stretch)->EnableWindow(false);
GetDlgItem(IDC_RADIO_Copy)->EnableWindow(false);
UpdateData(false);
}
void CPictureDlg::ResetControls()
{
BOOL IsFirstRecord = FirstRecord();
BOOL IsLastRecord = LastRecord();
GetDlgItem(ID_DownloadPrePicture)->EnableWindow(!IsFirstRecord);
GetDlgItem(ID_DownloadNextPicture)->EnableWindow(!IsLastRecord);
}
//判断是否是最后一个记录
BOOL CPictureDlg::LastRecord()
{
if(m_pSet->adoEOF)
{
return TRUE;
}
m_pSet->MoveNext();
if(m_pSet->adoEOF)
{
m_pSet->MovePrevious();
return TRUE;
}
else
{
m_pSet->MovePrevious();
return FALSE;
}
}
//判断是否是第一个记录
BOOL CPictureDlg::FirstRecord()
{
if(m_pSet->BOF)
{
return TRUE;
}
m_pSet->MovePrevious();
if(m_pSet->BOF)
{
m_pSet->MoveFirst();
return TRUE;
}
else
{
m_pSet->MoveNext();
return FALSE;
}
}
void CPictureDlg::OnRADIOStretch()
{
// TODO: Add your control notification handler code here
bStretchToFit=TRUE;
Invalidate(true);
}
void CPictureDlg::OnRADIOCopy()
{
// TODO: Add your control notification handler code here
bStretchToFit=FALSE;
Invalidate(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -