To enable applications to place output in memory rather than sending it to an actual device, use a special device context for bitmap operations called a memory device context. A memory DC enables the system to treat a portion of memory as a virtual device. it is an array of BITs in memory that an application can use temporarily to store the color data for bitmaps created on a normal drawing surface. because the bitmap is compatible with the device, a memory DC is also sometimes referred to as a compatible device context.
To allow applications to draw in memory rather than on the actual device, a special device content called memory device content (memory device context) is used for bitmap operations. The memory DC allows the system to allocate a part of memory for virtual DC. The memory DC is a series of bits stored in the memory for the bitmap created in the common drawing area to save color data temporarily. Because bitmaps are compatible with devices, a memory DC is also called compatible device content.
The memory DC stores bitmap images for a participant device. An application can create a memory DC by calling the createcompatibledc function.
Memory DC stores bitmaps for specific devices. An application can call the createcompatibledc function to create a memory DC. About the createcompatibledc function:
A memory DC exists only in memory. when the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel High. before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. to select a bitmap into a DC, use the createcompatiblebitmap function, specifying the height, width, and color o Rganization required. memory DC only exists in memory.
When a memory DC is created, its display area is only one pixel wide and one pixel High. Before an application uses the memory DC for plotting, a correct height and width bitmap must be selected into the memory DC. To select a bitmap to the memory DC, you can use the createcompatiblebitmap function. Use the createcompatiblebitmap function to specify the height, width, and color of the bitmap.
When a memory DC is created, all attributes are set to normal default values. the memory DC can be used as a normal DC. you can set the attributes; obtain the current settings of its attributes; and select pens, brushes, and regions.
When a memory DC is created, all attributes are set as default attributes. The memory DC can be used as a common DC. You can set attributes to get the current settings or attributes, and you can select pen, paint brush, and area.
The original bitmap in a memory DC is simply a placeholder. its dimensions are one pixel by one pixel. before an application can begin drawing, it must select a bitmap with the appropriate width and height into the DC by calling the SelectObject function. to create a bitmap of the appropriate dimensions, use the createbitmap, createbitmapindirect, or createcompatiblebitmap function. after the bitmap is selected into the memory DC, the system replaces the single-bit array with an array large enough to store color information for the specified rectangle of pixels.
The initial bitmap in the memory DC is just a simple place to place. It is only 1x1 in size. Before an application starts drawing, a bitmap of the proper height and width must be selected to the memory DC Through the SelectObject function. If you want to create a suitable bitmap, you can use createbitmap, createbitmapindirect, or the createcompatiblebitmap function. When a bitmap is selected into the memory DC, the system will replace the original single-digit array with an array that is large enough to save color information of a specific size.
Createcompatiblebitmap function: Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. if this memory device context is used in createcompatiblebitmap, the bitmap that is created is a monochrome bitmap. to create a color bitmap, use the HDC that was used to create the memory device context, as shown in the following code:
Note: When a memory DC is created, it initially only has a 1x1 monochrome bitmap. If the memory DC is used by the createcompatiblebitmap function, the returned bitmap can only be monochrome. To create a color bitmap, you must use the HDC used to create the memory DC. Just like the following code: HDC memdc = createcompatibledc (HDC );
Hbitmap membm = createcompatiblebitmap (HDC, nwidth, nheight); // note that HDC is the DC SelectObject (memdc, membm) used to create the memory DC );
When an application passes the handle returned by createcompatibledc to one of the drawing functions, the requested output does not appear on a device's drawing surface. instead, the system stores the color information for the resultant line, curve, text, or region in the array of bits. the application can copy the image stored in memory back onto a drawing surface by calling the bitblt function, identifying the memory DC as the source device context and a window or screen DC as the target device context. when the application passes the createcompatibledc
The handle returned by the function is passed to the drawing function. The requested output is not on the drawing surface of the device. On the contrary, the system saves the color information of the response line, arc, and text to the memory. Applications can use the bitblt function to copy images from the memory to a drawing surface, and use the memory DC as the source DC, window or screen DC as the target DC. All screen capture codes should be (added during wm_paint Message Processing): HDC = beginpaint (hwnd, & PS); HDC hmemdc; HDC hscreendc; hbitmap bitmap; hmemdc = createcompatibledc (HDC); hbitmap = createcompatiblebitmap (HDC, 1024,768); SelectObject (hmemdc, hbitmap );
// Obtain the screen DC and copy the content of the screen DC to the memory DC
Hscreendc = getdc (null); bitblt (hmemdc, 1024,768, hscreendc, srccopy );
// Copy the memory DC to the DC of the form, and display bitblt (HDC, 1024,768, hmemdc, srccopy) in the form );
// Create a compatible bitmap to save hbitmap = createcompatiblebitmap (HDC, 1024,768); bitmap = (hbitmap) SelectObject (hmemdc, hbitmap) in the memory DC );
// Put the bitmap into the clipboard openclipboard (hwnd); emptyclipboard ();
Setclipboarddata (cf_bitmap, bitmap );
Closeclipboard (); // release DC releasedc (null, hscreendc );
Deletedc (hmemdc );
Endpaint (hwnd, & PS );