# Thread: drawing right onto form and making it stay

1. ## drawing right onto form and making it stay

i was bored, so i decided to make sin/cos/tan waves. i got htem to work directly on the form (its a MFC app), but when imove a window over it, all of the window's contents get erased

Code:
```void Csin_wave_mfcDlg::OnBnClickedButton2()
{

CPaintDC dc(this);
CRect lRect;
GetClientRect(lRect);
lRect.NormalizeRect();

// Calculate the distance between each of the lines
CPoint pStart;
CPoint pEnd;
this->GetWindowRect(lRect);

// Specify the starting points
pStart.y = lRect.Height()/2;
pStart.x = 0;

double angle=0,rx=0,x=0, y=0;

//sin wave
dc.MoveTo(pStart);
while(angle<=lRect.Width()*2){
x = rx;// + sin(90/180 * 3.14);
y = 100 * sin(angle/180 * 3.14)+lRect.Height()/2;

pEnd.x = x;
pEnd.y = y;

dc.LineTo(pEnd);
rx+=1;
angle+=2;
Sleep(1);
}

//cosine wave
dc.MoveTo(pStart);
rx=0; angle=0;
while(angle<=lRect.Width()*2){
x = rx;// + sin(90/180 * 3.14);
y = 100 * cos(angle/180 * 3.14)+lRect.Height()/2;

pEnd.x = x;
pEnd.y = y;

dc.LineTo(pEnd);
rx+=1;
angle+=2;
Sleep(1);
}

//tan wave
dc.MoveTo(pStart);
rx=0; angle=0;
while(angle<=lRect.Width()*2){
if(angle==90){
angle +=2;
}

x = rx;// + sin(90/180 * 3.14);
y = 100 * tan(angle/180 * 3.14)+lRect.Height()/2;

pEnd.x = x;
pEnd.y = y;

dc.LineTo(pEnd);
rx+=1;
angle+=2;
Sleep(1);
}
}```
i probably have to add something into the OnPaint() function, but i don't know what. its being drawn directly onto the form

2. Move your drawing code to your OnPaint handler and call ::InvalidateRect in response to your button click event.

3. did that, froze

4. Try a timer and, in response to a WM_TIMER msg, do your maths and call InvalidateRect; your OnPaint should just do the drawing. Activate the timer in response to the btn click event.

5. Why the timer?

The only thing you need to do is do the math before calling InvalidateRect. (Do both of these in response to the button click) then paint it on WM_PAINT.

Its what Ken said minus the timer.

-Futura

6. >>Why the timer?<<

: *shuffles from foot to foot* -er - it was late and I -er- didn't read the post.

Glad that someone's paying attention, though.

( ...still haven't read the post...)

7. what does invalidaterect() do?

8. >>what does invalidaterect() do?<<

This.

9. oh ok, so baiscally anything drawn within the rectangle passed is "saved".

cool, im gonna tryto make this into a non-mfc app now. thanks for your h elp

10. >> oh ok, so baiscally anything drawn within the rectangle passed is "saved".

Well, errmm ok, It's like this. Say you have your program's window open, then you take another window and drag it over top of the corner of your prog's window. Now Windows flags this region as "invalid", in other words, it needs to be painted, and call's the windows procedure and passes it the WM_PAINT message. Now, calling InvalidateRect is like manually saying this part of the window is not right any more, it's contents need to be repainted. Then, the next time that the procedure receives the WM_PAINT message, then rectangle passed to InvalidateRect, gets painted.

I don't know about the technical accuracy of my description, but I hope you get the point.

>> *shuffles from foot to foot* -er - it was late and I -er- didn't read the post.

Well, that's not as bad as the boner I pulled in this thread.

-Futura