( i ) non-dynamic load picture .
Method 1. let's start with the simplest. , with Picture control to implement .
Steps :
First Import a picture in the resource , ID is idb_bitmap2
Then add a picture control on the dialog , Right-click on the Open property ,
Select the type drop-down box BITMAP, followed by an Image drop -down box,
You will see all the pictures that have been loaded ,
Select the picture you want . you run the program to see .
Method 2. through the background map
Same as above , first load a picture , ID is idb_bitmap2
in TestDlg.h
CBrush m_brbk;// defined in public
in TestDlg.cpp
In the initialization function OnInitDialog () , add :
BOOL Ctestdlg::oninitdialog () {cdialog::oninitdialog (); CBitmap bmp;bmp. LoadBitmap (IDB_BITMAP2); M_brbk.createpatternbrush (&bmp); bmp. DeleteObject (); return TRUE; // return trueunless You set the focus to a control}
Then open the Class Wizard, find the wm_ctlcolor message , overload the corresponding function onctlcolor (), Add the following :
Hbrushctestdlg::onctlcolor (cdc* PDC, cwnd*= cdialog::onctlcolor (PDC, pWnd, nCtlColor); if This ) {return m_brbk;} return HBR;}
( two ) Dynamically loading pictures .
Method 3 Image control ( This example uses the KoDak image Edit Control )
1.You should first ensure that the control is in the system. Note that it cannot be used alone, and must be with several other controls (especiallyImgcmn.dll) used together. If not, from another machine.Copyyou can come here. These files areImgadmin.ocx,Imgcmn.dll,ImgEdit.ocx,Imgscan.ocx,Imgshl.dll,Imgthumb.ocx,Imgutil.dll, put themCopyto theWindows/systemdirectory, and then use theregsvr32.exeregister them separately.
2. Open the Project , go to Explorer , Right-click on the dialog box, click Insert Activex control... Select the Kodak Image Edit control, any size.
3. Select the control on the dialog box and add a variable to it : M_ctrlpicture.
4. Add the following in BOOL ctestdlg::oninitdialog () :
BOOL Ctestdlg::oninitdialog () {cdialog::oninitdialog (); M_ctrlpicture.setimage ("aa.jpg "); // ensure the image is in the project directory, you can also write the absolute path M_ctrlpicture.display (); return TRUE; // return TRUE Unless you set the focus to a control // Exception:ocx Property Pages should return FALSE}
Compile and run is OK , The advantage of this method is that it may be for a variety of image formats .
Method 4 is drawn directly with OnPaint () by Cbitmap,hbitmap
First, Declare a variable in the Ctestdlg class : cbitmapm_bmp;
Then we add a picture tag to the dialog , named idc_static1
Then :
BOOL Cdisplaypic::oninitdialog () {cdialog::oninitdialog ();if(M_bmp.m_hobject! = NULL)//Judgingm_bmp. DeleteObject ();/////////Loading PicturesHbitmap hbmp =(HBITMAP):: LoadImage (AfxGetInstanceHandle (),"c://aaa.bmp", Image_bitmap,0,0, lr_createdibsection|lr_loadfromfile);if(hbmp = =NULL)returnFALSE;///////////////////////The program is used to obtain the information of the loaded BMP////////////////////////m_bmp. Attach (hbmp);D ibsection ds; Bitmapinfoheader&bminfo =Ds.dsbmih; m_bmp. GetObject (sizeof(DS), &DS);intCx=bminfo.biwidth;//get the image widthintCy=bminfo.biheight;//Get Image Height/////////////////// /////////////////////////////////////////////Get the image width and height, we can adapt the image size, that is, adjust the size of the control, so that it appears exactly one picture///////////////////////////CRect rect; GetDlgItem (IDC_STATIC1)->getwindowrect (&rect); ScreenToClient (&rect); GetDlgItem (IDC_STATIC1)->movewindow (Rect.left,rect.top,cx,cy,true);//Adjust SizereturnTRUE;//return TRUE Unless you set the focus to a control//Exception:ocx Property Pages should return FALSE}
BOOL Cdisplaypic::oninitdialog () {cdialog::oninitdialog ();if(M_bmp.m_hobject! = NULL)//Judgingm_bmp. DeleteObject ();/////////Loading PicturesHbitmap hbmp =(HBITMAP):: LoadImage (AfxGetInstanceHandle (),"c://aaa.bmp", Image_bitmap,0,0, lr_createdibsection|lr_loadfromfile);if(hbmp = =NULL)returnFALSE;///////////////////////The program is used to obtain the information of the loaded BMP////////////////////////m_bmp. Attach (hbmp);D ibsection ds; Bitmapinfoheader&bminfo =Ds.dsbmih; m_bmp. GetObject (sizeof(DS), &DS);intCx=bminfo.biwidth;//get the image widthintCy=bminfo.biheight;//Get Image Height/////////////////// /////////////////////////////////////////////Get the image width and height, we can adapt the image size, that is, adjust the size of the control, so that it appears exactly one picture///////////////////////////CRect rect; GetDlgItem (IDC_STATIC1)->getwindowrect (&rect); ScreenToClient (&rect); GetDlgItem (IDC_STATIC1)->movewindow (Rect.left,rect.top,cx,cy,true);//Adjust SizereturnTRUE;//return TRUE Unless you set the focus to a control//Exception:ocx Property Pages should return FALSE}
The picture loaded successfully , the label size also adapts , the following is drawing the drawing image , Open the Class Wizard , overloaded WM_PAINT message
voidCdisplaypic::onpaint () {//////////////The following three kinds of situations can be different effect (only one exists)/////////////CPAINTDC DC (this);//If you use this sentence, you get the DC of the dialog box, and the picture is drawn on the dialog box.CPaintDC DC (GetDlgItem (IDC_STATIC1));//with this sentence, the DC of the picture control is obtained and the image is drawn on the control//CDC DC;//Dc.m_hdc=::getdc (NULL);//If you use these two sentences, get the DC of the screen, the picture will be drawn on the screen///////////////////////////////////////////////////////CRect rcclient; GetDlgItem (IDC_STATIC1)->getclientrect (&rcclient); CDC MEMDC;MEMDC. CreateCompatibleDC (&DC); CBitmap Bitmap;bitmap. CreateCompatibleBitmap (&DC, Rcclient. Width (), rcclient. Height ()); Memdc. SelectObject (&bitmap); CWnd::D Efwindowproc (WM_PAINT, (WPARAM) MEMDC.M_HDC,0); CDC MASKDC;MASKDC. CreateCompatibleDC (&DC); CBitmap Maskbitmap;maskbitmap. CreateBitmap (rcclient. Width (), rcclient. Height (),1,1, NULL); Maskdc. SelectObject (&maskbitmap); maskdc. BitBlt (0,0, Rcclient. Width (), rcclient. Height (), &MEMDC, Rcclient.left, Rcclient.top, srccopy); CBrush Brush;brush. CreatePatternBrush (&m_bmp);d c. FillRect (Rcclient,&brush);d C. BitBlt (Rcclient.left, Rcclient.top, Rcclient. Width (), rcclient. Height (),&MEMDC, Rcclient.left, rcclient.top,srcpaint); brush. DeleteObject ();//cdialog::onpaint () for painting messages}
The above four methods only KoDak can support a variety of images , The other only support BMP
Reprinted from: http://blog.csdn.net/liyonghust/article/details/1064011
MFC dialog Box Map