MFC displays bitmaps with double buffering

Source: Internet
Author: User

The load from the resource is:
CBitmap bmp;
Bmp. LoadBitmap (IDB_BITMAP1)

Loading from the file is, note that the bitmap is the BMP file.
CBitmap bitmap;
Bitmap.m_hobject= (HBITMAP):: LoadImage (NULL, "test.bmp", image_bitmap,500,400,lr_loadfromfile| Lr_createdibsection);
It is important to note that loading a bitmap from a file is not done by calling the CBitmap member function.
Instead, the loadimage of the CBitmap object is completed by assigning its return value to the member variable of CBitmap by using the SDK function, M_hobject.
The coercion type here can not be used, the use is emphasized meaning.

The image content in the image file can be loaded into the CBitmap class. Supported formats: BMP, JPG, GIF, and PNG.

CImage imgtemp;
Imgtemp.load (strFilePath);
if (pbitmap->m_hobject) Pbitmap->detach ();
Pbitmap->attach (Imgtemp.detach ());

But there is no cimage this class in VC6, C + + in the net2005+ platform. Use this class to first add #include <atlimage.h> in stdafx.h.

If the StretchBlt is used, it will look like a noticeable distortion when the destination area is smaller than the source area. than the excessive distortion level

Again, double buffering technology:

When the form is refreshed, there is always a process to erase the original image OnEraseBkgnd, which fills the form plot area with the background color and then redraws it by invoking a new drawing code so that the color of the image is contrasted by a wipe. When WM_PAINT's response is frequent, the contrast is becoming more pronounced. So we saw a flicker. Avoiding background color padding is the most straightforward approach. But in that case, the form will become a mess. Because each time the image is not removed from the original image, resulting in the image of the residue, so when the form is redrawn, the screen will often become a mess. So simply banning background repainting is not enough. We also have to re-draw, but the requirements are fast, so we think of using the BitBlt function. It can support the copying of graphics blocks and is very fast. We can draw in memory first, and then use this function to copy the good diagram to the foreground, and suppress the background refresh, thus eliminating flicker. The above is also the basic idea of double-buffered drawing.

Implementation steps:

Suppose we set up a draw project and we're going to do a drawing operation in Drawview.

In the double buffering method, the first thing to do is to mask the background refresh. The background refresh is actually in response to the WM_ERASEBKGND message. We add a response to this message in the View Class (CDrawView), and we can see that the default code is as follows:

BOOL cdrawview::onerasebkgnd (cdc* PDC)
{
Return Cdrawview::onerasebkgnd (PDC);
return TRUE;
}

(1) Adding member variables (in the DrawView.h file)

Parameter declaration
cbitmap* M_poldbitmap;
cbitmap* M_pmembitmap; Declaring a bitmap in memory that hosts a temporary image
cdc* M_PMEMDC; Declaring the memory DC used to buffer the drawing

(2) Initialize variables (in Drawview's constructor)

M_pmemdc=new CDC ();
M_pmembitmap=new CBitmap ();

(3) Add message response function wm_create (in DrawView.cpp)
int cdrawview::oncreate (lpcreatestruct lpcreatestruct)
{
if (cview::oncreate (lpcreatestruct) = =-1)
return-1;

int X=getsystemmetrics (sm_cxscreen);
int Y=getsystemmetrics (sm_cyscreen);

cdc* Pdc=getdc ();
M_pmemdc->createcompatibledc (PDC); Dependency window DC creates a compatible memory DC
M_pmembitmap->createcompatiblebitmap (Pdc,x,y); Create a compatible bitmap
M_poldbitmap=m_pmemdc->selectobject (M_PMEMBITMAP); Select the bitmap into the memory DC and save the image to M_poldbitmap

CBrush Brush (RGB (255,255,255));
M_pmemdc->fillrect (CRect (0,0,x,y), &brush); Set client area background to white

ReleaseDC (PDC);

Return 0;
}
(4) Modify the OnDraw () function (DrawView.cpp)

void Cdrawview::ondraw (cdc* PDC)
{
cdocument* pDoc = getdocum ENT ();
CRect RC;
GetClientRect (&RC);
Drawsomething ();  //In this function you can draw what you want to draw
Pdc->bitblt (0,0,RC. Width (), RC. Height (), m_pmemdc,0,0,srccopy);
//This is the memory of the canvas copied to the display device buffer
}

(5) its own drawing function (DrawView.cpp)

void drawsomething ()
{
&nb sp;   M_pmemdc->rectangle (0,0,100,100);       //Draw a rectangle here
}

(6) Delete the new object (in Drawview's destructor)

Delete m_pbitmap;
     Delete M_PMEMDC;

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.