How to use the device description table

Source: Internet
Author: User

When a Windows program paints on a screen, printer, or other device, it does not output pixels directly to the device, but instead draws the diagram to a "display plane" in the logical sense represented by the device description table. A Device Description table (DC) is a data structure in Windows that contains all the descriptive fields required by GDI for the display interface, including connected physical devices and a wide variety of state information. Before Windows Paint, the Windows program obtains the device description table handle (HDC) from GDI and returns the handle to GDI after each call to the GDI output function. This article demonstrates how to obtain and release HDC and how to use the CDC and its derived classes.

Download the sample engineering Dcdemo specific instructions:

1, the button idc_getdc_apinull function

void CDCDemoDlg::OnGetdcApinull()
{
  HDC hDC=::GetDC(NULL);
  ::MoveToEx(hDC,0,0,NULL);
  LineTo(hDC,200,20);
  ::ReleaseDC(NULL,hDC);
}

This code specifically demonstrates how to use the API function GetDC (NULL) to get the HDC of the screen and to paint it. HDC GetDC (HWND hwnd); Function: Gets the device description table handle of the HWND window. Gets the device description table handle for the entire screen when the HWND parameter is NULL. Movetoex and LineTo demonstrate a straight line on the DC. Remember, the last call ReleaseDC release HDC resources. The int ReleaseDC (
HWND hWnd,//你要控制的那个窗口的句柄,如果你在GetDC函数传递的是NULL,现在还要传递NULL。
HDC hDC //DC的句柄
);
compiler runs the program, presses the button, discovers the screen in the upper left corner draws a line.

2, the button Idc_getdc_api function

void CDCDemoDlg::OnGetdcApi()
{
  HDC hDC=::GetDC(m_hWnd);
  ::MoveToEx(hDC,0,0,NULL);
  LineTo(hDC,200,50);
  ::ReleaseDC(m_hWnd,hDC);
}

The only difference between this code and the previous code is that the GetDC argument is no longer null, but instead the CWnd member variable m_hwnd, the handle to the dialog window. Compare the two pieces of code to run the results, in-depth understanding of API function GetDC.

3, the button Idc_getdc_cwnd function

void CDCDemoDlg::OnGetdcCwnd()
{
  CDC *pDC=GetDC();
  pDC->MoveTo(0,0);
  pDC->LineTo(200,100);
  ReleaseDC(pDC);
}

This code demonstrates the use of the GETDC function and ReleaseDC method of the MFC CWnd class, which is handy for drawing in the program window. To avoid the hassle of acquiring and releasing the Device Description table, MFC provides some CDC derived classes, such as CPAINTDC,CCLIENTDC,CWINDOWDC, that are designed to be instantiated directly. The constructors and destructors of each class call the corresponding function capture and release device description table, making it more convenient and simple.

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.