简体   繁体   中英

Use button in a dialog to select shape in MFC

I am doing a small drawing tool with MFC.

I define five shapes: rectangle, line, circle, ellipse and dot. int m_drawType is used to switch between five shapes.

The code as below:

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();  
}

The pop up dialog as below:

在此输入图像描述

I have created a dialog of five buttons. My problem is I don't know how to link between the buttons and these five shapes. Could some one help me?

So your question is how to make the buttons of CShapeDlg set m_drawType ?

Your dialog could have a local public drawType variable. Introduce button click event handlers for the buttons.

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

Make the handlers set the local drawType variable to integer value 0 - 4 as the case may be.

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

Then

if (dlg.DoModal() == IDOK)
  m_drawType = dlg.drawType;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM