Undertake the above, introduce three methods, this article will introduce another method of dynamically loaded pictures:
Method 4 With CBitmap, HBitmap, draw on OnPaint ()
First declare a variable in the CTestDLG class: cbitmap m_BMP;
Then we join a Picture tag in the dialog, named IDC_STATIC1
then:
Bool cdisplayPic :: onInitdialog ()
{
CDIALOG :: OnInitdialog ();
IF (m_bmp.m_hobject! = null) // Judgment
m_bmp.deleteObject ();
/ Load pictures
Hbitmap hbmp = (hbitmap) :: loadimage (AfxgetInstanceHandle (),
"c: //aaa.bmp", image_bitmap, 0, 0, lr_createdibsection | lr_loadfromfile;
IF (HBMP == Null)
Return False;
/// This breakager is used to obtain 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 image width
INT cy = bminfo.biheight; // Get image height
///
/ After obtaining the width and height of the image, we can adapt the image size, that is, adjust the size of the control, let it display a picture ///
CRECT RECT;
Getdlgitem (IDC_STATIC1) -> getWindowRect (& REC);
ScreenToClient (& Re);
Getdlgitem (IDC_STATIC1) -> MoveWindow (Rect.Left, Rect.top, CX, CY, TRUE); // Adjust the size
Return True; // Return True UnsS you set the focus to a control
// Exception: OCX Property Pages SHOULD RETURN FALSE
}
The picture is loaded, the label size is also adapted, the following is to draw the drawing image, open the class wizard, and overload the WM_PAINT message.
Void cdisplayPic :: onpaint ()
{
// The following three situations optional one will be different effects (only one exist) ///
// CPAINTDC DC (this); // If this sentence is used, the DC of the dialog is obtained, and the picture will be drawn on the dialog.
CPAINTDC DC (getdlgitem (idc_static1)); // With this sentence, get the DC of the Picture control, the image will be drawn on the control
// CDC DC;
// dc.m_hdc = :: getdc (null); // If you use this 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.createCompaPLEBITMAP (& DC, rcclient.width (), rcclient.Height ());
MEMDC.SELECTOBJECT (& BITMAP);
CWnd :: DefWindowProc (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.top, rcclient.width (), rcclient.height (),
& MEMDC, RCCLIENT.LEFT, RCCLIENT.TOP, SRCPAINT);
Brush.deleteObject ();
// do not call cdialog :: onpaint () for Painting Messages
}
The above four methods can support multiple images, others only support BMP If the reader is interested, you can ask all the routines for the above method. Mailbox: zhucde@jxfw.com should be the proposal of the majority of friends, special will Cheng Pack, put it in the following address for download: http://www.7forever.com/c3d/study/pictest.ra Among them, the program directly draws the program for the dialog, it is not the size of the STATIC adaptive size.