Device context (DC) and memory: bitmap and image brush attributes in device context are fragmented.

Source: Internet
Author: User
Tags bitmask

Device content

As we all know, the device context is a Windows data structure that contains information about the drawing properties of a device, such as a display or printer. All painting calls are performed through the device context object, including the paint brush, bitmap, and painting brush. The device content is related to a specific display device. For a video display, the device content is always related to a specific window on the display.

Some values in the device content are graphic attributes that define the working details of the GDI drawing function. For example, textout, and the properties of the device content determine the color of the text and the background color of the text, X coordinate, Y coordinate map to the area to be displayed, and the font used by the system when the text is displayed.

When a program needs to plot, it first needs to get the device content handle. After obtaining the handle, Windows fills in the internal device content structure with a set attribute. The device content handle must be released after painting.

Memory device content

By using a device environment especially for bitmap operations, applications can perform output operations in the memory rather than sending them to the actual device. The memory device environment allows the system to use some memory as virtual devices. It is a bit array in the memory, so that the application can use it to create a bitmap on a standard drawing surface to temporarily store color data. Because bitmap is compatible with devices, the memory device environment may be compatible with devices.

The memory device environment stores bitmap images as a special environment. Applications can call createcompatibledc to create a memory device environment.

The initial bitmap in the memory device environment is just a simple placeholder. The size is 1*1 pixel. Before drawing an application, you must call SelectObject to select a bitmap of the proper width and height to the memory environment. To create an appropriate bitmap, you can use createbitmap, createbitmapindirect, or createcompatiblebitmap. After the bitmap is selected as the memory device environment, the system replaces the unit array with a large enough array to store the color information of the specified rectangle pixel.

When an application passes a handle returned by createcompatibledc to the plotting function, the requested output is not displayed on the drawing surface of the device. On the contrary, the system stores the color information of the line segment, curve, text, or area in an array. Applications can copy images stored in the memory to the drawing surface by calling the bitblt function, and use the memory device environment as the source device environment, window or screen device environment as the target device environment.

Example 1:

/* ----------------------------------------- <Br/> bitmask. C -- bitmap masking demonstration <br/> (c) Charles Petzold, 1998 <br/> ------------------------------------------- */</P> <p> # include <windows. h> </P> <p> lresult callback wndproc (hwnd, uint, wparam, lparam); </P> <p> int winapi winmain (hinstance, hinstance hprevinstance, <br/> pstr szcmdline, int icmdshow) <br/> {<br/> static tchar Szappname [] = text ("bitmask"); <br/> hwnd; <br/> MSG; <br/> wndclass; </P> <p> wndclass. style = cs_hredraw | cs_vredraw; <br/> wndclass. lpfnwndproc = wndproc; <br/> wndclass. cbclsextra = 0; <br/> wndclass. cbwndextra = 0; <br/> wndclass. hinstance = hinstance; <br/> wndclass. hicon = loadicon (null, idi_application); <br/> wndclass. hcursor = loadcursor (null, idc_arrow); <Br/> wndclass. hbrbackground = (hbrush) getstockobject (ltgray_brush); <br/> wndclass. lpszmenuname = NULL; <br/> wndclass. lpszclassname = szappname; </P> <p> If (! Registerclass (& wndclass) <br/>{< br/> MessageBox (null, text ("this program requires Windows NT! "), <Br/> szappname, mb_iconerror); <br/> return 0; <br/>}</P> <p> hwnd = createwindow (szappname, text ("bitmap masking Demo"), <br/> ws_overlappedwindow, <br/> cw_usedefault, cw_usedefault, <br/> cw_usedefault, cw_usedefault, <br/> null, null, hinstance, null); </P> <p> showwindow (hwnd, icmdshow); <br/> updatewindow (hwnd ); </P> <p> while (getmessage (& MSG, null, 0, 0) <br/>{< br/> translatemessage (& MSG ); <br/> dispatchmessage (& MSG); <br/>}< br/> return MSG. wparam; <br/>}</P> <p> lresult callback wndproc (hwnd, uint message, wparam, lparam) <br/>{< br/> static hbitmap hbitmapimag, hbitmapmask; <br/> static hinstance; <br/> static int cxclient, cyclient, cxbitmap, and cybitmap; <br/> Bitmap bitmap; <br/> HDC, hdcmemimag, hdcmemmask; <br/> int X, Y; <br/> paintstruct pS; <br/> hbrush; <br/> hbitmap; <br/> rect; </P> <p> switch (Message) <br/>{< br/> case wm_create: <br/> hinstance = (lpcreatestruct) lparam)-> hinstance; </P> <p> // load the original image and get its size </P> <p> hbitmapimag = loadbitmap (hinstance, text ("Matthew ")); <br/> hbrush = createpatternbrush (hbitmapimag); <br/> GetObject (hbitmapimag, sizeof (Bitmap), & Bitmap); <br/> cxbitmap = bitmap. bmwidth; <br/> cybitmap = bitmap. bmheight; </P> <p> // select the original image into a memory DC </P> <p> hdcmemimag = createcompatibledc (null ); <br/> SelectObject (hdcmemimag, hbitmapimag ); </P> <p> // create the monochrome mask bitmap and memory DC </P> <p> hbitmapmask = createbitmap (cxbitmap, cybitmap, 1, 1, null ); <br/> hdcmemmask = createcompatibledc (null); <br/> SelectObject (hdcmemmask, hbitmapmask ); </P> <p> // color the mask bitmap black with a white ellipse </P> <p> SelectObject (hdcmemmask, getstockobject (black_brush )); <br/> rectangle (hdcmemmask, 0, 0, cxbitmap, cybitmap); <br/> SelectObject (hdcmemmask, getstockobject (white_brush); <br/> ellipse (hdcmemmask, 0, 0, cxbitmap, cybitmap); </P> <p> // mask the original image </P> <p> bitblt (hdcmemimag, 0, 0, cxbitmap, cybitmap, <br/> hdcmemmask, 0, 0, srcand); </P> <p> deletedc (hdcmemimag); <br/> deletedc (hdcmemmask ); <br/> return 0; </P> <p> case wm_size: <br/> cxclient = loword (lparam); <br/> cyclient = hiword (lparam ); <br/> return 0; </P> <p> case wm_paint: <br/> hbitmap = loadbitmap (hinstance, text ("Matthew ")); <br/> hbrush = createpatternbrush (hbitmap); <br/> HDC = beginpaint (hwnd, & PS); <br/> rect. left = 0; <br/> rect. top = 0; <br/> rect. right = 200; <br/> rect. bottom = 320; <br/> rectangle (HDC, rect. left, rect. top, rect. right, rect. bottom); <br/> fillrect (HDC, & rect, hbrush ); <br/> // select bitmaps into memory DCS </P> <p> hdcmemimag = createcompatibledc (HDC); <br/> SelectObject (hdcmemimag, hbitmapimag ); </P> <p> hdcmemmask = createcompatibledc (HDC); <br/> SelectObject (hdcmemmask, hbitmapmask ); </P> <p> // center image </P> <p> X = (cxclient-cxbitmap)/2; <br/> Y = (cyclient-cybitmap) /2; </P> <p> // do the bitblts </P> <p> // bitblt (HDC, X, Y, cxbitmap, cybitmap, hdcmemmask, 0, 0, 0x220326); <br/> // bitblt (HDC, X, Y, cxbitmap, cybitmap, hdcmemimag, 0, 0, srcpaint ); <br/> bitblt (HDC, X, Y, cxbitmap, cybitmap, hdcmemimag, 0, 0, srccopy); <br/> deletedc (hdcmemimag ); <br/> deletedc (hdcmemmask); <br/> deleteobject (hbrush); <br/> endpaint (hwnd, & PS); <br/> return 0; </P> <p> case wm_destroy: <br/> deleteobject (hbitmapimag); <br/> deleteobject (hbitmapmask); <br/> postquitmessage (0 ); <br/> return 0; <br/>}< br/> return defwindowproc (hwnd, message, wparam, lparam); <br/>}< br/>

In Windows program design, the example in the bitmap section is slightly modified. The running result shows that the bitmap has changed, that is, it is no longer the image that was just imported from the outside (the image owner is the nephew of the author Charles ).

First, hbitmapimag = loadbitmap (hinstance, text ("Matthew"); loads a bitmap

Hdcmemimag = createcompatibledc (null );

SelectObject (hdcmemimag, hbitmapimag );

Secondly, bitblt (hdcmemimag, 0, 0, cxbitmap, cybitmap, hdcmemmask, 0, 0, srcand); bitmap is modified, at this time, the modification is undoubtedly a bitmap in the memory device content, the modification method is very simple and clever, but it is very useful (to create a monochrome bitmap, in fact, we can use this idea to create a transparent bitmap, which is not too long here)

Finally, we output a bitmap in the center of the customer area to find that the output is a modified bitmap. In addition, do not be disturbed by the bitmap image brush created in the code, just to achieve the comparison effect by Combining Example 2.

A simple conclusion can be drawn: When a bitmap is selected into the content of the memory device, it is actually selected into the bitmap itself, rather than the copy of the bitmap, therefore, modifying the bitmap in the content of the memory device will inevitably give feedback to the bitmap itself in a timely manner. Therefore, the output bitmap is the modified bitmap.

Example 2;

Bricks3.c </P> <p>/* restart </P> <p> bricks3.c -- createpatternbrush demonstration </P> <p> (c) Charles Petzold, 1998 </P> <p> ----------------------------------------------------------------------------- */</P> <p> # include <windows. h> </P> <p> lresult callback wndproc (hwnd, uint, wparam, lparam); </P> <p> int winapi winmain (hinstance, hin Stance hprevinstance, </P> <p> pstr sz1_line, int icmdshow) </P> <p >{</P> <p> static tchar szappname [] = text ("bricks3"); </P> <p> hbitmap; </P> <p> hbrush; </P> <p> hwnd; </P> <p> MSG; </P> <p> wndclass; </P> <p> hbitmap = loadbitmap (hinstance, text ("Bricks"); </P> <p> hbrush = createpatternbrush (hbitmap ); </P> <p> deleteobject (hbitmap); </P> <p> wndclass. style = cs_hre Draw | cs_vredraw; </P> <p> wndclass. lpfnwndproc = wndproc; </P> <p> wndclass. cbclsextra = 0; </P> <p> wndclass. cbwndextra = 0; </P> <p> wndclass. hinstance = hinstance; </P> <p> wndclass. hicon = loadicon (null, idi_application); </P> <p> wndclass. hcursor = loadcursor (null, idc_arrow); </P> <p> wndclass. hbrbackground = hbrush; </P> <p> wndclass. lpszmenuname = NULL; </P> <p> wndclass. lpszclassname = szappna Me; </P> <p> If (! Registerclass (& wndclass) </P> <p >{</P> <p> MessageBox (null, text ("this program requires Windows NT! "), </P> <p> szappname, mb_iconerror); </P> <p> return 0; </P> <p >}</P> <p> hwnd = createwindow (szappname, text ("createpatternbrush Demo"), </P> <p> ws_overlappedwindow, </P> <p> cw_usedefault, cw_usedefault, </P> <p> cw_usedefault, cw_usedefault, </P> <p> null, null, hinstance, null ); </P> <p> showwindow (hwnd, icmdshow); </P> <p> updatewindow (hwnd); </P> <p> while (getmessage (& MSG, null, 0, 0) </P> <p >{</P> <p> translatemessage (& MSG ); </P> <p> dispatchmessage (& MSG); </P> <p >}</P> <p> deleteobject (hbrush ); </P> <p> return MSG. wparam; </P> <p >}</P> <p> lresult callback wndproc (hwnd, uint message, wparam, lparam) </P> <p >{</P> <p> switch (Message) </P> <p >{</P> <p> case wm_destroy: </P> <p> postquitmessage (0); </P> <p> return 0; </P> <p >}</P> <p> return defwindowproc (hwnd, message, wparam, lparam); </P> <p >}< br/>

A brick example in this book uses bitmap painting.

We are only interested in three lines of code:

Hbitmap = loadbitmap (hinstance, text ("Bricks "));

Hbrush = createpatternbrush (hbitmap );

Deleteobject (hbitmap );
Load the bitmap, create a bitmap image brush, and immediately delete the bitmap.

The result is correct, and the image painting itself is not affected because the bitmap used as the image painting is deleted.

In this example, we can also conclude that the bitmap is used to create a paint brush. In this case, the bitmap in the paint brush is a copy of the external bitmap. Deleting or deleting the bitmap does not affect each other because it is already independent of each other.

Let's get to know about DC in this way:

Each DC is associated with many objects, such as bitmaps and image brushes. DC obtains the pointers of these objects, and subsequent operations on DC are performed on the associated objects.

Creating a paint brush creates a paint brush based on the content of the bitmap. The content of the paint brush is copied from the bitmap. After the image is created, this bitmap has no connection with this paint brush and is independent from each other. Deleting the bitmap will not affect the painting brush.

Example 3 is used to prove the above points:

/* ----------------------------------------- <Br/> bitmask. C -- bitmap masking demonstration <br/> (c) Charles Petzold, 1998 <br/> ------------------------------------------- */</P> <p> # include <windows. h> </P> <p> lresult callback wndproc (hwnd, uint, wparam, lparam); </P> <p> int winapi winmain (hinstance, hinstance hprevinstance, <br/> pstr szcmdline, int icmdshow) <br/> {<br/> static tchar Szappname [] = text ("bitmask"); <br/> hwnd; <br/> MSG; <br/> wndclass; </P> <p> wndclass. style = cs_hredraw | cs_vredraw; <br/> wndclass. lpfnwndproc = wndproc; <br/> wndclass. cbclsextra = 0; <br/> wndclass. cbwndextra = 0; <br/> wndclass. hinstance = hinstance; <br/> wndclass. hicon = loadicon (null, idi_application); <br/> wndclass. hcursor = loadcursor (null, idc_arrow); <Br/> wndclass. hbrbackground = (hbrush) getstockobject (ltgray_brush); <br/> wndclass. lpszmenuname = NULL; <br/> wndclass. lpszclassname = szappname; </P> <p> If (! Registerclass (& wndclass) <br/>{< br/> MessageBox (null, text ("this program requires Windows NT! "), <Br/> szappname, mb_iconerror); <br/> return 0; <br/>}</P> <p> hwnd = createwindow (szappname, text ("bitmap masking Demo"), <br/> ws_overlappedwindow, <br/> cw_usedefault, cw_usedefault, <br/> cw_usedefault, cw_usedefault, <br/> null, null, hinstance, null); </P> <p> showwindow (hwnd, icmdshow); <br/> updatewindow (hwnd ); </P> <p> while (getmessage (& MSG, null, 0, 0) <br/>{< br/> translatemessage (& MSG ); <br/> dispatchmessage (& MSG); <br/>}< br/> return MSG. wparam; <br/>}</P> <p> lresult callback wndproc (hwnd, uint message, wparam, lparam) <br/>{< br/> static hbitmap hbitmapimag, hbitmapmask; <br/> static hinstance; <br/> static int cxclient, cyclient, cxbitmap, and cybitmap; <br/> Bitmap bitmap; <br/> HDC, hdcmemimag, hdcmemmask; <br/> int X, Y; <br/> paintstruct pS; <br/> static hbrush; // define a paint brush to facilitate the test definition as a static variable <br/> rect; </P> <p> switch (Message) <br/>{< br/> case wm_create: <br/> hinstance = (lpcreatestruct) lparam)-> hinstance; </P> <p> // load the original image and get its size </P> <p> hbitmapimag = loadbitmap (hinstance, text ("Matthew ")); <br/> hbrush = createpatternbrush (hbitmapimag); // create a bitmap image brush using the original bitmap. <br/> GetObject (hbitmapimag, sizeof (Bitmap), & Bitmap ); <br/> cxbitmap = bitmap. bmwidth; <br/> cybitmap = bitmap. bmheight; </P> <p> // select the original image into a memory DC </P> <p> hdcmemimag = createcompatibledc (null ); <br/> SelectObject (hdcmemimag, hbitmapimag ); </P> <p> // create the monochrome mask bitmap and memory DC </P> <p> hbitmapmask = createbitmap (cxbitmap, cybitmap, 1, 1, null ); <br/> hdcmemmask = createcompatibledc (null); <br/> SelectObject (hdcmemmask, hbitmapmask ); </P> <p> // color the mask bitmap black with a white ellipse </P> <p> SelectObject (hdcmemmask, getstockobject (black_brush )); <br/> rectangle (hdcmemmask, 0, 0, cxbitmap, cybitmap); <br/> SelectObject (hdcmemmask, getstockobject (white_brush); <br/> ellipse (hdcmemmask, 0, 0, cxbitmap, cybitmap); </P> <p> // mask the original image </P> <p> bitblt (hdcmemimag, 0, 0, cxbitmap, cybitmap, <br/> hdcmemmask, 0, 0, srcand); // The bitmap changes. If the bitmap is changed, it is unknown </P> <p> deletedc (hdcmemimag ); <br/> deletedc (hdcmemmask); <br/> return 0; </P> <p> case wm_size: <br/> cxclient = loword (lparam ); <br/> cyclient = hiword (lparam); <br/> return 0; </P> <p> case wm_paint: <br/> HDC = beginpaint (hwnd, & PS); <br/> rect. left = 0; <br/> rect. top = 0; <br/> rect. right = 200; <br/> rect. bottom = 320; <br/> rectangle (HDC, rect. left, rect. top, rect. right, rect. bottom); <br/> fillrect (HDC, & rect, hbrush); // fill the area with bitmap paint brushes, test whether bitmap changes affect bitmap painting. <br/> // select bitmaps into memory DCS </P> <p> hdcmemimag = createcompatibledc (HDC ); <br/> SelectObject (hdcmemimag, hbitmapimag); </P> <p> hdcmemmask = createcompatibledc (HDC); <br/> SelectObject (hdcmemmask, hbitmapmask ); </P> <p> // center image </P> <p> X = (cxclient-cxbitmap)/2; <br/> Y = (cyclient-cybitmap) /2; </P> <p> // do the bitblts </P> <p> // bitblt (HDC, X, Y, cxbitmap, cybitmap, hdcmemmask, 0, 0, 0x220326); <br/> // bitblt (HDC, X, Y, cxbitmap, cybitmap, hdcmemimag, 0, 0, srcpaint ); <br/> bitblt (HDC, X, Y, cxbitmap, cybitmap, hdcmemimag, 0, 0, srccopy); // verify whether the bitmap is changed <br/> deletedc (hdcmemimag ); <br/> deletedc (hdcmemmask); <br/> endpaint (hwnd, & PS); <br/> return 0; </P> <p> case wm_destroy: <br/> deleteobject (hbitmapimag); <br/> deleteobject (hbitmapmask); <br/> deleteobject (hbrush); // It must be placed in wm_destroy, placed in wm_paint will be deleted and will disappear next time <br/> postquitmessage (0); <br/> return 0; <br/>}< br/> return defwindowproc (hwnd, message, wparam, lparam); <br/>}< br/>

The key changes in the Code are commented out. You need to use the Windows program to design the source files in this book, or simply select your own long and elegant image for testing.

 

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.