CStatic *mPicture = (CStatic *)GetDlgItem(pictureGroup1[i]); HICON hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON_CHK), IMAGE_ICON, 16, 16, 0); mPicture->ModifyStyle(0x0,SS_ICON|SS_CENTERIMAGE); mPicture->SetIcon(hIcon);
//创建DC CClientDC dc(GetDlgItem(资源ID)); //获取图片内容 BITMAP bm; CBitmap* hbmp = CBitmap::FromHandle(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1))); hbmp->GetBitmap(&bm); CDC dcMem; //创建兼容DC dcMem.CreateCompatibleDC(&dc); CBitmap *pOldBitmap=dcMem.SelectObject(hbmp); CRect lRect; //获取Static控件的大小范围 m_bt_1.GetClientRect(&lRect); //在Static控件上显示位图 //判断是否需要调整到适合画布 dc.StretchBlt(lRect.left, lRect.top, lRect.Width()+100, lRect.Height()+100, &dcMem, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY); dc.SelectObject(pOldBitmap);
HBITMAP hbitmap; //将pStatic指向要显示的地方 CStatic *pStatic = NULL; //根据ID获取Static控件 pStatic=(CStatic*)GetDlgItem(IDC_BUTTON1); //装载资源*.bmp hbitmap=(HBITMAP)::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1)); CBitmap hbmp; hbmp.Attach(hbitmap); //获取图片内容 BITMAP bm; hbmp.GetBitmap(&bm); CDC dcMem; //创建兼容DC dcMem.CreateCompatibleDC(GetDC()); CBitmap *pOldBitmap=(CBitmap*)dcMem.SelectObject(hbmp); CRect lRect; //获取Static控件的大小范围 pStatic->GetClientRect(&lRect); //在Static控件上显示位图 pStatic->GetDC()->StretchBlt(lRect.left ,lRect.top ,lRect.Width(),lRect.Height(), &dcMem,0 ,0,bm.bmWidth,bm.bmHeight,SRCCOPY); dcMem.SelectObject(&pOldBitmap);
用这种方法可以在按钮上绘制图片
#include<ocidl.h> #include<olectl.h> // lpstrFile:文件名(最好是包含路径在内) // hWnd // nScrWidth:输出图片的宽度 // nScrHeight:输出图片的高度 // nScrWidth、nScrHeight是输出图片的实际大小,换而言之是指缩放后的尺寸 HRESULT ShowPic(TCHAR *lpstrFile,HWND hWnd,int nScrWidth,int nScrHeight) { HDC hDC_Temp=GetDC(hWnd); IPicture* pPic ; IStream* pStm ; BOOL bResult ; HANDLE hFile=NULL ; DWORD dwFileSize,dwByteRead ; //打开硬盘中的图形文件 hFile=CreateFile(lpstrFile,GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(hFile!=INVALID_HANDLE_VALUE) { dwFileSize=GetFileSize(hFile,NULL); //获取文件字节数 if(dwFileSize==0xFFFFFFFF) return E_FAIL ; } else { return E_FAIL ; } //分配全局存储空间 HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize); LPVOID pvData=NULL ; if(hGlobal==NULL) return E_FAIL ; //锁定分配内存块 if((pvData=GlobalLock(hGlobal))==NULL)return E_FAIL ; ReadFile(hFile,pvData,dwFileSize,&dwByteRead,NULL); //把文件读入内存缓冲区 GlobalUnlock(hGlobal); CreateStreamOnHGlobal(hGlobal,TRUE,&pStm); //装入图形文件 bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic); if(FAILED(bResult)) return E_FAIL ; OLE_XSIZE_HIMETRIC hmWidth ; //图片的真实宽度 OLE_YSIZE_HIMETRIC hmHeight ; //图片的真实高度 pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight); if(hmWidth!=0) nScrHeight=(int)((long double)nScrWidth*hmHeight/hmWidth); //将图形输出到屏幕上(有点像BitBlt) bResult=pPic->Render(hDC_Temp,0,0,nScrWidth,nScrHeight, 0,hmHeight,hmWidth,-hmHeight,NULL); pPic->Release(); CloseHandle(hFile); //关闭打开的文件 if(SUCCEEDED(bResult)) { return S_OK ; } else { return E_FAIL ; } }这种方法有一个现成的封装类,下载地址: http://download.csdn.net/detail/windows_nt/5567871