对话框绘图
对话框界面绘图,代码如下:
void CMyDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptEnd=point;
CClientDC dc(this);
CRect rect;
CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(* pBrush);
switch(m_nDrawType)
{
case 1:
dc.SetPixel(m_ptEnd,RGB(255,0,0));
break;
case 2:
dc.MoveTo(m_ptOrigin);
dc.LineTo(m_ptEnd);
break;
case 3:
dc.Rectangle(CRect(m_ptOrigin,m_ptEnd));
break;
case 4:
dc.Ellipse(CRect(m_ptOrigin,m_ptEnd));
break;
}
CGraph graph(m_nDrawType, m_ptOrigin, m_ptEnd);
m_ptrArray.Add(&graph);
CDialog::OnLButtonUp(nFlags, point);
}
void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptOrigin=point;
CDialog::OnLButtonDown(nFlags, point);
}
其中 ,m_ptrArray为CMyDlg的CPtrArray成员变量,设为Public。 CGraph是自己构造的类。m_nDrawType, m_ptOrigin, m_ptEnd均为Public.
现在要重绘图形,在::OnPaint()中加入代码如下:
void CMyDlg::OnPaint()
{
int r=m_ptrArray.GetSize();
CClientDC dc(this);
CRect rect;
CPen pen(PS_SOLID,2,RGB(0,0,255));
CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(* pBrush);
for (int i=0;i<r;i++)
{
int f =((CGraph*)m_ptrArray.GetAt( i ) )->m_nDrawType;
switch( f )
{
case 1:
dc.SetPixel(((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd,RGB(255,0,0));
break;
case 2:
dc.MoveTo(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin);
dc.LineTo(((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd);
break;
case 3:
dc.Rectangle(CRect(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin,((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd));
break;
case 4:
dc.Ellipse(CRect(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin,((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd));
break;
}
}
。
。
。
CDialog::OnPaint();
}
调试中发现,f 赋不到值。怎么回事?谢谢!
[ 本帖最后由 rui_biao 于 2008-4-9 05:02 编辑 ]