VC中圖像控件的用法與總結(jié)_第1頁
VC中圖像控件的用法與總結(jié)_第2頁
VC中圖像控件的用法與總結(jié)_第3頁
VC中圖像控件的用法與總結(jié)_第4頁
VC中圖像控件的用法與總結(jié)_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、使用picture控件動態(tài)顯示圖片方法一 控件適應(yīng)于圖片尺寸添加一個picture控件,修改id為idc-picture,并添加一cbitmap m_bmp成員變量在oninitdialog()函數(shù)中添加如下代碼if(m_bmp.m_hobject!=null) m_bmp.deleteobject();/以絕對路徑加載bmp圖片hbitmap hbmp=(hbitmap):loadimage(afxgetinstancehandle(),1.bmp,image_bitmap,0,0,lr_createdibsection | lr_loadfromfile);if(hbmp=null) re

2、turn false;/獲得加載位圖的文件信息m_bmp.attach(hbmp);dibsection ds;bitmapinfoheader &bminfo=ds.dsbmih;/獲得位圖信息圖m_bmp.getobject(sizeof(ds),&ds);int cx=bminfo.biwidth;/獲得位圖寬度int cy=bminfo.biheight;/獲得位圖高度/該段程序根據(jù)圖象的寬和高,調(diào)整控件大小,讓它正好顯示一張圖片crect rect;getdlgitem(idc_pictrue)-getwindowrect(&rect);/獲得控件所在矩形區(qū)域screentoclie

3、nt(&rect);getdlgitem(idc_pictrue)-movewindow(rect.left,rect.top,cx,cy,true);/調(diào)整控件大小在onpaint()函數(shù)中添加如下代碼/三種方法獲得pictrue控件dc,圖象將被繪制在控件上cpaintdc dc(getdlgitem(idc_pictrue);/cpaintdc dc(this);/cdc dc;/dc.m_hdc=:getdc(null);crect rcclient;getdlgitem(idc_pictrue)-getclientrect(&rcclient);cdc memdc;memdc.cre

4、atecompatibledc(&dc);cbitmap bitmap;bitmap.createcompatiblebitmap(&dc,rcclient.width(),rcclient.height();memdc.selectobject(&bitmap);cwnd:defwindowproc(wm_paint,(wparam)memdc.m_hdc,0); cdc maskdc;maskdc.createcompatibledc(&dc);cbitmap maskbitmap;maskbitmap.createbitmap(rcclient.width(),rcclient.heig

5、ht(),1,1,null);maskdc.selectobject(&maskbitmap);maskdc.bitblt(0,0,rcclient.width(),rcclient.height(),&memdc,rcclient.left,rcclient.top,srccopy);cbrush brush;brush.createpatternbrush(&m_bmp);dc.fillrect(rcclient,&brush);dc.bitblt(rcclient.left,rcclient.top,rcclient.width(),rcclient.height(),&memdc,rc

6、client.left,rcclient.top,srcpaint);brush.deleteobject();二 圖片適用于控件尺寸 添加一個picture控件,修改id為idc-picture在onpaint()函數(shù)中添加如下代碼cwnd *pwnd=getdlgitem(idc_pictrue);/獲得pictrue控件窗口的句柄crect rect;pwnd-getclientrect(&rect);/獲得pictrue控件所在的舉行區(qū)域cdc *pdc=pwnd-getdc();/獲得pictrue控件的dcistream *pstm;cfilestatus fstatus;cfil

7、e file;long cb;ipicture *ppic;/定義顯示圖片的接口(可顯示jpg格式圖片)cstring m_spath=1.bmp;/設(shè)置想要顯示的圖片/打開圖象文件,獲取文件字節(jié)數(shù)if(file.open(m_spath,cfile:moderead) & file.getstatus(m_spath,fstatus) & (cb=fstatus.m_size)!=-1) /分配全局存儲空間 hglobal hglobal=globalalloc(gmem_moveable,cb); lpvoid pvdata=null; if(hglobal!=null) /鎖定分配內(nèi)存塊

8、 if(pvdata=globallock(hglobal)!=null) /讀入文件到內(nèi)存緩沖區(qū) file.readhuge(pvdata,cb); globalunlock(hglobal); createstreamonhglobal(hglobal,true,&pstm); /裝載圖象文件if(succeeded(oleloadpicture(pstm,fstatus.m_size,true,iid_ipicture,(lpvoid*)&ppic) ole_xsize_himetric hmwidth; ole_xsize_himetric hmheight; ppic-get_wid

9、th(&hmwidth);/獲得圖象真實寬度 ppic-get_height(&hmheight);/獲得圖象真實高度 /在控件上顯示圖片 ppic-render(*pdc,0,0,rect.width(),rect.height(),0,hmheight,hmwidth,-hmheight,null); ppic-release();圖片顯示通用模塊首先為類添加如下兩個函數(shù)和一個成員變量ipicture* m_ppicture;/圖片加載函數(shù)hresult loadfrombuffer(byte *pbuff, int nsize)bool bresult=false;hglobal hgl

10、obal=globalalloc(gmem_moveable,nsize);/分配內(nèi)存空間void *pdata=globallock(hglobal);/定義圖象數(shù)據(jù)指針memcpy(pdata,pbuff,nsize);globalunlock(hglobal);/釋放內(nèi)存空間istream *pstream=null;/定義數(shù)據(jù)流指針/創(chuàng)建數(shù)據(jù)流if(createstreamonhglobal(hglobal,true,&pstream)=s_ok)hresult hr;/定義返回結(jié)果if(hr=oleloadpicture(pstream,nsize,false,iid_ipictur

11、e,(lpvoid*)&m_ppicture)=s_ok)bresult=true;pstream-release();/釋放數(shù)據(jù)流return bresult;/圖片顯示函數(shù)bool drawfromfile(cstring filename, cdc *pdc)cfile cfile;cfileexception e;/打開文件if(cfile.open(filename,cfile:moderead | cfile:typebinary,&e)byte *pbuff=new bytecfile.getlength();/定義分配內(nèi)存if(cfile.read(pbuff,cfile.ge

12、tlength()0)/將文件讀入緩存loadfrombuffer(pbuff,cfile.getlength();delete pbuff;/釋放緩存long hmwidth,hmheight;m_ppicture-get_width(&hmwidth);m_ppicture-get_height(&hmheight);/獲取圖片尺寸int nwidth=muldiv(hmwidth,pdc-getdevicecaps(logpixelsx),2540);int nheight=muldiv(hmheight,pdc-getdevicecaps(logpixelsx),2540);/在內(nèi)存中

13、畫cdc mendc;cbitmap bmp;bmp.createcompatiblebitmap(pdc,nwidth,nheight);mendc.createcompatibledc(null);mendc.selectobject(&bmp);m_ppicture-render(mendc.m_hdc,0,0,nwidth,nheight,0,hmheight,hmwidth,-hmheight,null);/將內(nèi)存中畫拷貝到目的地pdc-bitblt(0,0,nwidth,nheight,&mendc,0,0,srccopy);return true;然后在onpaint()函數(shù)中顯示void cdialogdlg:onpaint() cpaintdc dc(this); / device context for paintingif (isiconic()sendmessage(wm_iconerasebkgnd, (wparam) dc.getsafehdc(), 0);/ center icon in client rectangleint cxicon = getsystemmetrics(sm_cxicon);int cyicon = getsystemmetrics(sm_cyicon);crect rect;getcl

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論