The cclientdc class is derived from the CDC. The windows function getdc is called during the construction and releasedc is called during the analysis. This means that the device context related to the cclientdc object is the customer zone of the window. For more information about cclientdc, see the online document "Visual C ++ programmer's Guide to installing mechanical copies of the pugan side.
# Include <afxwin. h>
See CDC
Cclientdc class member
Constructor
Cclientdc constructs a cclientdc object connected to cwnd
Data Member
M_hwnd hwnd of the valid window where cclientdc is located
Member Functions
Cclientdc: cclientdc
Cclientdc (cwnd * pwnd );
Throw (cresourceexception );
Parameters
The window in which the pwnd device context will access the customer zone.
Description
This function constructs a cclientdc object, which will access the customer zone of the cwnd to which pwnd points. This constructor calls the WINDOWS function getdc. If Windows function getdc fails to be called, a cresourceexception type exception is generated. If Windows has allocated all available device context, no new device context is available. At any time, applications are competing to use the five public display contexts provided by windows.
Data Member
Cclientdc: m_hwnd
Description
M_hwnd is a protected variable that is used to construct the hwnd pointer of the m_hwnd object.
========================================================== ======================================
Several DC types and differences
Cclientdc: (client zone device context) is used for output in the customer zone. It is associated with a specific window to allow developers to access the customer zone in the target window. Its constructor contains getdc, the Destructor contains releasedc:
Cclientdc: cclientdc (cwnd * pwnd)
{
...
If (! Attach (: getdc (m_hwnd = pwnd-> getsafehwnd ())))
Afxthrowresourceexception ();
}
Cclientdc ::~ Cclientdc ()
{
...
: Releasedc (m_hwnd, detach ());
}
Usage:
Cclientdc DC (this); // This generally points to this window or the current active view.
DC. textout (10, 10, STR, str. getlength (); // use DC to output text. If it is used in cscrollview, call onpreparedc (& DC) to adjust the coordinates of the device context.
The cpaintdc is used in response to the window re-painting message (wm_paint) which is the drawing output. Cpaintdc calls beginpaint () in the constructor to obtain the device context, and CALLS endpaint () in the destructor to release the device context. In addition to releasing the device context, endpaint () is also responsible for clearing the wm_paint message from the message queue. Therefore, you must use cpaintdc when processing the window re-painting. Otherwise, the wm_paint message cannot be cleared from the message queue and will cause constant window re-painting. Cpaintdc can only be used in wm_paint message processing.
Cpaintdc: cpaintdc (cwnd * pwnd)
{
...
If (! Attach (: beginpaint (m_hwnd = pwnd-> m_hwnd, & m_ps )))
Afxthrowresourceexception ();
}
Cpaintdc ::~ Cpaintdc ()
{
...
: Endpaint (m_hwnd, & m_ps );
Detach ();
}
Cwindowdc: associates a specific window and allows developers to draw any part of the target window, including the border and title. This DC is sent together with the wm_ncpaint message.
Differences between cwindowdc, cclientdc, and cpaintdc:
Cwindowdc can draw images in non-customer areas, while cclientdc and cpaintdc can only draw images in customer areas.
In cwindowdc, the coordinate origin is in the upper left corner of the screen, and in cclientdc and cpaintdc, the coordinate origin is in the upper left corner of the customer zone.
Difference between cclientdc and cpaintdc:
Cpaintdc objects are generally used in onpaint to respond to Windows message wm_paint. The painting is automatically completed and re-painted throughout the window,
Maintain the integrity of the original window.
When the cclientdc application does not respond to the Windows message wm_paint, it is drawn in real time.
========================================================== ======================================
Preliminary analysis of the plot under MFC in VC
Author: zieckey (zieckey@yahoo.com.cn)
All rights reserved!
First, let's draw a straight line by moving the mouse.
The following two messages are captured: wm_lbuttondown and wm_lbuttonup.
Responds to the starting point of the wm_lbuttondown message record line, responds to the end point of the wm_lbuttonup message record line, and draws a straight line.
Okay. Let's see how to respond.
Void cdrawview: onlbuttondown (uint nflags, cpoint point)
{
// MessageBox ("left button clicks drawview ");
M_ptorigin = point; // the starting point of an internal variable to save the straight line is defined first.
Cview: onlbuttondown (nflags, point );
}
Next let's take a look at the end point of the line record in the response wm_lbuttonup message and draw a line.
// Method 1
Void cdrawview: onlbuttonup (uint nflags, cpoint point)
{
HDC;
HDC =: getdc (m_hwnd); // call the global function
Movetoex (HDC, m_ptorigin.x, m_ptorigin.y, 0 );
Lineto (HDC, point. X, point. y );
: Releasedc (m_hwnd, HDC );
Cview: onlbuttonup (nflags, point );
}
// Method 2
Void cdrawview: onlbuttonup (uint nflags, cpoint point)
{
CDC * PDC = getdc ();
PDC-> moveTo (m_ptorigin );
PDC-> lineto (point );
Releasedc (PDC );
}
// Method 3
Void cdrawview: onlbuttonup (uint nflags, cpoint point)
{
Cclientdc DC (this); // getdc is called when the cclientdc object is constructed.
Releasedc, which can only access the customer Zone
Cclientdc DC (getparent ());
DC. moveTo (m_ptorigin );
DC. lineto (point );
}
// Method 4
Void cdrawview: onlbuttonup (uint nflags, cpoint point)
{
Cwindowdc DC (this); // cwindowdc can access the customer zone and non-customer Zone
DC. moveTo (m_ptorigin );
DC. lineto (point );
}
Void cdrawview: onlbuttonup (uint nflags, cpoint point)
{
Cwindowdc DC (getdesktopwindow (); // you can now access the Desktop
DC. moveTo (m_ptorigin );
DC. lineto (point );
}
Continuous Line Drawing:
Train of Thought: The mouse moves the signal to be captured, and then responds to the signal at any time to draw a line
Set a bool variable m_bdraw to determine whether the left mouse button is pressed.
// Draw continuous lines
Void cdrawview: onmousemove (uint nflags, cpoint point)
{
If (m_bdraw)
{
Cclientdc DC (this );
DC. moveTo (m_ptorigin); // move to the original Vertex
DC. lineto (point); // draw a straight line
M_ptorigin = point; // assign the current vertex to the original coordinate for the next call
}
Cview: onmousemove (nflags, point );
}
// Change the paint brush color
Void cdrawview: onmousemove (uint nflags, cpoint point)
{
If (m_bdraw)
{
Cclientdc DC (this );
Cpen pen (ps_solid, 1, RGB (255,128,128 ));
Cpen * poldpen = Dc. SelectObject (& pen );
DC. moveTo (m_ptorigin );
DC. lineto (point );
M_ptorigin = point;
DC. SelectObject (poldpen );
}
Cview: onmousemove (nflags, point );
}
// Fan type
Void cdrawview: onmousemove (uint nflags, cpoint point)
{
If (m_bdraw)
{
Cclientdc DC (this );
Cpen pen (ps_solid, 1, RGB (255,128,128 ));
Cpen * poldpen = Dc. SelectObject (& pen );
DC. moveTo (m_ptorigin );
DC. lineto (m_ptold );
DC. moveTo (m_ptorigin );
DC. lineto (point );
M_ptold = point;
DC. SelectObject (poldpen );
}
Cview: onmousemove (nflags, point );
}
// Draw a fan with Edges
Void cdrawview: onmousemove (uint nflags, cpoint point)
{
If (m_bdraw)
{
Cclientdc DC (this );
Cpen pen (ps_solid, 1, RGB (255,128,128 ));
Cpen * poldpen = Dc. SelectObject (& pen );
DC. moveTo (m_ptorigin );
DC. lineto (m_ptold );
DC. moveTo (m_ptorigin );
DC. lineto (point );
DC. moveTo (m_ptold );
DC. lineto (point );
M_ptold = point;
DC. SelectObject (poldpen );
}
Cview: onmousemove (nflags, point );
}
// Check the drawing mode setting method.
Void cdrawview: onmousemove (uint nflags, cpoint point)
{
If (m_bdraw)
{
Cclientdc DC (this );
DC. setrop2 (r2_black); // set the drawing mode, always draw a black image
Cpen pen (ps_solid, 1, RGB (255,128,128 ));
Cpen * poldpen = Dc. SelectObject (& pen );
DC. moveTo (m_ptorigin );
DC. lineto (m_ptold );
DC. moveTo (m_ptorigin );
DC. lineto (point );
DC. moveTo (m_ptold );
DC. lineto (point );
M_ptold = point;
DC. SelectObject (poldpen );
}
Cview: onmousemove (nflags, point );
}
Now we know the general plotting method.
========================================================== ======
Image File Operations
1. display a bitmap:
Int cthreadpooldlg: showface (INT xpos, int ypos, cstring filepath)
{
// Todo: add your control notification handler code here
Cclientdc * PDC = new cclientdc (this );
Cbitmap bitmap;
Hbitmap = (hbitmap) LoadImage (null, filepath, image_bitmap, 0, 0, lr_loadfromfile );
Bitmap. Attach (hbitmap );
CDC dccompatible;
Dccompatible. createcompatibledc (PDC );
Dccompatible. SelectObject (& Bitmap );
Bitmap bminfo;
Bitmap. GetObject (sizeof (bminfo), & bminfo );
PDC-> bitblt (xpos, ypos, bminfo. bmwidth, bminfo. bmheight, & dccompatible, 0, 0, srccopy );
Return 1;
}