Source: http://www.cnblogs.com/volnet/articles/cpp_about_draw.html
First, record the point of the mouse, so add the code in wm_lbuttondown:
Void cdrawview: onlbuttondown (uint nflags, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
M_ptorigin = point;
Cview: onlbuttondown (nflags, point );
}
Record the position of the wm_lbuttonup point again to complete the following 1-3 tasks.
1. Draw a line
Void cdrawview: onlbuttonup (uint nflags, cpoint point)
{
// Todo: add the message processing program code and/or call the default value here
// Draw a black line (method 1)
/* HDC; // declare an HDC
HDC =: getdc (m_hwnd); // use the global function HDC getdc (hwnd); to obtain the device handle. Because it is a global function, add ":" to the front of the function.
Movetoex (HDC, m_ptorigin.x, m_ptorigin.y, null );
Lineto (HDC, point. X, point. y );
: Releasedc (m_hwnd, HDC); // release the HDC */
/* Method 2 Description: Use the CDC class (do all drawing through the member functions of a CDC object. the class provides member functions for device-context operations, working with drawing tools, type-safe Graphics Device Interface (GDI) object selection, and working with colors and palettes .)
There are two functions under it.
MoveTo
Moves the current position.
Polybetiller
Draws one or more B 'SPS. The current position is neither used nor updated.
Compared with method 1, method 2 does not need to define the form handle */
// Draw a black line (method 2)
/* CDC * PDC = getdc ();
PDC-> moveTo (m_ptorigin );
PDC-> lineto (point );
Releasedc (PDC );*/
/* Method 3 Description: The cwindowdc class is derived from CDC. it callthe Windows functions getwindowdc at construction time and releasedc at destruction time. this means that a cwindowdc object accesses the entire screen area of a cwnd (both client and nonclient areas ).)
The second advantage of this method is that the HDC has been implicitly obtained when the getwindowdc object is instantiated and it is automatically released at the end of the object's lifecycle to relieve our burden.
*/
// Draw a black line (method 3)
/* Cwindowdc DC (this); // This indicates the handle of the current form
DC. moveTo (m_ptorigin );
DC. lineto (point );*/
/* It is worth noting that in cwindowdc (this); this represents the handle of the current form, so we only need to pass in the form pointer to be painted. In this example, because the message is based on the view class, it is valid to draw the View window in the program. To change it to the mainframe class, because the mainframe class is the parent window of the View class, we only need to obtain the pointer of the parent window of the current class, that is, replace the current this with getparent (). If you want to paint on the entire Windows desktop, you can get the desktop pointer getdesktopwindow (). The code is implemented as follows :*/
// Draw a black line (method 3) (based on the mainframe class box)
/* Cwindowdc DC (getparent ());
DC. moveTo (m_ptorigin );
DC. lineto (point );*/
// Draw a black line (method 3) (based on Windows desktop)
/* Cwindowdc DC (getdesktopwindow ());
DC. moveTo (m_ptorigin );
DC. lineto (point );*/
/* Method 4 Description: The cclientdc class is derived from CDC and takes care of calling the Windows functions getdc at construction time and releasedc at destruction time. this means that the device context associated with a cclientdc object is the client area of a window .)
The method is similar to that of cwindowdc.
*/
// Draw a black line (Method 4)
/* Cclientdc (this );
DC. moveTo (m_ptorigin );
DC. lineto (point );*/
2. Change the color
/*************************************** **************************************** **********/
// Draw a red line
/* Cpen pen (ps_solid, 1, RGB (234,23, 53); // defines a cpen Class Object and uses the RGB macro to change its color
Cclientdc DC (this );
Cpen * poldpen = Dc. SelectObject (& pen); // select it from the device table
DC. moveTo (m_ptorigin );
DC. lineto (point );
DC. SelectObject (poldpen); // restores the default value changed in the previous device table */
/* Method Description: Change the attributes of the paint brush cpen and select them into the device table. Then, draw a line. The default value of the device table is black */
3. Draw a rectangle
/*************************************** **************************************** **********/
// Draw a rectangle Without Borders
/* Method Description: Fill the rectangle with the fillrect function. The filled content is the painting brush content, that is, the pointer of the painting brush object. */
// Draw a rectangle Without Borders
/* Cbrush brush (RGB (5,255,255 ));
Cclientdc DC (this );
DC. fillrect (crect (m_ptorigin, point), & brush );*/
// Use bitmap as the image brush Resource
// Load the bitmap to the image brush
/* Cbitmap bitmap;
Bitmap. loadbitmap (idb_bitmap1);/* Add a bitmap resource from "project" and "Add Resource" and select its ID number to the loadbitmap () function. */
Cbrush brush (& Bitmap );
Cclientdc DC (this );
DC. fillrect (crect (m_ptorigin, point), & brush );*/
// Draw a rectangle with a border
/* Method Description: Use the rectangle method to draw a rectangle. The cbrush and cpen classes are responsible for the attributes of the inside rectangle and the border of the rectangle */
// The White Rectangle in the painting
/* Cclientdc (this );
DC. rectangle (crect (m_ptorigin, point ));*/
// The center of the painting is a transparent rectangle.
/* Cclientdc (this );
Cbrush * pbrush = cbrush: fromhandle (hbrush) getstockobject (null_brush ));
Cbrush * poldbrush = Dc. SelectObject (pbrush );
DC. rectangle (crect (m_ptorigin, point ));
Poldbrush = pbrush ;*/
// A rectangle in any color with a transparent side in the painting
/* Cclientdc (this );
Cpen pen (ps_solid, 1, RGB (234,23, 53 ));
Cpen * poldpen = Dc. SelectObject (& pen );
Cbrush * pbrush = cbrush: fromhandle (hbrush) getstockobject (null_brush ));
Cbrush * poldbrush = Dc. SelectObject (pbrush );
DC. rectangle (crect (m_ptorigin, point ));
DC. SelectObject (poldpen );
DC. SelectObject (poldbrush );*/
4. draw any line
/* To record the position of each vertex, you must add the following code to the Message wm_mousemove to record every point in the movement.
However, you must use the left mouse button to press this action to record the event. Otherwise, the move mouse event will include the case where the left mouse button is not pressed. Therefore, we must define a global variable to record whether the left mouse button is pressed. Therefore, define a bool variable to record whether to press or not. In C... Add a private Boolean m_bdraw; variable to the view. h header file. In wm_lbuttondown, change the value to m_bdraw = true; In wm_lbuttonup, change m_bdraw = false; and add the following code to wm_mousemove. */
Cclientdc DC (this );
If (m_bdraw = true)
{
DC. moveTo (m_ptorigin );
DC. lineto (point );
M_ptorigin = point;
}
// Draw slice lines
/* Cclientdc (this );
If (m_bdraw = true)
{
DC. moveTo (m_ptorigin );
DC. lineto (point );
}*/
// Draw a fan line with an envelope
/* The information of the old vertex needs to be recorded. Therefore, the value of m_ptold in wm_lbuttondown is changed in wm_mousemove after it is assigned as a point */
/* Cclientdc (this );
If (m_bdraw = true)
{
DC. moveTo (m_ptorigin );
DC. lineto (m_ptold );
DC. moveTo (m_ptold );
DC. lineto (point );
M_ptold = point;
}*/
// Introduces int setrop2 (INT ndrawmode). This method changes the painting parameter by changing the value of ndrawmode.
/* Cclientdc (this );
If (m_bdraw = true)
{
DC. setrop2 (r2_black );
DC. moveTo (m_ptorigin );
DC. lineto (m_ptold );
DC. moveTo (m_ptold );
DC. lineto (point );
M_ptold = point;
}*/
/* Msdn: sets the current drawing mode.
Int setrop2 (INT ndrawmode );
*/
/Files/volnet/code_draw.rar