0

我正在用 MFC 做一个小型绘图工具。

我定义了五种形状:矩形、直线、圆形、椭圆和点。intm_drawType用于在五种形状之间切换。

代码如下:

void CDrawToolView::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_startRect=TRUE; 
    m_startPoint=point;
    m_OldPoint=point; 
    ::SetCursor(m_HCross);  
    CView::OnLButtonDown(nFlags, point);
}

void CDrawToolView::OnMouseMove(UINT nFlags, CPoint point)
{
    CClientDC dc(this); 
    dc.SetROP2(R2_NOT);
    dc.SetROP2(R2_NOT);
    dc.SelectStockObject(NULL_BRUSH); 
    if(TRUE==m_startRect) 
    {  
        switch(m_drawType)  
        {  
             case 1://Rectangle  
                ::SetCursor(m_HCross);  
                dc.Rectangle(CRect(m_startPoint,m_OldPoint));  
                dc.Rectangle(CRect(m_startPoint,point));  
                m_OldPoint=point;  
             break;  

             case 2: //Line  
                ::SetCursor(m_HCross);  
                dc.MoveTo(m_startPoint);  
                dc.LineTo(m_OldPoint); 
                dc.MoveTo(m_startPoint);  
                dc.LineTo(point);  
                m_OldPoint=point;  
             break;
             .
             .
             .
        }
    }
    CView::OnMouseMove(nFlags, point);
}

void CDrawToolView::OnLButtonUp(UINT nFlags, CPoint point)
{
    m_startRect=FALSE;
    ::ClipCursor(NULL);
    CClientDC dc(this);
    dc.SelectStockObject(NULL_BRUSH);   
    switch(m_drawType)  
    {  
    case 1:   //Retangle 
        dc.Rectangle(CRect(m_startPoint,m_OldPoint));  
        dc.Rectangle(CRect(m_startPoint,point));  
    break;  

    case 2:  //Line  
         dc.MoveTo(m_startPoint);  
         dc.LineTo(m_OldPoint);    
         dc.MoveTo(m_startPoint);  
         dc.LineTo(point);  
    break;  
    .
    .
    .
    } 
    CView::OnLButtonUp(nFlags, point);
}

void CDrawToolView::OnEditShape() // when click menu-edit-shape a dialog is pop up
{
    CShapeDlg dlg;  
    dlg.DoModal();  
}

弹出对话框如下:

在此处输入图像描述

我创建了一个包含五个按钮的对话框。我的问题是我不知道如何在按钮和这五个形状之间建立联系。有人可以帮我吗?

4

2 回答 2

1

The dialog should have an m_drawtype variable that it sets when a button is clicked. When DoModal returns this variable can be read to get the result:

dlg.DoModal();  
m_drawtype = dlg.m_drawtype;
于 2013-09-06T12:44:27.410 回答
1

所以你的问题是如何制作CShapeDlgset的按钮m_drawType

您的对话框可能有一个本地公共drawType变量。为按钮引入按钮单击事件处理程序。

BEGIN_MESSAGE_MAP(CShapeDlg, CDialog)
    ON_BN_CLICKED(IDC_BUTTON1, &CShapeDlg::OnBnClickedButton1)
    ... etc.
    ON_BN_CLICKED(IDC_BUTTON5, &CShapeDlg::OnBnClickedButton5)
END_MESSAGE_MAP()

使处理程序将本地 drawType 变量设置为整数值 0 - 4,视情况而定。

void CShapeDlg::OnBnClickedButton1() { drawType = 0; }
... etc.
void CShapeDlg::OnBnClickedButton5() { drawType = 4; }

然后

if (dlg.DoModal() == IDOK)
  m_drawType = dlg.drawType;
于 2013-09-06T12:41:56.500 回答