Click to See Complete Forum and Search --> : Simple plot on a dialog


madenai
June 8th, 2007, 06:09 AM
Hi,

I have this small MFC project which displays a grid on a dialog box.

//////////////////////////////////////////////////////////////////////////////////
// SimplePlotDlg.h
//
class CSimplePlotDlg : public CDialog
{
.
.
.
public:

CBrush Brush;
CPen m_GridPen;
CPen m_PlotPen;
CRect m_PlotRect;
CDC* m_dcArea;

public:
void Grid();
.
.
.
///////////////////////////////////////////////////////////////////////////////
// SimplePlotDlg.cpp
//

BOOL CSimplePlotDlg::OnInitDialog()
{
.
.
.
// TODO: Add extra initialization here
m_dcArea = m_Plot.GetDC();
m_PlotPen.CreatePen(PS_SOLID,1,RGB(255,0,0));
m_GridPen.CreatePen(PS_SOLID,1,RGB(0,120,0));
Brush.CreateSolidBrush(RGB(0,0,0));
m_Plot.GetClientRect(&m_PlotRect);

return TRUE; // return TRUE unless you set the focus to a control
}
.
.
.


///////////////////////////////////////////////////////////////////////////
void CSimplePlotDlg::Grid()
{
int i;
m_dcArea->SelectObject(Brush);
m_dcArea->SelectObject(m_GridPen);
m_dcArea->FillRect(&m_PlotRect,&Brush);

for (i = 1;i<(m_PlotRect.Width()/10);i++)
{
m_dcArea->MoveTo(i*((double)m_PlotRect.Width()/10),0);
m_dcArea->LineTo(i*((double)m_PlotRect.Width()/10),m_PlotRect.Height());
}
for (i = 1;i<(m_PlotRect.Height()/10);i++)
{
m_dcArea->MoveTo(0,m_PlotRect.Height()-i*((double)m_PlotRect.Height()/10));
m_dcArea->LineTo(m_PlotRect.Width(),m_PlotRect.Height()-i*((double)m_PlotRect.Height()/10));
}
}

///////////////////////////////////////////////////////////////////////////////
void CSimplePlotDlg::OnPlot()
{
// TODO: Add your control notification handler code here

Grid();
}

A button click from the dialog execute OnPlot and generate the grid.

I would like to generate this grid in the call of InitDialog() with the previous lines of code .

BOOL CSimplePlotDlg::OnInitDialog()
{
.
.
.
// TODO: Add extra initialization here
m_dcArea = m_Plot.GetDC();
m_PlotPen.CreatePen(PS_SOLID,1,RGB(255,0,0));
m_GridPen.CreatePen(PS_SOLID,1,RGB(0,120,0));
Brush.CreateSolidBrush(RGB(0,0,0));
m_Plot.GetClientRect(&m_PlotRect);
Grid(); <-------- HERE !

return TRUE; // return TRUE unless you set the focus to a control
}
.
.
.

However it doesn't work and works only on the button click !!(the above structure).

PS Project is attached

Regards

madenai

MrViggy
June 8th, 2007, 01:27 PM
Please use code tags. You should only be painting in the 'OnPaint' function. Handle the WM_PAINT message. Otherwise, you'll run into strange issues like only painting once; not painting after a move, or another window covers your dialog.

Viggy

madenai
June 8th, 2007, 02:28 PM
Hi Viggy,

What do you mean by code tags. Do you mean the Grid() function should be inserted in OnPaint().

Grid() can not be executed in InitDialog() because OnPaint() is fired after
Also I want to get rid of the control button.

PS could you please explain your suggestions "code tags " on the code I have submitted.

Thanks

MikeAThon
June 8th, 2007, 02:42 PM
It's often a mistake to draw directly onto the dialog. One common alterrnative is to derive a new class from CStatic (for example), which performs the desired drawing in its OnPaint handler. Then embed an instance of this new class, as a child window of the dialog.

With respect to "code" tags, they have nothing to do with your code. They are helpful formatting tags when posting to this forum. Surround code with the tags [ code ] and [ /code ] (without the spaces), and your code will be displayed like this:
BOOL CSimplePlotDlg::OnInitDialog()
{
.
.
.
// TODO: Add extra initialization here
m_dcArea = m_Plot.GetDC();
m_PlotPen.CreatePen(PS_SOLID,1,RGB(255,0,0));
m_GridPen.CreatePen(PS_SOLID,1,RGB(0,120,0));
Brush.CreateSolidBrush(RGB(0,0,0));
m_Plot.GetClientRect(&m_PlotRect);

return TRUE; // return TRUE unless you set the focus to a control
}
Mike