The MFC dialog box displays the BMP file.

Source: Internet
Author: User
Tags bmp image

From: http://www.cnblogs.com/jjzhou1988/archive/2009/07/22/1347862.html

The original article is as follows:

Some time ago I wanted to use the VC dialog box to dynamically display a BMP bitmap. I checked many articles and found that it was very complicated (and it seems that many of them were copied and pasted ), there is not much real value. in fact, it is very convenient for VC to display BMP files. All the code can be added up to five or six lines. therefore, I would like to write my own experience into my blog, hoping to help people who need it.

To dynamically display bitmaps in VC, there are two simple methods:

1. display the bitmap that has been added as a project resource

Preparation: add the image as a project resource and set its handle name to IDB_BITMAP1. Then, place a static control or image control on the dialog box and set its handle name to IDC_SHOWBMP;

Code: Add the following code to the desired area (such as the button function, OnInitDialog (), or OnPaint:

CStatic * p = (CStatic *) GetDlgItem (IDC_SHOWBMP ); // Obtain the pointer to IDC_SHOWBMP, forcibly convert it to the CStatic * type, and assign the value to p

HBITMAP hBitmap =: LoadBitmap (AfxGetResourceHandle (), MAKEINTRESOURCE (IDB_BITMAP1); // obtain the bitmap handle of IDB_BITMAP1 from the project resource and assign it to the hBitmap variable

P-> Modifystyle0xF, SS_BITMAP | SS_CENTERIMAGE); // set the static control (static control pointed by p) for displaying bitmap, and it is displayed in the center of the control // (if this step is not available, it cannot be displayed, because it does not indicate the type of static dialog box to be displayed)

P-> SetBitmaphBitmap );// Set the bitmap to bBitmap, that is, the resource IDB_BITMAP1 handle.

 

2. display the bitmap through the path of the bitmap (this method does not need to add the bitmap file as the resource of the project in advance)

Preparation: Put a static control (it must be a static control, but it cannot be displayed if the image control is not displayed) on the dialog box and rename it to IDC_BMP (if it is not renamed, the variable cannot be added ), then add a variable named m_image to the control.

Code: Add the following code to the desired area (such as the button function, OnInitDialog (), or OnPaint:

This-> m_image.ModifyStyle (0, SS_BITMAP | SS_CENTERIMAGE );// Set the static control

              Is used to display bitmap, but here the modifystyle operation is performed through the variables in the static dialog box.

HBITMAP hBmp = (HBITMAP): LoadImage (0, "path \ 1.bmp", IMAGE_BITMAP, 0, 0,

 LR_LOADFROMFILE );    // Read the handle of the BMP image directly from the path of the BMP file and convert it to the hbitmap type

// Note: "path \ 1.bmp" is a relative path, that is, it is in the same directory as the executable file.

// There is a path folder with a bitmap file named 1.bmp.
    This-> m_image.setbitmap (hbmp); // you can use the m_image variable to set the bitmap file displayed in the static box.

----------------------------------------------------------------------------

Summary:

Display images on controls, especially static text controls. The above method is indeed feasible, especially the second method is recommended.

However, if the second method is used, only part of the displayed BMP file is displayed if its width is higher than the width or height of the control.

Therefore, modify the preceding method.

 

For example, in the onpaint () function of a dialog box program, do the following:

Void ctemptestdlg: onpaint ()
{
 If (isiconic ())
 {
   Cpaintdc DC (this); // device context for painting

Sendmessage (wm_iconerasebkgnd, (wparam) DC. getsafehdc (), 0 );

// Center icon in client rectangle
   Int cxicon = getsystemmetrics (sm_cxicon );
   Int cyicon = getsystemmetrics (sm_cyicon );
   Crect rect;
   Getclientrect (& rect );
   Int x = (rect. Width ()-cxicon + 1)/2;
   Int y = (rect. Height ()-cyicon + 1)/2;

  // Draw the icon
 DC. drawicon (X, Y, m_hicon );
 }
 Else
 { 
  CPaintDC dc (this );
  // Load the image
  This-> m_img.ModifyStyle (0, SS_BITMAP | SS_CENTERIMAGE );// M_img is the Control variable of the Control.
  HBITMAP hbitmap = (HBITMAP): LoadImage (NULL, m_edit1, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );

// M_edit1 stores the image path
  // This-> m_img.SetBitmap (hbitmap); block this sentence
  // Obtain the texture position
  CRect rcImage;
  GetDlgItem (IDC_IMG)-> GetWindowRect (rcImage); // IDC_IMG is the control type.
  ScreenToClient (rcImage );
  
  
  // Drawing
  CDC memdc;
  Memdc. CreateCompatibleDC (& dc );
  BITMAP bmp;
  : GetObject (hbitmap, sizeof (bmp), & bmp );
  HBITMAP hOldBitmap = (HBITMAP): SelectObject (memdc. GetSafeHdc (), hbitmap );
  Dc. SetStretchBltMode (HALFTONE );
  DC. stretchblt (rcimage. Left, rcimage. Top, rcimage. Width (), rcimage. Height (),
   & Memdc, 0, 0, BMP. bmwidth, BMP. bmheight, srccopy );
  DC. setstretchbltmode (halftone );

: SelectObject (memdc. getsafehdc (), holdbitmap );
 Memdc. deletedc ();
  Cdialog: onpaint ();
 }
}

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.