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.