I have a picture control box (a CStatic) in a dialog. When the user presses a button in the dialog I need the onPaint() to draw an image in it. The problem is, The image is drawn at the loading of the dialog. How do I prevent this and call it only at button press.
My onPaint code;
void CStaticGraph::OnPaint()
{
POINT xy[1000];
CPaintDC dc(this); // device context for painting
CRect Recto;
char LocDim[80];
GetWindowRect(&Recto);
CPoint pBottom,pTop,pLeft;
CPoint p[50];
pBottom.SetPoint(0,0);
pTop.SetPoint(0,Recto.Height());
pLeft.SetPoint(Recto.Width(),Recto.Height());
dc.MoveTo(pBottom);
dc.LineTo(pTop);
dc.LineTo(pLeft);
int y[] ={80,120,180,200};
int x=0;
for(int i=0; i<sizeof(y);i++){
p[i].SetPoint(Recto.Height()-x,y[i]);
if(i>0){
dc.MoveTo(p[i-1]);
dc.LineTo(p[i]);
}
x+=50;
}
}
As you can see I'm plotting a graph, I also need to pass data (the y[] values) at button press. I haven't done that yet.
Thanks.
Add a variable, such as a BOOL, to your CStaticGraph class to act as a flag to tell OnPaint() what to do. Initialize the variable in the constructor and change it when the button is clicked. For example:
In the header file for CStaticGraph add:
BOOL m_fButtonPressed;
In the CStaticGraph constructor add:
m_fButtonPressed = FALSE;
In your button click handler do something like:
void CStaticGraph::OnButtonClick()
{
m_fButtonPressed = TRUE;
Invalidate();
}
Then in your OnPaint only draw the graph when flag is set:
void CStaticGraph::OnPaint()
(
CPaintDC dc(this);
if ( m_fButtonPressed )
{
// Clear the window
return;
}
// Draw the graph
. . .
}