GDI Drawing in MFC (2)

Source: Internet
Author: User
Tags function prototype

Two GDI drawing in MFC

the GDI drawing consists of the following steps: Gets the device environment, sets the coordinate mapping, Create a drawing tool and call the DC drawing function Drawing.

1. Get the equipment environment

(1) In SDK programming, there are two ways to get the device environment:

<1> via API function BeginPaint. When the application responds to a graphical refresh of the WM_PAINT message, the device environment is obtained primarily through the BeginPaint function, and the API function EndPaint released before the message handler function is returned.

Function prototypes are: Winuserapi HDC WINAPI beginpaint (HWND hwnd,lppaintstruct lppaint);

// The following is an example of the win API :: BeginPaint (HWND hwnd, lppaintstruct Lppaint);

Case WM_PAINT:// window client area needs repainting

{

Char sztext[]="Hello World";

Paintstruct PS;

HDC Hdc=::beginpaint (HWND,&PS);

:: TextOut (Hdc,10,10,sztext,strlen (Sztext));

:: EndPaint (HWND,&PS);

return 0;

}

MFC encapsulates the BeginPaint:

cwnd::beginpaint,cdc* BeginPaint (lppaintstruct lppaint); Equivalent to

:: BeginPaint (Cwnd::m_hwnd, lppaintstruct lppaint);

<2> via API function GetDC. In a non-WM_PAINT message handler, you need to call GetDC to get the device environment and invoke the API function ReleaseDC to release the device environment.

The function prototype is: Winuserapi HDC WINAPI GetDC (HWND hwnd);

(2) in MFC, MFC provides different types of DC classes, each of which encapsulates a DC handle, and their constructors automatically call the Get DC's API function, and the destructor automatically calls the API function that frees the DC. Therefore, an object in the program that declares an MFC device environment class automatically acquires a DC, and when the object is destroyed it automatically frees the acquired DC. The OnDraw () function created by the MFC AppWizard Application Wizard automatically supports the obtained DC.

<1> CPaintDC constructor: CPaintDC (cwnd* pWnd); Constructs an CPaintDC object (pwnd points to the CWnd object to which a CPaintDC object belongs) and prepares the application window for painting.

BeginPaint

void Cview::onpaint ()

{

CPaintDC DC (this); //device context for painting

//Todo:add your message Handler code here

OnPrepareDC (&DC);

OnDraw (&DC)

}

When we change the window size, move the window, or restore the previously overwritten section, the application window receives a WM_PAINT message sent by the Windows system and calls the OnPaint function of the base class CView or the message handler function we added ourselves OnPaint. We can redraw the re-visible part of the window in the OnPaint function (), but the simple approach is to redraw the entire window. In the above code, the OnDraw function is called by the OnPaint function of the base class CView, so the application often draws the view in the OnDraw function.

<2>cclientdc constructor: CClientDC (cwnd* pWnd); Constructs a CClientDC object that accesses the client area of the CWnd that pwnd points to.

//  left mouse button event handling

void Cexview::onlbuttondown (UINT nflags, CPoint Point)

{

//TODO: Add Message Handler code and/ or call default values here

CCLIENTDC DC (this); // Define customer Area equipment Environment

dc. LineTo (point); // Draw Segment

}

CCLIENTDC represents the display context for the client area of the window, which invokes the API function GetDC at construction time and m_hwnd the handle of the current window as a function parameter, and invokes the API function ReleaseDC at the time of the destructor. When the customer goes to the drawing, the CClientDC class is used to define a client area device environment handle.

Sometimes you need access to a Window object associated with a client device environment, and you can pass the CCLIENTDC member m_hwnd handle to a Window object through the Attach function, which is a window associated with the client area device environment.

Cwnd::attach,bool Attach (HWND hwndnew);

Description: Connects a Windows window to a CWnd object.
Return value: If successful, a value other than 0 is returned, otherwise 0 is returned.
Parameter: hwndnew specifies a handle to the Windows window

<3>CWINDOWDC constructor: CWINDOWDC (cwnd* pWnd) constructs a CWINDOWDC object that accesses the entire screen area of the CWnd object that the pWnd points to, including the customer area and the non-client area. For example, when we do a screen saver, we generally use the entire screen area as the drawing area.

--------------------to Be Continued------------------

GDI Drawing in MFC (2)

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.