//绑定button的事件处理函数
void CloadfileDlg::OnBnClickedButton1() { //进度条置为零 //CProgressCtrl *prgs = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS); //prgs->SetPos(0); //选择图片 CFileDialog fileDlg(TRUE, _T("png"), NULL, 0, _T("image Files(*.bmp; *.jpg;*.png)|*.JPG;*.PNG;*.BMP|All Files (*.*) |*.*||"), this); fileDlg.DoModal(); //获取图片路径和图片名称 strFilePath = fileDlg.GetPathName(); strFileName = fileDlg.GetFileName();
//判断路径不为空 if (strFilePath == _T("")) { return; } image.Load(strFilePath);
//以下两个矩形主要作用是,获取对话框上面的Picture Control的width和height,
//并设置到图片矩形rectPicture,根据图片矩形rectPicture对图片进行处理,
//最后绘制图片到对话框上Picture Control上面 CRect rectControl; //控件矩形对象 CRect rectPicture; //图片矩形对象
int x = image.GetWidth(); int y = image.GetHeight();
//Picture Control的ID为IDC_IMAGE CWnd *pWnd = GetDlgItem(IDC_IMAGE); pWnd->GetClientRect(rectControl); CDC *pDc = GetDlgItem(IDC_IMAGE)->GetDC(); SetStretchBltMode(pDc->m_hDC, STRETCH_HALFTONE); rectPicture = CRect(rectControl.TopLeft(), CSize((int)rectControl.Width(), (int)rectControl.Height())); ((CStatic*)GetDlgItem(IDC_IMAGE))->SetBitmap(NULL); //以下两种方法都可绘制图片 //image.StretchBlt(pDc->m_hDC, rectPicture, SRCCOPY); //将图片绘制到Picture控件表示的矩形区域 image.Draw(pDc->m_hDC, rectPicture); //将图片绘制到Picture控件表示的矩形区域 image.Destroy(); pWnd->ReleaseDC(pDc); }
运行结果:点击选择图片按钮,选择图片后,图片在Picture Control上展示