首页 > 代码库 > 用按钮打开对话框选取图片按大小显示在控件中
用按钮打开对话框选取图片按大小显示在控件中
[cpp] view plaincopy
- 打开按钮相应消息
[cpp] view plaincopy
- void CLoadBmpDlg::OnBtnOpenBmp()
- {
- // TODO: Add your control notification handler code here
- ////选取图片文件
[cpp] view plaincopy
- CFileDialog file_open_dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,"BMP Files(*.bmp)|*.bmp|All Files(*.*)|*.*||");
- if(file_open_dlg.DoModal() != IDOK)
- {
- return;
- }
- m_pFilePath = file_open_dlg.GetPathName();
- // ((CStatic*)GetDlgItem(IDC_STC_BMP))->SetBitmap(::LoadBitmap(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_BITMAP)));
- // HBITMAP hsource = (HBITMAP)LoadImage(AfxGetInstanceHandle(), m_pFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- // HBITMAP hbmp = ((CStatic*)GetDlgItem(IDC_STC_BMP))-> SetBitmap(hsource);
- Invalidate( FALSE );
- }
///OnPaint函数
[cpp] view plaincopy
- void CLoadBmpDlg::OnPaint()
[cpp] view plaincopy
- {
[cpp] view plaincopy
- //////////////////////////////////////////////////////////////
- CPaintDC bmpDC(GetDlgItem( IDC_STC_BMP ));
- CBitmap SourceImage;
- HBITMAP OldBitmap;
- CDC mDC;
- // SourceImage.LoadBitmap(image_name);
- HANDLE filehandle = (HBITMAP)LoadImage( NULL, m_pFilePath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
- if ( filehandle != NULL )
- {
- BITMAP bmpInfo;
- SourceImage.Attach( filehandle );
- SourceImage.GetBitmap( &bmpInfo );
- CDC dcCompatible;
- dcCompatible.CreateCompatibleDC(&bmpDC);
- OldBitmap = (HBITMAP)dcCompatible.SelectObject(&SourceImage);
- CRect rec;
- ((CStatic *)GetDlgItem(IDC_STC_BMP))->GetClientRect(&rec);
- // dc.BitBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, 0, 0, SRCCOPY);
- int n = bmpDC.StretchBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, rec.left, rec.top, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
- //
- // CBrush brush;
- // brush.CreatePatternBrush(&SourceImage);
- // bmpDC.FillRect(rec, &brush);
- // bmpDC.StretchBlt(rec.left, rec.top, rec.Width(), rec.Height(), &dcCompatible, rec.left, rec.top, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY);
- // brush.DeleteObject();
- dcCompatible.SelectObject(&OldBitmap);
- SourceImage.Detach();
- }
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。