win10, vc6; create a new single-document project;
Add the message processing function of the left mouse button up to the visual class;
Add a member variable to the class header file: CPoint m_ptOrigin;
Initialized in the constructor of the CPP file depending on the class,
CMypenView::CMypenView()
{
// TODO: add construction code here
m_ptOrigin.x=20;
m_ptOrigin.y=20;
}
The effect of not using a brush is as follows;
After creating a 10 pixel wide, red brush, the line drawing effect is as follows;
The left mouse button bounces the code;
void CMypenView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//HDC hdc;
//hdc = ::GetDC(m_hWnd);
//MoveToEx(hdc,m_ptOrigin.x, m_ptOrigin.y,NULL);
//LineTo(hdc, point.x, point.y);
//::ReleaseDC(m_hWnd,hdc);
CWindowDC dc(this);
CPen pen(PS_SOLID, 10, RGB(255,0,0));
CPen *ptr = dc.SelectObject(&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
dc.SelectObject(ptr);
CView::OnLButtonUp(nFlags, point);
}
CPen *ptr = dc.SelectObject(&pen);
The pen is a CPen object; after creating the pen, use dc.SelectObject to select the device description table; draw a line again to use the pen; the return value of the above statement is the original pen; use dc.SelectObject(ptr) after drawing the picture Select the original brush into the device description table;
The style of the brush is defined as follows,
PS_SOLID: solid line
PS_DOT: dotted line
PS_DASH: dashed line PS_DASHDOT: dashed line
PS_DASHDOTDOT: dashed line with
two dots
PS_NULL: transparent line
PS_INSIDEFRAME: along the inner border