1, MFC dialog box display BMP picture
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
If you use the following two sentences, get the DC of the screen, the picture will be drawn on the screen/////////////////////////////////////////////////////
CDC DC;
DC.M_HDC=::GETDC (NULL);
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
Above transfer from: http://blog.csdn.net/cecilia214/article/details/5346302
The following transfers are from: http://topic.csdn.net/u/20090410/10/ad7a5afe-c906-45ff-a673-f601e2ec05be.html
2. MFC dialog box displays JPEG pictures
Put it in the OnPaint of the dialog box.
1 cdc* PDC;
2 Pdc=getdc ();
3 Showpicture (PDC, "c:\\12.jpg", 0,0,200,100);
4
5
6
7 #define Himetric_inch 2540
8 Lppicture gppicture;
9 void Ccdmadlg::showpicture (CDC *pdc, CString m_strbroute, int x, int y, int width, int height)
10 {
HANDLE hfile =createfile (m_strbroute, generic_read, 0, NULL, open_existing, 0, NULL);
_asserte (Invalid_handle_value! = hfile);
13
14//Get File size
DWORD dwfilesize = GetFileSize (hfile, NULL);
_asserte ( -1! = dwfilesize);
LPVOID pvData = NULL;
18//Allocate memory based on file size
Hglobal hglobal = GlobalAlloc (gmem_moveable, dwfilesize);
_asserte (NULL! = hglobal);
PvData = GlobalLock (hglobal);
_asserte (NULL! = pvData);
dwBytesRead DWORD = 0;
24//Read the file and deposit to global memory
BRead BOOL = ReadFile (hfile, PvData, dwFileSize, &dwbytesread, NULL);
_asserte (FALSE! = BRead);
GlobalUnlock (HGLOBAL);
CloseHandle (hfile);
Lpstream pstm = NULL;
30//Create a istream* pointer through global memory
(HRESULT) hr = CreateStreamOnHGlobal (Hglobal, TRUE, &pstm);
_asserte (SUCCEEDED (HR) && pstm);
33//Create a IPicture object from a graphics file
if (gppicture)
Gppicture->release ();
hr = OleLoadPicture (pstm, dwFileSize, FALSE, Iid_ipicture, (LPVOID *) &gppicture);
PNS _asserte (SUCCEEDED (HR) && gppicture);
Pstm->release ();
(a). HDC hdc;
HDC=PDC->GETSAFEHDC ();
if (gppicture)
42 {
43//Get picture width and height
Hmwidth long;
a long hmheight;
Gppicture->get_width (&hmwidth);
Gppicture->get_height (&hmheight);
48//Width-to-height conversion to pixels
nwidth int = MulDiv (hmwidth, GetDeviceCaps (hdc, LOGPIXELSX), himetric_inch);
int nheight = MulDiv (Hmheight, GetDeviceCaps (hdc, Logpixelsy), himetric_inch);
N-RECT RC;
GetClientRect (&RC);/* Get customer Area */
Gppicture->render (hdc, x, y, (int) height*hmwidth/hmheight,height, 0, Hmheight, Hmwidth,-HM Height, &RC);
54/* Show Pictures */
55}
57}
MFC dialog box showing BMP pictures