Bitmap manipulation and double buffering mechanisms

Source: Internet
Author: User

Bitmap manipulation and double buffering mechanisms

Bitmap manipulation
Code section:
CRect rect;
GetClientRect (rect);
Pdc->setmapmode (Mm_anisotropic);
Pdc->setwindowext (rect. Width (), Rect. Height ());
Pdc->setviewportext (rect. Width (),-rect. Height ());
Pdc->setviewportorg (rect. Width ()/2, Rect. Height ()/2);

CDC MEMDC;
CBitmap Newbitmap, *poldbitmap;
Newbitmap.loadbitmap (idb_about);
BITMAP bmp; Newbitmap.getbitmap (&bmp);
Memdc.createcompatibledc (PDC);
Poldbitmap = Memdc.selectobject (&newbitmap);

Memdc.setmapmode (Mm_anisotropic);
Memdc.setwindowext (Bmp.bmwidth, bmp.bmheight);

Memdc.setviewportext (Bmp.bmwidth,-bmp.bmheight);
Memdc.setviewportorg (BMP.BMWIDTH/2, bmp.bmheight);
Pdc->bitblt (-rect. Width ()/2,-rect. Height ()/2, Rect. Width (), Rect.  Height (), &MEMDC,-BMP.BMWIDTH/2,-BMP.BMHEIGHT/2, srccopy); Memdc.selectobject (POLDBITMAP);

Double buffering mechanism
CRect rect;
GetClientRect (&rect);
Pdc->setmapmode (Mm_anisotropic);
Pdc->setwindowext (rect. Width (), Rect. Height ());
Pdc->setviewportext (rect. Width (),-rect. Height ());
Pdc->setviewportorg (rect. Width ()/2,rect. Height ()/2);

CDC memdc;//declaring memory DC
CBitmap Newbitmap,*poldbitmap;
Memdc.createcompatibledc (PDC);//Create a memory DC that is compatible with the display DC
Newbitmap.createcompatiblebitmap (Pdc,rect. Width (), Rect. Height ());//Create a compatible memory bitmap
Poldbitmap=memdc.selectobject (&NEWBITMAP);//Select the compatible bitmap into the memory DC
Memdc.fillsolidrect (rect, RGB (0, 0, 0))//Fill the client area with the original background color, otherwise black

Rect. Offsetrect (-rect. Width ()/2,-rect. Height ()/2);
Memdc.setmapmode (mm_anisotropic);//memory DC custom coordinate system
Memdc.setwindowext (rect. Width (), Rect. Height ());
Memdc.setviewportext (rect. Width (),-rect. Height ());
Memdc.setviewportorg (rect. Width ()/2,rect. Height ()/2);

Drawobject (&MEMDC);
Pdc->bitblt (Rect.left,rect.top,rect. Width (), Rect. Height (), &memdc,-rect. Width ()/2,-rect. Height ()/2,srccopy); Copy a bitmap from a memory DC to a device DC
Memdc.selectobject (POLDBITMAP);

Both sections of code use CREATECOMPATIBLEDC (), but they are different, and the related sections look below

Cbitmap::getbitmap int Getbitmap (bitmap* pbitmap);
Return value: Non-zero success, failure zero
Parameter: pbitmap pointer to bitmap struct, must be non-null
Description: Call this member function to retrieve information for a CBitmap object. Information returned through the bitmap structure

Cdc::createcompatibledc virtual BOOL CreateCompatibleDC (cdc* PDC);
Return value: Non-zero success, failure zero
Parameters: PDC A pointer to the device context. If NULL, the function creates a memory device context that is compatible with the display device context
Description: Creates a memory device context that is compatible with the device context that the PDC points to. A memory device context is a block of memory representing a display polygon. It can be used to prepare images in memory when you want to copy them to the actual compatible device above.
When a memory device context is created, GDI automatically selects a 1x1 monochrome pixel bitmap. The GDI output function can be used with a memory device context when and only if a bitmap is created and selected to that context.
This function can only be used to create a compatible device context for devices that can support raster operations. View the CDC:BITBLT member function for information about the transfer of tables between device contexts. Determines whether a device context supports raster operations, to see member functions:: GetDeviceCaps's RC_BITBLT grating capability.

Cbitmap::createcompatiblebitmap BOOL CreateCompatibleBitmap (cdc* pDC, int nwidth, int nheight);
The return value succeeds nonzero, failure zero
Parameters: PDC Specifies the device context nwidth specifies the pixel width of the bitmap nheight the pixel height of the specified bitmap
Description: Initializes a bitmap that is compatible with the device specified by the PDC. The bitmap and the specified device context have the same number of color values or the same format (the same number of color bits per pixel). It can be selected as the current bitmap by any memory device (compatible with a device specified by the PDC).
If the PDC is a memory device context, the bitmap is returned in the device context and the currently selected bitmap has the same format as the bitmap. A "Memory device context" is a piece of memory that represents a display face. It can be used to prepare images in memory before they are copied to the actual compatible device.
When a memory device context is created, GDI automatically chooses a monochrome standby bitmap for it. Because a color memory device context can select a color or monochrome bitmap, the bitmap format returned by the CreateCompatibleBitmap function is not always the same, however, a bitmap that is compatible with the Nonmemory device context is always in the format of the device.
When you complete the CBitmap object creation with the CreateCompatibleBitmap function, the bitmap is first selected for the device context and then the CBitmap object is deleted.

The difference between Cdc::createcompatibledc and Cbitmap::createcompatiblebitmap is the device context, and one is the bitmap; This is the biggest difference.

cwnd::invalidate void Invalidate (BOOL bErase = TRUE);
Parameter: bErase Specifies whether the background in the update area is deleted
Description: The entire client area of WND is invalid. When the WM_PAINT message appears, the customer area is marked as painting. The region can be validated by the ValidateRect and VALIDATERGN member functions before the WM_PAINT message occurs.
The Berase parameter specifies whether the background in the update area is deleted when the update area is processed. If Berase is true, the BeginPaint member function is invoked when the background is removed, and if the berase is false, the background remains the same. If berase applies to any part of the update area, the background of the entire region (not only in a given section) is removed.
As long as the CWnd update zone is not empty and there is no other message for the application queue in that window, Windows sends a WM_PAINT message.

The double-buffering principle is to draw the graph in the memory device context first, then copy it to the display device context, and the code part of it should look carefully.

Bitmap manipulation and double buffering mechanisms

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.