drawing right onto form and making it stay

This is a discussion on drawing right onto form and making it stay within the Windows Programming forums, part of the Platform Specific Boards category; i was bored, so i decided to make sin/cos/tan waves. i got htem to work directly on the form (its ...

  1. #1
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455

    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. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Move your drawing code to your OnPaint handler and call ::InvalidateRect in response to your button click event.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    did that, froze
    Last edited by the Wookie; 01-04-2003 at 07:07 PM.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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.

    For more information on the use/abuse of timers, search this board.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    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
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>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...)

    ...sorry about that.

  7. #7
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    what does invalidaterect() do?

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>what does invalidaterect() do?<<

    This.

  9. #9
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    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. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    >> 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
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program closes when startup form closes
    By dcboy in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 02:40 AM
  2. Windows Form?
    By MK4554 in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 09:43 AM
  3. making a form to what i build
    By aleminio in forum C++ Programming
    Replies: 3
    Last Post: 06-13-2004, 07:06 AM
  4. Making an MFC form to suit
    By TJJ in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2004, 11:20 AM
  5. Need help making a choice
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-09-2003, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21