[Microsoft Visual Studio 2010] Open the picture based on the MFC dialog box and display it on the rectangular box
This article was written under the guidance of the school teacher. It is a simple program application based on the MFC dialog box. I hope to provide a reference for students who are new to MFC. The compiler software is Microsoft Visual Studio 2010. If the program code cannot be executed because the vs2010 version of the school computer room is too old, you can refer to another blog of mine.
Body:
First, run VS2010 to create a new project, select MFC application under Visual C++, enter the project name and save address and click OK.
Select the dialog box based on the MFC program wizard, and click Next or Finish directly.
After the project is built, you can compile the empty project first. After confirming that it is correct, start the next step.
Select Toolbox to add a rectangular picture frame and two buttons in the dialog box.
Configure the ID of the rectangular picture frame, and configure the ID of the rectangular frame 1 as ORGIMAGE. Modify the caption of the button.
Right-click the inner rectangular box to add a variable, the variable name is IMAGE1.
Then right-click the outer large rectangle to add two variables of type CString, named strFilePath and strFileName. Right-click the class wizard to view the member variables to check whether the variable is successfully added.
Then double-click button 1 to add the following code
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;
}
CImage image;
image.Load(strFilePath);
CRect rectControl; //控件矩形对象
IMAGE1.GetClientRect(rectControl);
CDC *pDc = IMAGE1.GetDC(); //设备上下文对象的类
rectControl = CRect(rectControl.TopLeft(), CSize((int)rectControl.Width(), (int)rectControl.Height()));
IMAGE1.SetBitmap(NULL); //清空picture
image.Draw(pDc->m_hDC, rectControl); //将图片绘制到Picture控件表示的矩形区域
image.Destroy();
IMAGE1.ReleaseDC(pDc);
If no error is reported, press F5 to compile and execute.
Final effect:
refer to the blog: https://blog.csdn.net/qq_37907622/article/details/83410667