0
votes

I need to draw simple graph of some dependency. Since I only wanted to display it once for device debugging purposes I used simple MFC Dialog based App. In OnPaint() message I draw graph labels, rectangle, texts. But when I use LineTo and MoveTo methods in for loop nothing happens. If I use them outside loop everything is drawn as it should. I also tried to create simple POINT array with 3 points and draw this in loop. No luck either. Here is the code:

CPaintDC dc(&mGraph);

int grXStart = 50;
int grYStart = 10;
int maxHeight = 600;
int zeroPos = 400;
int maxWidth = 580;
int numLines = 11;
int minLine = 50;

dc.Rectangle(grXStart,grYStart,grXStart+maxWidth,grYStart+maxHeight);
FOR(iY,numLines)
{
    dc.MoveTo(grXStart - 5, grYStart + maxHeight-(numLines-iY)*minLine);
    dc.LineTo(grXStart + 5, grYStart + maxHeight-(numLines-iY)*minLine);
    CString xLabel;
    xLabel.Format(_T("%d"),((numLines-iY)*minLine-(maxHeight-zeroPos)));
    RECT rct;
    rct.left = grXStart - 30;
    rct.right = grXStart - 5;
    rct.bottom = grYStart + maxHeight-(numLines-iY)*minLine + 8;
    rct.top = grYStart + maxHeight-(numLines-iY)*minLine - 8;
    dc.DrawText(xLabel,&rct,0);
}

int numLinesX = 12;
FOR(iX,numLinesX)
{
    dc.MoveTo(grXStart + 50*iX, grYStart + maxHeight-5);
    dc.LineTo(grXStart + 50*iX, grYStart + maxHeight+5);
    CString xLabel;
    xLabel.Format(_T("%d"),(50*iX+360));
    RECT rct;
    rct.left = grXStart + 50*iX - 17;
    rct.right = grXStart + 50*iX + 17;
    rct.bottom = grYStart + maxHeight + 8 + 18;
    rct.top = grYStart + maxHeight - 8 + 18;
    dc.DrawText(xLabel,&rct,0);
}


dc.MoveTo(grXStart + 10, grYStart+zeroPos - 50);
dc.LineTo(grXStart + 11, grYStart+zeroPos - 30);

dc.MoveTo(grXStart + 50, grYStart+zeroPos - 50);
dc.LineTo(grXStart + 49, grYStart+zeroPos - 30);

// everything to this point is drawn
if(mPointsLoaded)
{
    // nothing from this is drawn and yes - mPointsLoaded == true
    dc.MoveTo(grXStart, grYStart + zeroPos);
    for(int iSm = 0; iSm < 581; iSm++)
    {
        int newPtY = grYStart + zeroPos - mPointsLarge[iSm].y;
        if(newPtY > 500) newPtY = grYStart + zeroPos - 100;
        if(newPtY <= 0) newPtY = grYStart;
        dc.LineTo(grXStart + iSm, newPtY);
        dc.MoveTo(grXStart + iSm, newPtY);
    }
}
CDialog::OnPaint();

I checked the array in debug for some incorrect numbers, but there are none.

Thank you in advance for your help.

1

1 Answers

2
votes

Ok I had chat with my old colleague and he figured out the solution. I used PostMessage to call OnPaint message handler and control was considered to be up to date, so no redraw happend. I had to call Invalidate on CDialog (not mGraph control) with bErase=0 to fire OnPaint and update region. My stupid fault.