Mouse buttons choosing color

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

THis code is straight out of a text book but the color is always black. It is suppossed to draw black with one mouse button and red with the other. Can anyone spot a problem? Thanks.
************
void CMouseDlg::OnMouseMove(UINT nFlags, CPoint point)
{

if (((nFlags & MK_LBUTTON) == MK_LBUTTON) ||
((nFlags & MK_RBUTTON) == MK_RBUTTON))
{

CPen lpen;
CClientDC dc(this);
CPen* pPrevPen = NULL;



if ((nFlags & MK_LBUTTON) == MK_LBUTTON)
CPen lpen(PS_SOLID, 16, RGB(255,0,0));

if ((nFlags & MK_RBUTTON) == MK_RBUTTON)
CPen lpen(PS_SOLID, 16, RGB(255,0,0));

pPrevPen = dc.SelectObject(&lpen);
dc.MoveTo(m_iPrevx, m_iPrevy);
dc.LineTo(point.x, point.y);
m_iPrevx = point.x;
m_iPrevy = point.y;
dc.SelectObject(pPrevPen);
}
CDialog::OnMouseMove(nFlags, point);
}




void CMouseDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
m_iPrevx = point.x;
m_iPrevy = point.y;
CDialog::OnLButtonDown(nFlags, point);
}

void CMouseDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
m_iPrevx = point.x;
m_iPrevy = point.y;
CDialog::OnRButtonDown(nFlags, point);
}
*******************
 
try the following code MouseMove
=====================================================
CPen lpen;
CClientDC dc(this);
CPen* pPrevPen = NULL;


if ((nFlags & MK_LBUTTON) == MK_LBUTTON)
lpen.CreatePen(PS_SOLID, 16, RGB(255,0,0));

if ((nFlags & MK_RBUTTON) == MK_RBUTTON)
lpen.CreatePen(PS_SOLID, 16, RGB(255,0,0));

pPrevPen = dc.SelectObject(&lpen);
dc.MoveTo(m_iPrevx, m_iPrevy);
dc.LineTo(point.x, point.y);
m_iPrevx = point.x;
m_iPrevy = point.y;
dc.SelectObject(pPrevPen);
=====================================================

Notice that I have changed the CPen lpen(....) to lpen.CreatePen

I think this will work for you.

darrellm58 said:
THis code is straight out of a text book but the color is always black. It
is suppossed to draw black with one mouse button and red with the other. Can
anyone spot a problem? Thanks.
 
Back
Top