MFC multiple ways to display pictures in a dialog box (four methods)

Source: Internet
Author: User

Let's start with a simple one. First divide a class:
(a) non-animated display picture (that is, the picture is first loaded through the explorer, there is a fixed ID)
(b) Dynamic loading of images (that is, only the path of the specified image in the program can be loaded)

For the convenience of illustration, we have built good one dialog-based project named TTest.
dialog box class is Ctestdlg
(a) non-dynamic loading of images.

Method 1. Start with the simplest, and use the picture control to implement it.
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. Run the program to see it.

Method 2: Pass 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 TRUE unless you set the focus to a control
}

In the Open Class Wizard, locate the WM_CTLCOLOR message, overload the corresponding function OnCtlColor (), and add the following:
Hbrush Ctestdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
{
Hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);

if (pWnd = = this)
{
return M_BRBK;
}
return HBR;
}

(b) Dynamic loading of images.

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 used in conjunction with several other controls, especially Imgcmn.dll. If not, copy it from another machine. These files are Imgadmin.ocx,imgcmn.dll,imgedit.ocx,imgscan.ocx,imgshl.dll,imgthumb.ocx,imgutil.dll, copy them to windows\. System directory, and then use Regsvr32.exe to register 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: CBitmap m_bmp;
Then we add a picture tag to the dialog, named Idc_static1
And then:
BOOL Cdisplaypic::oninitdialog ()
{
CDialog::OnInitDialog ();
if (m_bmp.m_hobject! = NULL)//judgment
M_bmp. DeleteObject ();
Loading pictures
Hbitmap hbmp = (HBITMAP):: LoadImage (AfxGetInstanceHandle (),
"C:\\aaa.bmp", Image_bitmap, 0, 0, lr_createdibsection| Lr_loadfromfile);
if (hbmp = = NULL)
return FALSE;
The program is used to obtain the information of the loaded BMP////////////////////////
M_bmp. Attach (hbmp);
Dibsection ds;
Bitmapinfoheader &bminfo = Ds.dsbmih;
M_bmp. GetObject (sizeof (DS), &ds);
int cx=bminfo.biwidth; Get the image width
int cy=bminfo.biheight; Get Image Height
/////////////////// ////////////////////////////////
After we get the width and height of the image, we can adapt the size of the image to the size of the control, so that it shows exactly one picture///////////////////////////
CRect rect;
GetDlgItem (IDC_STATIC1)->getwindowrect (&rect);
ScreenToClient (&rect);
GetDlgItem (IDC_STATIC1)->movewindow (rect.left,rect.top,cx,cy,true);//Sizing

return TRUE; Return TRUE unless you set the focus to a control
Exception:ocx Property Pages should return FALSE
}
The picture loading succeeds, the label size also adapts, below is draws the drawing image, opens the Class Wizard, overloads the WM_PAINT message
void Cdisplaypic::onpaint ()
{
The following three cases can be a 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);
dc. FillRect (Rcclient, &brush);


dc. 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

Http://www.cnblogs.com/lidabo/p/3649459.html

MFC multiple ways to display pictures in a dialog box (four methods)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.