发新话题
打印

对话框绘图

对话框绘图

对话框界面绘图,代码如下:
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 编辑 ]

TOP

换个头像,看见广告就眼红,直接封ID。

TOP

可能是构造的类有问题:
class CGraph : public CObject  
{
DECLARE_SERIAL(CGraph)
public:
void Draw(CDC *pDC);
CPoint m_ptOrigin;
CPoint m_ptEnd;
UINT m_nDrawType;
CGraph();
CGraph(UINT m_nDrawType,CPoint m_ptOrigin,CPoint m_ptEnd);
void Serialize(CArchive &ar);
virtual ~CGraph();
};
Class选项卡中My Class点击新建类时,类的类型选择Generic Class, 基类Drived from 编辑框中填入 CObject, 确认,系统提示:Class Wizard 没有找到合适的包含CObject基类的头文件.

TOP

发新话题