Re: PolyLine and autoscrolling canvas
Luigino wrote:
Hello again Joe and Stephen,
I've corrected whatever you suggested to, Joe, made a Create() method
to initialize, removed the OnCreate and the rest of stuffs...
Stephen, I modified also the OnPaint() thing as you said as you can
see below:
void CCaChart::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SetBkColor(RGB(0,0,0));
CRect rect;
GetClientRect(&rect);
int save = dc.SaveDC();
if(bSetDraw)
{
CMemDC mDC(&dc);
//mDC.SetBkColor(RGB(0,0,0));
mDC.SetMapMode(MM_ANISOTROPIC);
mDC.SetWindowOrg(rect.BottomRight());
mDC.SetViewportOrg(0, 0);
mDC.SetViewportExt(-1, -1);
//mDC.SetViewportExt(-rect.right, -rect.bottom);
mDC.SetWindowExt(1, 1);
//mDC.SetWindowExt(rect.Width(), rect.Height());
// ********** Background ***********
// Grid
if (bActivateGrid)
{
CPen qLinePen(PS_SOLID, 1, RGB(0,139,0));
mDC.SelectObject(&qLinePen);
// Grid - Horizontal lines
mDC.MoveTo(1, 0);
mDC.LineTo(rect.Width(), 0);
int height = rect.Height();
int maxlines = height / (int)12.5;
for (int i=1;i<=maxlines;i++){
int y_axis = (int)((double)i * 12.5);
if (y_axis <= rect.Height()) {
mDC.MoveTo(1, y_axis);
mDC.LineTo(rect.Width(),
y_axis);
}
}
// Grid - Vertical lines
mDC.MoveTo(0, 0);
mDC.LineTo(0, rect.Height());
int width = rect.Width();
maxlines = width / (int)12.5;
for (int i=1;i<=maxlines;i++){
int x_axis = (int)(((double)i * 12.5)
- gridOffset);
if (x_axis <= rect.Width()) {
mDC.MoveTo(x_axis, 1);
mDC.LineTo(x_axis,
rect.Height());
}
}
qLinePen.DeleteObject();
}
// *********** graphic component ***********
// based on choice of graph type
CPen qPolylinePen(PS_SOLID, 1, RGB(0, 255, 0));
switch (iGraphType)
{
case GRAPH_BARS:
break;
case GRAPH_LINES:
{
if (vtPoints.capacity() == 1)
{
mDC.SelectObject(qPolylinePen);
vectfPoints* pointsline =
&vtPoints[0];
mDC.Polyline(&(*pointsline)
[0], (int)pointsline->size());
//mDC->PolyPolyline()
qPolylinePen.DeleteObject();
}
}
break;
default:
break;
}
GDI_FLUSH();
}
dc.RestoreDC(save);
}
but when I resize window the vertical and horizontal lines are
repainted to the whole canvas but the Polyline stuff isn't rescaled; I
mean I have values in the vtPoints array which MM_ANISOTROPIC should
rescale automatically due having set SetViewportExt and SetWindowExt
respectively to -1, -1 and 1, 1 so I guess it should scale 1:1 and the
polyline graphic should be adjusted when resizing window....but it
doesn't... what I could have missed or made wrong in those
SetViewportExt and SetWindowExt?...
Thanks
Ciao
Luigi
Your problem is with the (over) use of rect. You don't need it anywhere
except to initialize the CDC's mapping.
You do not care what the size of the rectangle on the screen is. You
want your drawing to fill the rectangle.
Ignoring the mapping calls, you want to be able to do the equivalent of
MoveTo(1,2); LineTo(7,3). The MoveTo and LineTo parameters do not
change based on the screen coordinates. You're going to let the CDC
manage that. This allows you to collect the data in quantities that
make sense to you without regard for the underlying screen resolution.
SetWindowExt is in logical units.
SetViewportExt is in screen units.
All your drawing is then in logical units.
It will make it easier to debug if you draw your gridlines in logical
units. Or just draw a line from one corner diagonally across to the
opposite corner. When that's working properly, adding the polyline
should be easy.
Steve