VC中图像控件的用法与总结
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
int nHeight=MulDiv(hmHeight,pDC->GetDeviceCaps(LOGPIXELSX),2540);
//在内存中画
CDC MenDC;
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC,nWidth,nHeight);
MenDC.CreateCompatibleDC(NULL);
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
IPicture *pPic;//定义显示图片的接口(可显示jpg格式图片)
CString m_sPath="1.BMP";//设置想要显示的图片
//打开图象文件,获取文件字节数
if(file.Open(m_sPath,CFile::modeRead) && file.GetStatus(m_sPath,fstatus)
使用picture控件动态显示图片方法
一控件适应于图片尺寸
添加一个picture控件,修改ID为IDC-PICTURE,并添加一CBitmap m_bmp成员变量
在OnInitDialog()函数中添加如下代码
if(m_bmp.m_hObject!=NULL) m_bmp.DeleteObject();
//获得加载位图的文件信息
m_bmp.Attach(hbmp);
DIBSECTION ds;
BITMAPINFOHEADER &bminfo=ds.dsBmih;//获得位图信息图
m_bmp.GetObject(sizeof(ds),&ds);
int cx=bminfo.biWidth;//获得位图宽度
}
return true;
}
然后在OnPaint()函数中显示
void CDialogDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
//以绝对路径加载BMP图片
HBITMAP hbmp=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"1.bmp",IMAGE_BITMAP,
0,0,LR_CREATEDIBSECTION | LR_LOADFROMFILE);
if(hbmp==NULL) return false;
GetDlgItem(IDC_PICTRUE)->MoveWindow(rect.left,rect.top,cx,cy,true);//调整控件大小
在OnPaint()函数中添加如下代码
//三种方法获得pictrue控件DC,图象将被绘制在控件上
CPaintDC dc(GetDlgItem(IDC_PICTRUE));
void *pData=GlobalLock(hGlobal);//定义图象数据指针
memcpy(pData,pBuff,nSize);
GlobalUnlock(hGlobal);//释放内存空间
IStream *pStream=NULL;//定义数据流指针
//创建数据流
if(CreateStreamOnHGlobal(hGlobal,TRUE,&pStream)==S_OK)
pPic->Release();
}
图片显示通用模块
首先为类添加如下两个函数和一个成员变量IPicture* m_pPicture;
//图片加载函数
HRESULTLoadFromBuffer(BYTE *pBuff, int nSize)
{
bool bResult=false;
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,nSize);//分配内存空间
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CString filename;
filename="1.bmp";
DrawFromFile(filename,&dc);
int cy=bminfo.biHeight;//获得位图高度
//该段程序根据图象的宽和高,调整控件大小,让它正好显示一张图片
CRect rect;
GetDlgItem(IDC_PICTRUE)->GetWindowRect(&rect);//获得控件所在矩形区域
ScreenToClient(&rect);
// 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;
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc,rcclient.Width(),rcclient.Height());
memdc.SelectObject(&bitmap);
CWnd::DefWindowProc(WM_PAINT,(WPARAM)memdc.m_hDC,0);
在OnPaint()函数中添加如下代码
CWnd *pWnd=GetDlgItem(IDC_PICTRUE);//获得pictrue控件窗口的句柄
CRect rect;
pWnd->GetClientRect(&rect);//获得pictrue控件所在的举行区域
CDC *pDC=pWnd->GetDC();//获得pictrue控件的DC
&& ((cb=fstatus.m_size)!=-1))
{
//分配全局存储空间
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,cb);
LPVOID pvData=NULL;
if(hGlobal!=NULL)
{
//锁定分配内存块
if((pvData=GlobalLock(hGlobal))!=NULL)
{
//读入文件到内存缓冲区
file.ReadHuge(pvData,cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,true,&pStm);
}
}
}
//装载图象文件
if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
CDialog::OnPaint();
}
}
MenDC.SelectObject(&bmp);
m_pPicture->Render(MenDC.m_hDC,0,0,nWidth,nHeight,0,hmHeight,hmWidth,-hmHeight,NULL);
//将内存中画拷贝到目的地
pDC->BitBlt(0,0,nWidth,nHeight,&MenDC,0,0,SRCCOPY);
//CPaintDC dc(this);
//CDC dc;
//dc.m_hDC=::GetDC(NULL);
CRect rcclient;
GetDlgItem(IDC_PICTRUE)->GetClientRect(&rcclient);
CDC memdc;
memdc.CreateCompatibleDC(&dc);
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_XSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);//获得图象真实宽度
pPic->get_Height(&hmHeight);//获得图象真实高度
//在控件上显示图片pPic->Render(*pDC,0,0,rect.Width(),rect.Height(),0,hmHeight,hmWidth,-hmHeight,NULL);
{百度文库
BYTE *pBuff=new BYTE[cFile.GetLength()];//定义分配内存
if(cFile.Read(pBuff,cFile.GetLength())>0)//将文件读入缓存
LoadFromBuffer(pBuff,cFile.GetLength());
delete []pBuff;//释放缓存
dc.BitBlt(rcclient.left,rcclient.top,rcclient.Width(),rcclient.Height(),&memdc,
rcclient.left,rcclient.top,SRCPAINT);
brush.DeleteObject();
二图片适用于控件尺寸
添加一个picture控件,修改ID为IDC-PICTURE
CDC maskdc;
maskdc.CreateCompatibleDC(&dc);
CBitmap maskbitmap;
maskbitmap.CreateBitmap(rcclient.Width(),rcclient.Height(),1,1,NULL);
maskdc.SelectObject(&maskbitmap);
{
HRESULT hr;//定义返回结果
if((hr=OleLoadPicture(pStream,nSize,FALSE,IID_IPicture,(LPVOID*)&m_pPicture))==S_OK)
bResult=true;
pStream->Release();//释放数据流
}
return bResult;
}
//图片显示函数
BOOLDrawFromFile(CString filename, CDC *pDC)
{
CFile cFile;
CFileException e;
//打开文件
if(cFile.Open(filename,CFile::modeRead | CFile::typeBinary,&e))
long hmWidth,hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight);//获取图片尺寸
int nWidth=MulDiv(hmWidth,pDC->GetDeviceCaps(LOGPIXELSX),2540);
maskdc.BitBlt(0,0,rcclient.Width(),rcclient.Height(),&memdc,rcclient.left,rcclient.top,SRCCOPY);
CBrush brush;
brush.CreatePatternBrush(&m_bmp);
dc.FillRect(rcclient,&brush);
//在内存中画
CDC MenDC;
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC,nWidth,nHeight);
MenDC.CreateCompatibleDC(NULL);
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
IPicture *pPic;//定义显示图片的接口(可显示jpg格式图片)
CString m_sPath="1.BMP";//设置想要显示的图片
//打开图象文件,获取文件字节数
if(file.Open(m_sPath,CFile::modeRead) && file.GetStatus(m_sPath,fstatus)
使用picture控件动态显示图片方法
一控件适应于图片尺寸
添加一个picture控件,修改ID为IDC-PICTURE,并添加一CBitmap m_bmp成员变量
在OnInitDialog()函数中添加如下代码
if(m_bmp.m_hObject!=NULL) m_bmp.DeleteObject();
//获得加载位图的文件信息
m_bmp.Attach(hbmp);
DIBSECTION ds;
BITMAPINFOHEADER &bminfo=ds.dsBmih;//获得位图信息图
m_bmp.GetObject(sizeof(ds),&ds);
int cx=bminfo.biWidth;//获得位图宽度
}
return true;
}
然后在OnPaint()函数中显示
void CDialogDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
//以绝对路径加载BMP图片
HBITMAP hbmp=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),"1.bmp",IMAGE_BITMAP,
0,0,LR_CREATEDIBSECTION | LR_LOADFROMFILE);
if(hbmp==NULL) return false;
GetDlgItem(IDC_PICTRUE)->MoveWindow(rect.left,rect.top,cx,cy,true);//调整控件大小
在OnPaint()函数中添加如下代码
//三种方法获得pictrue控件DC,图象将被绘制在控件上
CPaintDC dc(GetDlgItem(IDC_PICTRUE));
void *pData=GlobalLock(hGlobal);//定义图象数据指针
memcpy(pData,pBuff,nSize);
GlobalUnlock(hGlobal);//释放内存空间
IStream *pStream=NULL;//定义数据流指针
//创建数据流
if(CreateStreamOnHGlobal(hGlobal,TRUE,&pStream)==S_OK)
pPic->Release();
}
图片显示通用模块
首先为类添加如下两个函数和一个成员变量IPicture* m_pPicture;
//图片加载函数
HRESULTLoadFromBuffer(BYTE *pBuff, int nSize)
{
bool bResult=false;
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,nSize);//分配内存空间
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CString filename;
filename="1.bmp";
DrawFromFile(filename,&dc);
int cy=bminfo.biHeight;//获得位图高度
//该段程序根据图象的宽和高,调整控件大小,让它正好显示一张图片
CRect rect;
GetDlgItem(IDC_PICTRUE)->GetWindowRect(&rect);//获得控件所在矩形区域
ScreenToClient(&rect);
// 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;
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc,rcclient.Width(),rcclient.Height());
memdc.SelectObject(&bitmap);
CWnd::DefWindowProc(WM_PAINT,(WPARAM)memdc.m_hDC,0);
在OnPaint()函数中添加如下代码
CWnd *pWnd=GetDlgItem(IDC_PICTRUE);//获得pictrue控件窗口的句柄
CRect rect;
pWnd->GetClientRect(&rect);//获得pictrue控件所在的举行区域
CDC *pDC=pWnd->GetDC();//获得pictrue控件的DC
&& ((cb=fstatus.m_size)!=-1))
{
//分配全局存储空间
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,cb);
LPVOID pvData=NULL;
if(hGlobal!=NULL)
{
//锁定分配内存块
if((pvData=GlobalLock(hGlobal))!=NULL)
{
//读入文件到内存缓冲区
file.ReadHuge(pvData,cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,true,&pStm);
}
}
}
//装载图象文件
if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
CDialog::OnPaint();
}
}
MenDC.SelectObject(&bmp);
m_pPicture->Render(MenDC.m_hDC,0,0,nWidth,nHeight,0,hmHeight,hmWidth,-hmHeight,NULL);
//将内存中画拷贝到目的地
pDC->BitBlt(0,0,nWidth,nHeight,&MenDC,0,0,SRCCOPY);
//CPaintDC dc(this);
//CDC dc;
//dc.m_hDC=::GetDC(NULL);
CRect rcclient;
GetDlgItem(IDC_PICTRUE)->GetClientRect(&rcclient);
CDC memdc;
memdc.CreateCompatibleDC(&dc);
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_XSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);//获得图象真实宽度
pPic->get_Height(&hmHeight);//获得图象真实高度
//在控件上显示图片pPic->Render(*pDC,0,0,rect.Width(),rect.Height(),0,hmHeight,hmWidth,-hmHeight,NULL);
{百度文库
BYTE *pBuff=new BYTE[cFile.GetLength()];//定义分配内存
if(cFile.Read(pBuff,cFile.GetLength())>0)//将文件读入缓存
LoadFromBuffer(pBuff,cFile.GetLength());
delete []pBuff;//释放缓存
dc.BitBlt(rcclient.left,rcclient.top,rcclient.Width(),rcclient.Height(),&memdc,
rcclient.left,rcclient.top,SRCPAINT);
brush.DeleteObject();
二图片适用于控件尺寸
添加一个picture控件,修改ID为IDC-PICTURE
CDC maskdc;
maskdc.CreateCompatibleDC(&dc);
CBitmap maskbitmap;
maskbitmap.CreateBitmap(rcclient.Width(),rcclient.Height(),1,1,NULL);
maskdc.SelectObject(&maskbitmap);
{
HRESULT hr;//定义返回结果
if((hr=OleLoadPicture(pStream,nSize,FALSE,IID_IPicture,(LPVOID*)&m_pPicture))==S_OK)
bResult=true;
pStream->Release();//释放数据流
}
return bResult;
}
//图片显示函数
BOOLDrawFromFile(CString filename, CDC *pDC)
{
CFile cFile;
CFileException e;
//打开文件
if(cFile.Open(filename,CFile::modeRead | CFile::typeBinary,&e))
long hmWidth,hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight);//获取图片尺寸
int nWidth=MulDiv(hmWidth,pDC->GetDeviceCaps(LOGPIXELSX),2540);
maskdc.BitBlt(0,0,rcclient.Width(),rcclient.Height(),&memdc,rcclient.left,rcclient.top,SRCCOPY);
CBrush brush;
brush.CreatePatternBrush(&m_bmp);
dc.FillRect(rcclient,&brush);