void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CPen pen(m_nType,m_nLineWidth,RGB(255,0,0));
CPen * pOldPen=dc.SelectObject(&pen);
CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush* pOldBrush=dc.SelectObject(pBrush);
switch(m_nDrawType)
{
case 1:
dc.SetPixel(point,RGB(255,0,0));
break;
case 2:
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
break;
case 3:
dc.Ellipse(CRect(m_ptOrigin,point));
break;
case 4:
dc.Rectangle(CRect(m_ptOrigin,point));
break;
}
dc.SelectObject(pOldBrush);
dc.SelectObject(pOldPen);
CView::OnLButtonUp(nFlags, point);
}
见上面程序段中的红色部分,很简单的两个参数m_nType指定要创建画笔的类型,m_nLineWidth指定要创建画笔的宽度,这两个参数均由外部传入。在m_nLineWidth赋值为1时,程序都能按照m_nType参数指定的类型进行绘图;但是当m_nLineWidth的赋值大于1时(即线宽大于1时),所绘制的图形均为实线类型,不管m_nType的值为多少。
解决:(参见MSDN)
-
PS_DASH Creates a dashed pen. Valid only when the pen width is 1 or less, in device units.
-
PS_DOT Creates a dotted pen. Valid only when the pen width is 1 or less, in device units.
-
PS_DASHDOT Creates a pen with alternating dashes and dots. Valid only when the pen width is 1 or less, in device units.
-
PS_DASHDOTDOT Creates a pen with alternating dashes and double dots. Valid only when the pen width is 1 or less, in device units