在MFC当中,怎么动态加载图片(bmp、jpg、gif等),还有就是如果使用picture控件,代码该怎么写?

开发编程  |  电脑网络

分享:
2017-07-15

2017-07-15最佳答案


给你个例子 利用流工程基于对话框 名字是IPicture 建好工程后 加入下面的代码 就可以显示图片了// IPictureDlg.h : 头文件BOOL DisplayPic(CString strFileName, HWND hWnd, int nScrWidth, int nScrHeight);// IPictureDlg.cpp : 实现文件BOOL CIPictureDlg::DisplayPic(CString strFileName, HWND hWnd, int nScrWidth, int nScrHeight){ HDC hDC=::GetDC(hWnd); IPicture *pPicture; IStream *pStream; HRESULT hResult; HANDLE hFile = NULL; DWORD dwFileSize,dwByteRead; hFile = CreateFile(strFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE){return E_FAIL;} dwFileSize = GetFileSize(hFile, NULL);HGLOBAL hGlobal = ::GlobalAlloc(GHND, dwFileSize);if(hGlobal == NULL){ AfxMessageBox("图形文件装载出错!"); return E_FAIL;} LPVOID lpData = NULL;lpData = ::GlobalLock(hGlobal); ReadFile(hFile, lpData, dwFileSize, &dwByteRead, NULL);::GlobalUnlock(hGlobal); if(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK){ AfxMessageBox("流初始化失败!"); return E_FAIL;} hResult = OleLoadPicture(pStream, dwFileSize, TRUE, IID_IPicture, (LPVOID*) &pPicture); if(FAILED(hResult)) { AfxMessageBox("图形文件装载出错!"); return E_FAIL; } OLE_XSIZE_HIMETRIC hmWidth;//图片的真实宽度 OLE_YSIZE_HIMETRIC hmHeight;//图片的真实高度 pPicture->get_Width(&hmWidth); pPicture->get_Height(&hmHeight); //将图形输出到屏幕上 hResult = pPicture->Render(hDC, 0, 0, nScrWidth, nScrHeight, 0, hmHeight, hmWidth, -hmHeight, NULL); CloseHandle(hFile);//关闭打开的文件 pPicture->Release(); // Free memory.::GlobalFree(hGlobal); if (SUCCEEDED(hResult)) { return S_OK; } else { AfxMessageBox("图形文件装载出错!"); return E_FAIL; }}void CIPictureDlg::OnBnClickedOk(){// TODO: 在此添加控件通知处理程序代码static char szFilter[] = "JPG Files (*.jpg)|*.jpg|BMP Files (*.bmp)|*.bmp|GIF Files (*.gif)|*.gif|All Files (*.*)|*.*||";CFileDialog dlg(TRUE, "BMP", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);if(dlg.DoModal() != IDOK)return;CRect rect;GetDlgItem(IDC_STATIC)->GetWindowRect(rect);DisplayPic(dlg.GetPathName(),GetDlgItem(IDC_STATIC)->GetSafeHwnd(), rect.Width(), rect.Height());}

其它2条答案


vc加载图片网上代码多了去了,google去吧

2017-07-15


用CImage类,load,bitblt。一共不超过10行代码

2017-07-15