First, understand the meaning of DC. In Windows, programmers are not allowed to directly access the hardware. Its operations on the screen are done through the Environment device, that is, DC. DC means the device context. The device context is the attributes of the current form, such as the paint brush and paint brush used by the device. That is to say, it does not directly output pixels to the device, but instead draws the figure to the "display plane" in the logical sense represented by the device description table. Each window on the screen corresponds to a DC. You can think of DC as a video buffer. Operations on this buffer zone are displayed on the screen window corresponding to this buffer zone.
The device description table (DC) is a data structure in windows. It contains all the Description fields required by GDI for display interface conditions, including connected physical devices and various status information. Before drawing images in windows, the windows program obtains the device description table handle (HDC) from the GDI, and returns the handle to the GDI after each call to the GDI output function.
Function prototype:
HDC GetDC(HWND hWnd);
Hwnd: the handle of the window retrieved from the device context environment. If this value is null, getdc retrieves the device context environment of the entire screen.
After you use a normal device context to draw a picture, you must call the releasedc function to release the device context. The typical and special device context environments do not need to be released, the number of device context environments is limited by the valid memory.
In addition to the window DC, you can create your own DC, that is, it does not correspond to the window, this method is createcompatibledc, this DC is a memory buffer, through this DC, you can save the compatible window DC to this DC, that is, you can copy data between different DC through it. For example, you first create data in the DC, and then refresh the window in the DC copied to the window.
Hdesktop = getdesktopwindow (); hsf-topdc = getdc (hdesktop); // desktop window DC hmemdc = createcompatibledc (hsf-topdc); // This Is a dc compatible with the desktop window.
Creatcompatibledc () creates a memory DC compatible with the DC of the current screen, you must create such a DC in the memory that is compatible with the current device environment, that is, use this function to load the bitmap to this memory, then, the bitmap function can be used to copy the bitmap from the memory to the DC of the screen. Or the memory DC is equivalent to having a "tablecloth" with a screen. You can plot on this DC and then replace the bitmap with the original bitmap, A new bitmap is formed.
HDC CreateCompatibleDC(HDC hdc);
If you want to perform a lot of GDI function operations on the screen, if each step is directly performed on the screen DC, most of the possibilities are that the screen flashes. A good solution is to use the memory DC to operate all these operations on the memory DC first, and then perform operations on the screen in sequence.
For example, if you use bitblt to copy an image on the screen, you can directly use the DC of the screen. However, if you need to set the background (fillrect) before bilblt, this involves two screen DC operations, so that the screen will easily flash.
The memory device context environment is a device context environment that only exists in the memory. When the memory device context environment is created, its display interface is a standard monochrome pixel width and a monochrome pixel height. Before an application can use the memory device context for plotting, it must select a bitmap with the correct height and width to the device context, you can use the createcompatiblebitmap function to specify a combination of height, width, and color to meet the function call needs.
CDC m_memdc; // The memory device environment is associated with the screen device environment (compatible) m_memdc.createcompatibledc (pwindowdc); // The memory bitmap is associated with the screen (compatible ), the size is the size of the game window m_membmp .createcompatiblebitmap (pwindowdc, m_nwidth, m_nheight); // The memory device environment is associated with the memory bitmap to draw m_memdc.selectobject (& m_membmp );