Background drawing Settings Transparent

Source: Internet
Author: User
Tags transparent color

Let's take a look at the prototype of the BitBlt function:

BOOL BitBlt (int x, int y, int nwidth,nheight, cdc* psrcdc, int xsrc, int ysrc, DWORD dwrop);

Parameter Introduction:

int x represents the x-coordinate of the upper-left corner of the drawing bitmap object;

int y indicates the y-coordinate of the upper-left corner of the drawing bitmap object;

The int nwidth represents the width of the area where the bitmap target is drawn;

The int nheight represents the height of the region to draw the bitmap target;

Cdc* PSRCDC represents the device description table of the storage source bitmap;

The int xsrc represents the x-coordinate of the upper-left corner of the source bitmap;

int YSRC represents the y-coordinate of the upper-left corner of the source bitmap;

The DWORD Dwrop represents the grid operation sign;


The values and values of the Dwrop are described in the following table:

Value

Describe

Blackness

Fills the destination rectangle with a color indexed in the palette of 0 (the default is black).

Captureblt

Fills the destination rectangle with a color indexed in the palette of 0 (the default is black).

Dstinvert

Color the target rectangle.

Mergecopy

Mixes the colors in the source rectangle with the brush selected by the current target device environment through logic and operation.

Mergepaint

Blends the colors of the source rectangle with the color of the destination rectangle through logic or manipulation.

Nomirrorbitmap

Prevents bitmap mirroring from flipping.

Notsrccopy

The source rectangle is copied back to the destination rectangle.

Notsrcerase

Blends the color of the source rectangle with the destination rectangle after the logic or operation, and then the color is reversed.

Patcopy

Copy the brush selected by the target device environment to the destination bitmap.

Patinvert

The brush selected by the target device environment is logically or manipulated with the colors in the destination rectangle and copied to the target bitmap.

Patpaint

The brush selected by the target device environment is logically or manipulated against the color of the source rectangle, and the result is logically or manipulated with the color of the destination rectangle.

Srcand

The color of the source rectangle and the destination rectangle is logically and manipulated.

Srccopy

Directly copy the source rectangle to the destination rectangle, the most commonly used to draw bitmap properties.

Srcerase

The inverse color of the target rectangle is logically and manipulated with the color of the source rectangle.

Srcinvert

The color of the source rectangle is logically different from the destination rectangle or manipulated.

Srcpaint

To logically or manipulate the color of the source rectangle with the destination rectangle.

Whiteness

Fills the destination rectangle with a color indexed in the palette of 1 (the default is white).

The following is the entire implementation process:

1 Create a bitmap with the same size as the one you want to draw the image as a "mask" bitmap (maskbmp);

2 Store the newly created mask bitmap in the Device Description table (MASKDC) of the mask bitmap;

3) The Memory Device description table (MEMDC) of the background set to "Transparent Color" (SetBkColor (RGB (?,?,?)), that is, do not need to display the color;

4 Copy and paste the bitmap into the device description table of the mask bitmap, at this time, the bitmap in the Mask Bitmap Device Description table is the same as the bitmap in the Bitmap Device description table (Maskdc.bitblt (...)), and a mask operation is performed to reflect the mask attribute. That is, the setting of the same background set to reflect the white, the different embodiment of black. (procedures are described in detail below.)

5 The memory DC picture and the actual window background color (set to white) to do logical or (keywords: srcinvert) operation and in the actual window image;

6 The Mask DC diagram and the actual window background color to do the logic (keyword: srcand) operation, and in the window image;

7) Repeat step fifth.

Here's a demo:

First of all, we will put the picture is as follows a picture (that is, black background Red Square):


We want to make it transparent and then display it on the window.

Description:

The picture width is defined as const int bmpwidth = 182,

Height is defined as const int bmpheight = 172;


First step: Create a mask diagram with the following code:

[CPP] view plain copy print?   CBitmap Maskbmp; Maskbmp.createbitmap (Bmpwidth, Bmpheight, 1, 1,null);

<span style= "FONT-SIZE:18PX;" >cbitmap maskbmp;
Maskbmp.createbitmap (Bmpwidth, Bmpheight, 1, 1,null);  </span>

CreateBitmap Function Description:

Parameter 1 nwidth: refers to the location chart width, units in pixels.

Parameter 2 nheight: refers to the position chart height, the unit is the pixel.

Parameter 3 cplanes: Specifies the number of color bit surfaces that the device uses.

Parameter 4 Cbitsperpel: Specifies the number of bits (bits) that are used to distinguish the color of a single pixel point.

Parameter 5 lpvbits: pointers to color data arrays. These color data are used to set the color of pixels within a rectangular region. Each scan line in a rectangular area must be an integer multiple of two bytes (the insufficient portion is filled with 0). If this argument is null, the newly generated bitmap is not initialized.

Step Two: Select the Mask bitmap you created into the device description table of the mask bitmap

As follows:

[CPP] view plain copy print? cdc maskdc;                   <span style= "white-space: pre;" >               </span>  //defines the Mask dc   Maskdc.createcompatibledc (NULL);    <span style= "White-space:  pre; " >         </span>//Initialization Device dc   CBitmap * Oldmaskbmp = maskdc.selectobject (&maskbmp);           //The Mask bitmap and returns the old bitmap   

<span style= "FONT-SIZE:18PX;" >CDC MASKDC;                  <span style= "WHITE-SPACE:PRE;" >				</span>//define Mask DC
Maskdc.createcompatibledc (NULL);   <span style= "WHITE-SPACE:PRE;" >			</span>//Initialization equipment DC
CBitmap *oldmaskbmp = Maskdc.selectobject (&maskbmp);          Deposit The Mask bitmap and return the old bitmap </span>

Step Three: set the background color of the memory device description table to transparent (that is, to achieve a transparent background color, as this example is black)

[CPP] View Plain copy print? Cbitmap bmpsquare; <span style= "white-space: pre;" >         </span>//defines a CBitmap object that stores the diagram (ID idc_square) that will be drawn    Bmpsquare.loadbitmap (Idc_square);              //load bitmap        cdc memdc;                    <span style= "White-space : pre; " >           </span>//Create memory dc   Memdc.createcompatibledc (NULL);                  //initialization dc   Cbitmap *oldmembmp = memdc.selectobject (& Bmpsquare)         //the bitmap into a memory DC and returns the old bitmap    Memdc.setbkcolor ( RGB (0,0,0)); &NBSP;&NBSP;&NBSP;&NBsp;           <span style= "white-space:  Pre; " >        </span> //sets the background color of the memory DC to black   

<span style= "FONT-SIZE:18PX;" >cbitmap Bmpsquare; <span style= "WHITE-SPACE:PRE;" >			</span>//defines a CBitmap object that stores the graph (id idc_square)
bmpsquare.loadbitmap (Idc_square) that will be drawn;             Load bitmap
 
CDC MEMDC;                   <span style= "WHITE-SPACE:PRE;" >			</span>//Create memory DC
MEMDC.CREATECOMPATIBLEDC (NULL);                 Initialization of DC
CBitmap *oldmembmp = Memdc.selectobject (&bmpsquare);        The bitmap is stored in the memory DC and the old bitmap
memdc.setbkcolor (RGB (0,0,0)) is returned;               <span style= "WHITE-SPACE:PRE;" >		</span>//Set the background color of the memory DC to black </span>

Step Fourth: bitmap The memory dc in the mask DC

[CPP] view plain copy print? Maskdc.bitblt (0, 0, bmpwidth, Bmpheight, &MEMDC, 0,0, srccopy);

<span style= "FONT-SIZE:18PX;" >maskdc.bitblt (0, 0, bmpwidth, Bmpheight, &MEMDC, 0,0, srccopy);</span>

Description:

MEMDC (Black Bottom Red Square): + MEMDC background color (All Black): = after mask calculation (white bottom black square):.

Now the diagram in MASKDC is: .... (White bottom black square)

 

Fifth: the memory dc in the picture and the actual window background color (set to white) to do logical or (keywords: srcinvert) operation and in the actual window to appear as follows:

[CPP] view plain copy print?            CPANITDC DC (this); Window DC DC. BitBlt (0, 0, bmpwidth, bmpheight, &MEMDC, 0, 0,srcinvert);

<span style= "FONT-SIZE:18PX;" >CPANITDC DC (this);            Window DC DC
. BitBlt (0, 0, bmpwidth, bmpheight, &MEMDC, 0, 0,srcinvert);</span>

Description:

MEMDC (Black Bottom Red Square): + Window background color (Kangbai): = XOR or after operation (white background blue-green square):.

Logical Description:

MEMDC background (black) Its RGB is: 0 0 0 0 0 0 0 0,0 0 0 0 0 0 0 0,0 0 0 0 0 0 0 0

Window background (white) is: 1 1 1 1 1 1 1 1,1 1 1 1 1 1 1 1,1 1 1 1 1 1 1 1

XOR or operation (same as 0 different for 1): 1 1 1 1 1 1 1 1,1 1 1 1 1 1 1 1,1 1 1 1 1 1 1-1 (white)

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

MEMDC box (red) is: 1 1 1 1 1 1 1 1,0 0 0 0 0 0 0 0,0 0 0 0 0 0 0 0

Window background (white) is: 1 1 1 1 1 1 1 1,1 1 1 1 1 1 1 1,1 1 1 1 1 1 1 1

XOR/Operation Result: 0 0 0 0 0 0 0 0,1 1 1 1 1 1 1 1,1 1 1 1 1 1 1 (cyan)

 

The Sixth step: The Mask DC diagram and the actual window background color to do logical and (keyword: srcand) operation, and in the window to appear as follows:

[CPP] view plain copy print? dc. BitBlt (0, 0, bmpwidth, bmpheight, &MASKDC, 0, 0,srcand);

<span style= "FONT-SIZE:18PX;" >DC. BitBlt (0, 0, bmpwidth, bmpheight, &MASKDC, 0, 0,srcand);</span>

Description:

MASKDC in the image (white bottom black square):

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.